Découplage Équilibré
Avec Symfony
chalas_r
chalasr
baksla.sh
@chalas_r
COUPLAGE
Niveau d'interaction entre deux composants.
@chalas_r
@chalas_r
PLACER LE CURSEUR.
RAPPORT COÛT/BÉNÉFICE
@chalas_r
ESSAYER,
ÉVALUER,
CHANGER.
@chalas_r
namespace AfupDay\Talk\Controller;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Attribute\Route;
#[Route("/talk/schedule", name: "schedule_talk")]
class ScheduleTalkController
{
public function __invoke(Request $request): Response
{
// ...
return new Response(...);
}
}
@chalas_r
class ScheduleTalkController
{
public function __construct(
private Security $security,
private ValidatorInterface $validator,
// ...
) {}
public function __invoke(Request $request): Response
{
// ...
}
}
@chalas_r
use Symfony\Component\HttpKernel\Attribute\MapRequestPayload;
class ScheduleTalkController
{
public function __invoke(
#[MapRequestPayload] ScheduleTalkDto $scheduleTalk,
): Response {
// ...
}
}
@chalas_r
$form = $this->formFactory->createForm(ScheduleTalk::class);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
/** @var ScheduleTalkDto $scheduleTalk */
$scheduleTalk = $form->getData();
$talk = new Talk(
$scheduleTalk->title,
$scheduleTalk->speaker,
$scheduleTalk->timeSlot
);
if ($scheduleTalk->confirmed) {
$talk->confirm();
}
$this->talkRepository->add($talk);
}
@chalas_r
use Symfony\Component\Messenger\HandleTrait;
use Symfony\Component\Messenger\MessageBusInterface;
final class ScheduleTalkCommand
{
public function __construct(
public string $title,
public string $author,
public string $timeSlot,
// ...
) {}
}
@chalas_r
use AfupDay\Talk\Repository\TalkRepositoryInterface;
use Symfony\Component\Messenger\HandleTrait;
use Symfony\Component\Messenger\MessageBusInterface;
final class ScheduleTalkCommandHandler
{
public function __construct(
private TalkRepositoryInterface $talkRepository
) {}
public function handle(ScheduleTalkCommand $command)
{
$talk = new Talk(
$command->title,
$command->author,
$command->timeSlot
);
// ...
$this->talkRepository->add($talk);
}
}
@chalas_r
use Symfony\Component\Messenger\HandleTrait;
use Symfony\Component\Messenger\MessageBusInterface;
class ScheduleTalkController
{
use HandleTrait;
public function __construct(
private MessageBusInterface $commandBus
// ...
) {}
public function __invoke(Request $request): Response
{
$this->commandBus->handle(
new ScheduleTalkCommand($request->request->get('...'))
);
//...
}
}
Merci !
@chalas_r