← Back
Editing: AddressSubscriber.php
<?php namespace App\EventSubscriber; use App\Event\AddressCreateEvent; use App\Repository\AdresseRepository; use App\Repository\ItemRepository; use App\Service\SolutionService; use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\Validator\Exception\ValidationFailedException; use Symfony\Component\Validator\Validator\ValidatorInterface; class AddressSubscriber implements EventSubscriberInterface { public function __construct( private readonly SolutionService $solutionService, private readonly AdresseRepository $addressRepository, private readonly ItemRepository $itemRepository, private readonly ValidatorInterface $validator ) { } public static function getSubscribedEvents(): array { return [ AddressCreateEvent::NAME => 'onAddressCreate', ]; } public function onAddressCreate(AddressCreateEvent $event): void { $address = $event->getAddress(); $agency = $address->getAgency(); $address->setSolution($this->solutionService->getCurrent()); $itemsList = $this->itemRepository->findDefaultByAgency($agency, $this->solutionService->getCurrent()); foreach ($itemsList as $item) { $address->addItem($item); } $errors = $this->validator->validate($address); if (count($errors) > 0) { throw new ValidationFailedException('Validation error', $errors); } $this->addressRepository->save($address, true); } }
Save File
Cancel