Skip to content

Commit

Permalink
Removed abstract class that wasn't necessary
Browse files Browse the repository at this point in the history
  • Loading branch information
loevgaard committed Dec 9, 2024
1 parent 3a0eac7 commit 2c178a4
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 138 deletions.
103 changes: 0 additions & 103 deletions src/Form/Type/AbstractConfigurablePromotionElementType.php

This file was deleted.

85 changes: 82 additions & 3 deletions src/Form/Type/PromotionRuleType.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,105 @@

namespace Setono\SyliusCatalogPromotionPlugin\Form\Type;

use Setono\SyliusCatalogPromotionPlugin\Model\PromotionRuleInterface;
use Sylius\Bundle\ResourceBundle\Form\Registry\FormTypeRegistryInterface;
use Sylius\Bundle\ResourceBundle\Form\Type\AbstractResourceType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Webmozart\Assert\Assert;

final class PromotionRuleType extends AbstractConfigurablePromotionElementType
final class PromotionRuleType extends AbstractResourceType
{
public function buildForm(FormBuilderInterface $builder, array $options = []): void
/**
* @param array<array-key, string> $validationGroups
*/
public function __construct(private readonly FormTypeRegistryInterface $formTypeRegistry, string $dataClass, array $validationGroups = [])
{
parent::buildForm($builder, $options);
parent::__construct($dataClass, $validationGroups);
}

public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('type', PromotionRuleChoiceType::class, [
'label' => 'setono_sylius_catalog_promotion.form.promotion_rule.type',
'attr' => [
'data-form-collection' => 'update',
],
])
->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event): void {
$type = $this->getRegistryIdentifier($event->getForm(), $event->getData());
if (null === $type) {
return;
}

$this->addConfigurationFields($event->getForm(), (string) $this->formTypeRegistry->get($type, 'default'));
})
->addEventListener(FormEvents::POST_SET_DATA, function (FormEvent $event): void {
$type = $this->getRegistryIdentifier($event->getForm(), $event->getData());
if (null === $type) {
return;
}

$event->getForm()->get('type')->setData($type);
})
->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event): void {
/** @var mixed $data */
$data = $event->getData();
Assert::isArray($data);

if (!isset($data['type'])) {
return;
}

Assert::string($data['type']);

$this->addConfigurationFields($event->getForm(), (string) $this->formTypeRegistry->get($data['type'], 'default'));
})
;
}

public function configureOptions(OptionsResolver $resolver): void
{
parent::configureOptions($resolver);

$resolver
->setDefault('configuration_type', null)
->setAllowedTypes('configuration_type', ['string', 'null'])
;
}

public function getBlockPrefix(): string
{
return 'setono_sylius_catalog_promotion_promotion_rule';
}

protected function addConfigurationFields(FormInterface $form, string $configurationType): void
{
$form->add('configuration', $configurationType, [
'label' => false,
]);
}

/**
* @param mixed $data
*/
protected function getRegistryIdentifier(FormInterface $form, $data = null): ?string
{
if ($data instanceof PromotionRuleInterface && null !== $data->getType()) {
return $data->getType();
}

if ($form->getConfig()->hasOption('configuration_type')) {
$res = $form->getConfig()->getOption('configuration_type');
Assert::nullOrString($res);

return $res;
}

return null;
}
}
16 changes: 0 additions & 16 deletions src/Model/ConfigurablePromotionElementInterface.php

This file was deleted.

8 changes: 7 additions & 1 deletion src/Model/PromotionRuleInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,19 @@

use Sylius\Component\Resource\Model\ResourceInterface;

interface PromotionRuleInterface extends ResourceInterface, ConfigurablePromotionElementInterface
interface PromotionRuleInterface extends ResourceInterface
{
public function getId(): ?int;

public function getType(): ?string;

public function setType(string $type): void;

public function getConfiguration(): array;

public function setConfiguration(array $configuration): void;

public function getPromotion(): ?PromotionInterface;

public function setPromotion(?PromotionInterface $promotion): void;
}
32 changes: 17 additions & 15 deletions src/Resources/config/services/form.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,58 +14,60 @@
</parameters>

<services>
<service id="setono_sylius_catalog_promotion.form_registry.rule" class="Sylius\Bundle\ResourceBundle\Form\Registry\FormTypeRegistry" />
<service id="setono_sylius_catalog_promotion.form_registry.rule"
class="Sylius\Bundle\ResourceBundle\Form\Registry\FormTypeRegistry"/>

<service id="setono_sylius_catalog_promotion.form.type.promotion"
class="Setono\SyliusCatalogPromotionPlugin\Form\Type\PromotionType">
<argument>%setono_sylius_catalog_promotion.model.promotion.class%</argument>
<argument>%setono_sylius_catalog_promotion.form.type.promotion.validation_groups%</argument>
<tag name="form.type" />
<tag name="form.type"/>
</service>

<service id="setono_sylius_catalog_promotion.form.type.promotion_rule"
class="Setono\SyliusCatalogPromotionPlugin\Form\Type\PromotionRuleType">
<argument type="service" id="setono_sylius_catalog_promotion.form_registry.rule"/>
<argument>%setono_sylius_catalog_promotion.model.promotion_rule.class%</argument>
<argument type="service" id="setono_sylius_catalog_promotion.form_registry.rule" />
<argument>%setono_sylius_catalog_promotion.form.type.promotion_rule.validation_groups%</argument>
<tag name="form.type" />

<tag name="form.type"/>
</service>

<service id="setono_sylius_catalog_promotion.form.type.promotion_rule.collection"
class="Setono\SyliusCatalogPromotionPlugin\Form\Type\PromotionRuleCollectionType">
<argument type="service" id="setono_sylius_catalog_promotion.registry.rule" />
<tag name="form.type" />
<argument type="service" id="setono_sylius_catalog_promotion.registry.rule"/>
<tag name="form.type"/>
</service>

<service id="setono_sylius_catalog_promotion.form.type.promotion_rule_choice"
class="Setono\SyliusCatalogPromotionPlugin\Form\Type\PromotionRuleChoiceType">
<argument>%setono_sylius_catalog_promotion.promotion_rules%</argument>
<tag name="form.type" />
<tag name="form.type"/>
</service>

<!-- Rules -->
<service id="setono_sylius_catalog_promotion.form.type.promotion_rule.has_taxon_configuration"
class="Setono\SyliusCatalogPromotionPlugin\Form\Type\Rule\HasTaxonConfigurationType">
<argument type="service" id="sylius.form.type.data_transformer.taxons_to_codes" />
<tag name="form.type" />
<argument type="service" id="sylius.form.type.data_transformer.taxons_to_codes"/>
<tag name="form.type"/>
</service>

<service id="setono_sylius_catalog_promotion.form.type.promotion_rule.has_not_taxon_configuration"
class="Setono\SyliusCatalogPromotionPlugin\Form\Type\Rule\HasNotTaxonConfigurationType">
<argument type="service" id="sylius.form.type.data_transformer.taxons_to_codes" />
<tag name="form.type" />
<argument type="service" id="sylius.form.type.data_transformer.taxons_to_codes"/>
<tag name="form.type"/>
</service>

<service id="setono_sylius_catalog_promotion.form.type.promotion_rule.contains_products_configuration"
class="Setono\SyliusCatalogPromotionPlugin\Form\Type\Rule\ContainsProductsConfigurationType">
<argument type="service" id="sylius.form.type.data_transformer.products_to_codes" />
<tag name="form.type" />
<argument type="service" id="sylius.form.type.data_transformer.products_to_codes"/>
<tag name="form.type"/>
</service>

<service id="setono_sylius_catalog_promotion.form.type.promotion_rule.contains_product_configuration"
class="Setono\SyliusCatalogPromotionPlugin\Form\Type\Rule\ContainsProductConfigurationType">
<argument type="service" id="sylius.repository.product" />
<tag name="form.type" />
<argument type="service" id="sylius.repository.product"/>
<tag name="form.type"/>
</service>
</services>
</container>

0 comments on commit 2c178a4

Please sign in to comment.