src/Form/FormHandler.php line 56

Open in your IDE?
  1. <?php
  2. /**
  3.  * @author Antony Tkachenko <at@canaryknight.ru>
  4.  */
  5. namespace App\Form;
  6. use App\Exception\ValidationException;
  7. use App\Form\Contracts\FormDtoInterface;
  8. use App\Utm\Utm;
  9. use Psr\Log\LoggerInterface;
  10. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  11. use Symfony\Component\PropertyInfo\PropertyInfoExtractorInterface;
  12. use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter;
  13. use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
  14. use Symfony\Component\Validator\Validator\ValidatorInterface;
  15. class FormHandler
  16. {
  17.     /** @var DenormalizerInterface */
  18.     private $serializer;
  19.     /** @var ValidatorInterface */
  20.     private $validator;
  21.     /** @var EventDispatcherInterface  */
  22.     private $eventDispatcher;
  23.     /** @var PropertyInfoExtractorInterface  */
  24.     private $propertyInfoExtractor;
  25.     public function __construct(
  26.         DenormalizerInterface $serializer,
  27.         ValidatorInterface $validator,
  28.         EventDispatcherInterface $eventDispatcher,
  29.         PropertyInfoExtractorInterface $propertyInfoExtractor
  30.     ) {
  31.         $this->serializer $serializer;
  32.         $this->validator $validator;
  33.         $this->eventDispatcher $eventDispatcher;
  34.         $this->propertyInfoExtractor $propertyInfoExtractor;
  35.     }
  36.     public function handle(string $dtoClass, array $data, ?Utm $utm)
  37.     {
  38.         $utm $utm ?? Utm::fromArray([]);
  39.         if (isset($data['type'])) {
  40.             unset($data['type']);
  41.         }
  42.         if (isset($data['ym_client_id'])) {
  43.             $utm->setYmClientId($data['ym_client_id']);
  44.             unset($data['ym_client_id']);
  45.         }
  46.         $dto $this->serializer->denormalize(
  47.             $data,
  48.             $dtoClass
  49.         );
  50.         $violations $this->validator->validate($dto);
  51.         if (!== count($violations)) {
  52.             throw new ValidationException($violations);
  53.         }
  54.         $c = new CamelCaseToSnakeCaseNameConverter();
  55.         $path explode('\\'$dtoClass);
  56.         $formName $c->normalize(array_pop($path));
  57.         /** @var FormDtoInterface $dto */
  58.         $data $this->extractData($dto);
  59.         $event = new FormHandledEvent($dto$data$formName$utm);
  60.         $this->eventDispatcher->dispatch($eventFormHandledEvent::NAME);
  61.         //$this->mailer->send($email);
  62.         return $dto;
  63.     }
  64.     public function extractData(object $dto): array
  65.     {
  66.         $properties = (new \ReflectionObject($dto))
  67.             ->getProperties(\ReflectionProperty::IS_PUBLIC);
  68.         $map = [];
  69.         $class get_class($dto);
  70.         foreach ($properties as $property) {
  71.             $name $property->getName();
  72.             if (empty($dto->{$name})) {
  73.                 continue;
  74.             }
  75.             $label $this->propertyInfoExtractor->getShortDescription($class$name);
  76.             $map[$label] = $dto->{$name};
  77.         }
  78.         return $map;
  79.     }
  80. }