src/EventListener/PostValidate/AccountBillingPostValidateTransformer.php line 55

Open in your IDE?
  1. <?php
  2. namespace App\EventListener\PostValidate;
  3. use App\DTO\AccountBilling\AccountBilling as AccountBillingDto;
  4. use App\Repository\AccountRepository;
  5. use App\Service\Manager\AccountBillingManager;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. use Symfony\Component\HttpFoundation\JsonResponse;
  8. use Symfony\Component\HttpKernel\Event\ViewEvent;
  9. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  10. use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
  11. class AccountBillingPostValidateTransformer implements EventSubscriberInterface
  12. {
  13.     private $accountRepository;
  14.     private $accountBillingManager;
  15.     private $normalizer;
  16.     public function __construct(
  17.         AccountRepository $accountRepository,
  18.         AccountBillingManager $accountBillingManager,
  19.         NormalizerInterface $normalizer
  20.     ) {
  21.         $this->accountRepository $accountRepository;
  22.         $this->accountBillingManager $accountBillingManager;
  23.         $this->normalizer $normalizer;
  24.     }
  25.     /**
  26.      * @return array
  27.      */
  28.     public static function getSubscribedEvents()
  29.     {
  30.         return [
  31.             PostValidateListener::POST_VALIDATE_TRANSFORMATION => 'transform',
  32.         ];
  33.     }
  34.     /**
  35.      * @param AccountBillingDto $payload
  36.      *
  37.      * @return bool
  38.      */
  39.     public function supportTransformation($payload)
  40.     {
  41.         return $payload instanceof AccountBillingDto;
  42.     }
  43.     /**
  44.      * @param ViewEvent $event
  45.      */
  46.     public function transform(ViewEvent $event)
  47.     {
  48.         /** @var AccountBillingDto $accountBillingDto */
  49.         $accountBillingDto $event->getControllerResult();
  50.         if (!$this->supportTransformation($accountBillingDto)) {
  51.             return;
  52.         }
  53.         $account $this->accountRepository->find($event->getRequest()->get('id'));
  54.         if (null === $account) {
  55.             throw new NotFoundHttpException();
  56.         }
  57.         if (null === $accountBilling $account->getAccountBilling()) {
  58.             $accountBilling $this->accountBillingManager->create(
  59.                 $account,
  60.                 $accountBillingDto->companyName,
  61.                 $accountBillingDto->address,
  62.                 $accountBillingDto->postCode,
  63.                 $accountBillingDto->city,
  64.                 $accountBillingDto->taxNumber,
  65.                 $accountBillingDto->regulationsAcceptance
  66.             );
  67.         } else {
  68.             $accountBilling->setCompanyName($accountBillingDto->companyName);
  69.             $accountBilling->setAddress($accountBillingDto->address);
  70.             $accountBilling->setPostCode($accountBillingDto->postCode);
  71.             $accountBilling->setCity($accountBillingDto->city);
  72.             $accountBilling->setTaxNumber($accountBillingDto->taxNumber);
  73.             $accountBilling->setRegulationsAcceptance($accountBillingDto->regulationsAcceptance);
  74.             $this->accountBillingManager->save($accountBilling);
  75.         }
  76.         $event->setResponse(new JsonResponse($this->normalizer->normalize($accountBilling)));
  77.     }
  78. }