vendor/roothirsch/core-bundle/Behaviors/Attributable/EventSubscriber/AttributeInjectorEventSubscriber.php line 68

Open in your IDE?
  1. <?php
  2. namespace Roothirsch\CoreBundle\Behaviors\Attributable\EventSubscriber;
  3. use ApiPlatform\Core\Security\ExpressionLanguage;
  4. use Doctrine\ORM\EntityRepository;
  5. use Roothirsch\CoreBundle\Behaviors\Attributable\Form\PreProcessAttributeInterface;
  6. use Roothirsch\CoreBundle\Behaviors\Attributable\MappedSuperclass\AbstractAttributable;
  7. use Roothirsch\CoreBundle\Behaviors\Attributable\MappedSuperclass\AbstractAttribute;
  8. use Roothirsch\CoreBundle\Behaviors\Attributable\MappedSuperclass\AbstractAttributeValue;
  9. use Roothirsch\CoreBundle\Behaviors\Attributable\Service\AttributeTypesService;
  10. use Roothirsch\CoreBundle\Behaviors\Attributable\Type\AttributeTypeInterface;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  13. use Symfony\Component\Form\FormEvent;
  14. use Symfony\Component\Form\FormEvents;
  15. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
  16. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  17. class AttributeInjectorEventSubscriber implements EventSubscriberInterface
  18. {
  19.     /** @var AbstractAttributable  */
  20.     private $attributable null;
  21.     /** @var EntityRepository  */
  22.     private $repository null;
  23.     /**
  24.      * @var AttributeTypesService
  25.      */
  26.     private $attributeTypes;
  27.     /**
  28.      * @var TokenStorageInterface
  29.      */
  30.     private $tokenStorage;
  31.     /**
  32.      * AttributeInjectorEventSubscriber constructor.
  33.      * @param AttributeTypesService|null $attributeTypes
  34.      */
  35.     public function __construct(?AttributeTypesService $attributeTypesTokenStorageInterface $tokenStorage)
  36.     {
  37.         $this->attributeTypes $attributeTypes;
  38.         $this->tokenStorage $tokenStorage;
  39.     }
  40.     public function setAttributable(AbstractAttributable $attributable): self{
  41.         $this->attributable $attributable;
  42.         return $this;
  43.     }
  44.     public function setRepository(EntityRepository $repository): self{
  45.         $this->repository $repository;
  46.         return $this;
  47.     }
  48.     public static function getSubscribedEvents()
  49.     {
  50.         return [
  51.             FormEvents::PRE_SET_DATA => "buildForm"
  52.         ];
  53.     }
  54.     public function buildForm(FormEvent $event){
  55.         /** @var Form $form */
  56.         $form $event->getForm();
  57.         if(count($this->attributable->attributes()) === 0){
  58.             $attributes $this->repository->findBy([], ['position' => 'ASC']);
  59.             $this->attributable->setAttributes($attributes);
  60.         }
  61.         foreach ($this->attributable->attributes() as $attribute) {
  62.             $attributeType $this->attributeTypes->findByAttribute($attribute);
  63.             /** @var AbstractAttribute $attribute */
  64.             $options = [
  65.                 'label' => $attribute,
  66.                 'required' => $attribute->getRequired(),
  67.                 'help' => $attribute->getHelp(),
  68.             ];
  69.             if($attributeType){
  70.                 $attributeType->preAdd($attribute$options);
  71.             }
  72.             $this->resolveDefaultValue($options$attribute);
  73.             $form->add(
  74.                 $attribute->getName(),
  75.                 $this->attributeTypes->resolveFormType($attribute),
  76.                 $options
  77.             );
  78.             if($attributeType){
  79.                 $attributeType->postAdd($form$attribute);
  80.             }
  81.         }
  82.     }
  83.     private function resolveDefaultValue($optionsAbstractAttribute $attribute){
  84.         /** @var AbstractAttributeValue $value */
  85.         $value $this->attributable->getAttributeValue($attribute);
  86.         $hasDefaultValueAndIsEmpty $attribute->getDefaultValue() && $value->getValue() === NULL;
  87.         if ($hasDefaultValueAndIsEmpty) {
  88.             $defaultValue $attribute->getDefaultValue();
  89.             try {
  90.                 $e = new ExpressionLanguage();
  91.                 $defaultValue $e->evaluate($attribute->getDefaultValue(), array_merge(
  92.                     ['user' => $this->tokenStorage->getToken()->getUser()],
  93.                     $value->resolveExpression()
  94.                 ));
  95.             } catch (\Exception $exception){}
  96.             $value->setValue($defaultValue);
  97.         }
  98.     }
  99. }