src/Factory/Ecommerce/AbstractGiftFormFactory.php line 47

Open in your IDE?
  1. <?php
  2. namespace App\Factory\Ecommerce;
  3. use App\Entity\Ecommerce\Extra;
  4. use App\Entity\Ecommerce\Gift;
  5. use App\Factory\Ecommerce\Interfaces\GiftFormFactoryInterface;
  6. use App\Form\Ecommerce\GiftType;
  7. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  8. use Symfony\Component\Form\FormFactoryInterface;
  9. use Symfony\Component\Form\FormInterface;
  10. use Symfony\Contracts\Translation\TranslatorInterface;
  11. use WAM\Component\Core\Util\ClassTools;
  12. /**
  13.  * Class AbstractGiftFormFactory.
  14.  *
  15.  * @author Juanjo Martínez <jmartinez@wearemarketing.com>
  16.  */
  17. abstract class AbstractGiftFormFactory implements GiftFormFactoryInterface
  18. {
  19.     const FORM_METHOD 'POST';
  20.     protected FormFactoryInterface $formFactory;
  21.     protected GiftType $formType;
  22.     private TranslatorInterface $translator;
  23.     /**
  24.      * AbstractGiftFormFactory constructor.
  25.      */
  26.     public function __construct(FormFactoryInterface $formFactoryGiftType $formTypeTranslatorInterface $translator)
  27.     {
  28.         $this->formFactory $formFactory;
  29.         $this->formType $formType;
  30.         $this->translator $translator;
  31.     }
  32.     private function getExtraString(Extra $extra): string
  33.     {
  34.         $price $extra->getPrice() ? $extra->getPrice().'€' $this->translator->trans('app.gifthasextra.included');
  35.         return (string) $extra->getTitle().' - '.$price;
  36.     }
  37.     protected function getForm(array $options): FormInterface
  38.     {
  39.         $form $this->formFactory->create(ClassTools::realClass($this->formType), nullarray_merge([
  40.             'method' => self::FORM_METHOD,
  41.         ], $options));
  42.         return $form;
  43.     }
  44.     protected function extractChoices(Gift $object): array
  45.     {
  46.         $choices = [];
  47.         $defaultValues = [];
  48.         foreach ($object->getGiftHasExtras() as $giftHasExtraItem) {
  49.             if ($giftHasExtraItem->getExtra()->getExtraType()) {
  50.                 $extraType $giftHasExtraItem->getExtra()->getExtraType()->getTitle();
  51.             } else {
  52.                 $extraType 'Undefined Extra Type';
  53.             }
  54.             if ($giftHasExtraItem->getExtra()->isPublished()) {
  55.                 $extraId $giftHasExtraItem->getExtra()->getId();
  56.                 $choices[$extraType][$extraId] = $giftHasExtraItem->getExtra();
  57.             }
  58.             if ($giftHasExtraItem->getIncluded()) {
  59.                 $defaultValues[$extraType]['default_value'] = $giftHasExtraItem->getExtra();
  60.             } else {
  61.                 if (empty($defaultValues[$extraType]['default_value'])) {
  62.                     $defaultValues[$extraType]['default_value'] = 'app.global.select_one';
  63.                 }
  64.             }
  65.         }
  66.         return [$choices$defaultValues];
  67.     }
  68.     /**
  69.      * @param $choices
  70.      * @param $defaultValues
  71.      */
  72.     protected function addFields($choices$defaultValuesFormInterface $form): void
  73.     {
  74.         $iteration 1;
  75.         foreach ($choices as $key => $items) {
  76.             $defaultValue $defaultValues[$key]['default_value'];
  77.             if ($defaultValues[$key]['default_value'] instanceof Extra) {
  78.                 $defaultValueId $defaultValue->getId();
  79.                 $defaultValue $this->getExtraString($defaultValues[$key]['default_value']);
  80.                 unset($items[$defaultValueId]);
  81.             }
  82.             $form->add('extras'.$iterationChoiceType::class, [
  83.                 'label' => $key,
  84.                 'choices' => $items,
  85.                 'placeholder' => $defaultValue,
  86.                 'required' => false,
  87.                 'choice_value' => function ($choice) {
  88.                     return $choice $choice->getId() : null;
  89.                 },
  90.                 'choice_label' => function ($choice) use ($items) {
  91.                     return $this->getExtraString($items[$choice->getId()]);
  92.                 },
  93.                 'choice_attr' => function ($choice) use ($items) {
  94.                     return ['data-text' => $this->getExtraString($items[$choice->getId()]), 'data-price' => $items[$choice->getId()]->getPrice()];
  95.                 },
  96.             ]);
  97.             ++$iteration;
  98.         }
  99.     }
  100. }