<?php
namespace App\Factory\Ecommerce;
use App\Entity\Ecommerce\Extra;
use App\Entity\Ecommerce\Gift;
use App\Factory\Ecommerce\Interfaces\GiftFormFactoryInterface;
use App\Form\Ecommerce\GiftType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
use WAM\Component\Core\Util\ClassTools;
/**
* Class AbstractGiftFormFactory.
*
* @author Juanjo Martínez <jmartinez@wearemarketing.com>
*/
abstract class AbstractGiftFormFactory implements GiftFormFactoryInterface
{
const FORM_METHOD = 'POST';
protected FormFactoryInterface $formFactory;
protected GiftType $formType;
private TranslatorInterface $translator;
/**
* AbstractGiftFormFactory constructor.
*/
public function __construct(FormFactoryInterface $formFactory, GiftType $formType, TranslatorInterface $translator)
{
$this->formFactory = $formFactory;
$this->formType = $formType;
$this->translator = $translator;
}
private function getExtraString(Extra $extra): string
{
$price = $extra->getPrice() ? $extra->getPrice().'€' : $this->translator->trans('app.gifthasextra.included');
return (string) $extra->getTitle().' - '.$price;
}
protected function getForm(array $options): FormInterface
{
$form = $this->formFactory->create(ClassTools::realClass($this->formType), null, array_merge([
'method' => self::FORM_METHOD,
], $options));
return $form;
}
protected function extractChoices(Gift $object): array
{
$choices = [];
$defaultValues = [];
foreach ($object->getGiftHasExtras() as $giftHasExtraItem) {
if ($giftHasExtraItem->getExtra()->getExtraType()) {
$extraType = $giftHasExtraItem->getExtra()->getExtraType()->getTitle();
} else {
$extraType = 'Undefined Extra Type';
}
if ($giftHasExtraItem->getExtra()->isPublished()) {
$extraId = $giftHasExtraItem->getExtra()->getId();
$choices[$extraType][$extraId] = $giftHasExtraItem->getExtra();
}
if ($giftHasExtraItem->getIncluded()) {
$defaultValues[$extraType]['default_value'] = $giftHasExtraItem->getExtra();
} else {
if (empty($defaultValues[$extraType]['default_value'])) {
$defaultValues[$extraType]['default_value'] = 'app.global.select_one';
}
}
}
return [$choices, $defaultValues];
}
/**
* @param $choices
* @param $defaultValues
*/
protected function addFields($choices, $defaultValues, FormInterface $form): void
{
$iteration = 1;
foreach ($choices as $key => $items) {
$defaultValue = $defaultValues[$key]['default_value'];
if ($defaultValues[$key]['default_value'] instanceof Extra) {
$defaultValueId = $defaultValue->getId();
$defaultValue = $this->getExtraString($defaultValues[$key]['default_value']);
unset($items[$defaultValueId]);
}
$form->add('extras'.$iteration, ChoiceType::class, [
'label' => $key,
'choices' => $items,
'placeholder' => $defaultValue,
'required' => false,
'choice_value' => function ($choice) {
return $choice ? $choice->getId() : null;
},
'choice_label' => function ($choice) use ($items) {
return $this->getExtraString($items[$choice->getId()]);
},
'choice_attr' => function ($choice) use ($items) {
return ['data-text' => $this->getExtraString($items[$choice->getId()]), 'data-price' => $items[$choice->getId()]->getPrice()];
},
]);
++$iteration;
}
}
}