Skip to content

[Maintenance] Refactor Options classes and usage #59

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Mar 27, 2025
28 changes: 24 additions & 4 deletions config/services/form.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@

-->

<container xmlns="http://symfony.com/schema/dic/services" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<container
xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"
>
<parameters>
<parameter key="sylius_mollie.form.type.mollie_gateway_config.validation_groups" type="collection">
<parameter>sylius</parameter>
Expand Down Expand Up @@ -77,9 +80,8 @@

<service id="sylius_mollie.form.extension.mollie_gateway_config" class="Sylius\MolliePlugin\Form\Type\MollieGatewayConfigType">
<argument>%sylius_mollie.model.mollie_gateway_config.class%</argument>
<argument type="service" id="sylius_mollie.documentation.documentation_links"/>
<argument>%sylius_locale.locale%</argument>
<argument>%sylius_mollie.form.type.mollie_gateway_config.validation_groups%</argument>
<argument>%sylius_locale.locale%</argument>
<tag name="form.type"/>
</service>

Expand Down Expand Up @@ -127,5 +129,23 @@
<argument>%sylius_mollie.model.mollie_gateway_config_translation.class%</argument>
<tag name="form.type" />
</service>

<service id="sylius_mollie.form.type.countries_restriction_choice" class="Sylius\MolliePlugin\Form\Type\CountriesRestrictionChoiceType">
<tag name="form.type" />
</service>

<service id="sylius_mollie.form.type.payment_type_choice" class="Sylius\MolliePlugin\Form\Type\PaymentTypeChoiceType">
<argument type="service" id="sylius_mollie.documentation.documentation_links" />

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

<service id="sylius_mollie.form.type.payment_surcharge_type_choice" class="Sylius\MolliePlugin\Form\Type\PaymentSurchargeFeeTypeChoiceType">
<tag name="form.type" />
</service>

<service id="sylius_mollie.form.type.logger_level_choice" class="Sylius\MolliePlugin\Form\Type\LoggerLevelChoiceType">
<tag name="form.type" />
</service>
</services>
</container>
17 changes: 9 additions & 8 deletions src/Action/CaptureAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
use Sylius\Component\Core\Repository\PaymentRepositoryInterface;
use Sylius\MolliePlugin\Action\Api\BaseApiAwareAction;
use Sylius\MolliePlugin\Entity\OrderInterface;
use Sylius\MolliePlugin\Payments\PaymentTerms\Options;
use Sylius\MolliePlugin\Payments\ApiTypeRestrictedPaymentMethods;
use Sylius\MolliePlugin\Payments\PaymentType;
use Sylius\MolliePlugin\Request\Api\CreateCustomer;
use Sylius\MolliePlugin\Request\Api\CreateInternalRecurring;
use Sylius\MolliePlugin\Request\Api\CreateOnDemandSubscription;
Expand Down Expand Up @@ -126,31 +127,31 @@ public function execute($request): void
$this->gateway->execute(new CreateOnDemandSubscriptionPayment($details));
}
} else {
if (isset($details['metadata']['methodType']) && Options::PAYMENT_API === $details['metadata']['methodType']) {
if (in_array($details['metadata']['molliePaymentMethods'], Options::getOnlyOrderAPIMethods(), true)) {
if (isset($details['metadata']['methodType']) && PaymentType::PAYMENT_API === $details['metadata']['methodType']) {
if (in_array($details['metadata']['molliePaymentMethods'], ApiTypeRestrictedPaymentMethods::onlyOrderApi(), true)) {
throw new InvalidArgumentException(sprintf(
'Method %s is not allowed to use %s',
$details['metadata']['molliePaymentMethods'],
Options::PAYMENT_API,
PaymentType::PAYMENT_API,
));
}

$this->gateway->execute(new CreatePayment($details));
}

if (isset($details['metadata']['methodType']) && Options::ORDER_API === $details['metadata']['methodType']) {
if (in_array($details['metadata']['molliePaymentMethods'], Options::getOnlyPaymentAPIMethods(), true)) {
if (isset($details['metadata']['methodType']) && PaymentType::ORDER_API === $details['metadata']['methodType']) {
if (in_array($details['metadata']['molliePaymentMethods'], ApiTypeRestrictedPaymentMethods::onlyPaymentApi(), true)) {
throw new InvalidArgumentException(sprintf(
'Method %s is not allowed to use %s',
$details['metadata']['molliePaymentMethods'],
Options::ORDER_API,
PaymentType::ORDER_API,
));
}

$this->gateway->execute(new CreateOrder($details));
}

if (isset($details['metadata']['methodType']) && Options::ORDER_API === $details['metadata']['methodType']) {
if (isset($details['metadata']['methodType']) && PaymentType::ORDER_API === $details['metadata']['methodType']) {
$this->gateway->execute(new CreateOrder($details));
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/Action/ConvertMolliePaymentAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
use Sylius\MolliePlugin\Helper\ConvertOrderInterface;
use Sylius\MolliePlugin\Helper\IntToStringConverterInterface;
use Sylius\MolliePlugin\Helper\PaymentDescriptionInterface;
use Sylius\MolliePlugin\Payments\PaymentTerms\Options;
use Sylius\MolliePlugin\Payments\PaymentType;
use Sylius\MolliePlugin\Provider\Divisor\DivisorProviderInterface;
use Sylius\MolliePlugin\Resolver\PaymentLocaleResolverInterface;
use Webmozart\Assert\Assert;
Expand Down Expand Up @@ -134,16 +134,16 @@ public function execute($request): void

if (false === $this->mollieApiClient->isRecurringSubscription()) {
$details['customerId'] = $model['customer_mollie_id'] ?? null;
$details['metadata']['methodType'] = Options::PAYMENT_API;
$details['metadata']['methodType'] = PaymentType::PAYMENT_API;

if (null !== ($paymentLocale = $this->paymentLocaleResolver->resolveFromOrder($order))) {
$details['locale'] = $paymentLocale;
}

if (Options::ORDER_API === array_search($method->getPaymentType(), Options::getAvailablePaymentType(), true)) {
if (PaymentType::ORDER_API === array_search($method->getPaymentType(), PaymentType::getAllAvailable(), true)) {
unset($details['customerId']);

$details['metadata']['methodType'] = Options::ORDER_API;
$details['metadata']['methodType'] = PaymentType::ORDER_API;
$details = $this->orderConverter->convert($order, $details, $divisor, $method);
}
}
Expand Down
4 changes: 0 additions & 4 deletions src/Entity/MollieGatewayConfigInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@

interface MollieGatewayConfigInterface extends MethodInterface, TranslatableInterface
{
public const ALL_COUNTRIES = 'ALL_COUNTRIES';

public const SELECTED_COUNTRIES = 'SELECTED_COUNTRIES';

public function getId(): int;

public function getMethodId(): ?string;
Expand Down
38 changes: 38 additions & 0 deletions src/Form/Type/CountriesRestrictionChoiceType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

/*
* This file is part of the Sylius Mollie Plugin package.
*
* (c) Sylius Sp. z o.o.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Sylius\MolliePlugin\Form\Type;

use Sylius\MolliePlugin\Model\CountriesRestriction;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\OptionsResolver\OptionsResolver;

final class CountriesRestrictionChoiceType extends AbstractType
{
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'label' => 'sylius_mollie_plugin.ui.country_level_restriction',
'choices' => [
'sylius_mollie_plugin.ui.all_countries' => CountriesRestriction::ALL,
'sylius_mollie_plugin.ui.selected_countries' => CountriesRestriction::SELECTED,
],
]);
}

public function getParent(): string
{
return ChoiceType::class;
}
}
69 changes: 69 additions & 0 deletions src/Form/Type/LoggerLevelChoiceType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

/*
* This file is part of the Sylius Mollie Plugin package.
*
* (c) Sylius Sp. z o.o.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Sylius\MolliePlugin\Form\Type;

use Sylius\MolliePlugin\Logger\LoggerLevel;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\OptionsResolver\OptionsResolver;

final class LoggerLevelChoiceType extends AbstractType
{
public const TYPE_DEBUG = 'debug';

public const TYPE_LOG = 'log';

public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'label' => 'sylius_mollie_plugin.ui.debug_level_log',
'log_type' => self::TYPE_DEBUG,
'choices' => self::getDebugChoices(),
]);

$resolver->setAllowedValues('log_type', [self::TYPE_DEBUG, self::TYPE_LOG]);

$resolver->addNormalizer('choices', static function ($options, $choices) {
if ($options['log_type'] === self::TYPE_LOG) {
return self::getLogChoices();
}

return $choices;
});
}

public function getParent(): string
{
return ChoiceType::class;
}

/** @return array<string, int> */
private static function getLogChoices(): array
{
return [
'sylius_mollie_plugin.ui.info' => LoggerLevel::LOG_ERRORS,
'sylius_mollie_plugin.ui.errors' => LoggerLevel::LOG_EVERYTHING,
];
}

/** @return array<string, int> */
private static function getDebugChoices(): array
{
return [
'sylius_mollie_plugin.ui.nothing_log' => LoggerLevel::LOG_DISABLED,
'sylius_mollie_plugin.ui.errors' => LoggerLevel::LOG_ERRORS,
'sylius_mollie_plugin.ui.everything' => LoggerLevel::LOG_EVERYTHING,
];
}
}
50 changes: 15 additions & 35 deletions src/Form/Type/MollieGatewayConfigType.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,13 @@
use Sylius\Bundle\ProductBundle\Form\Type\ProductType as ProductFormType;
use Sylius\Bundle\ResourceBundle\Form\Type\AbstractResourceType;
use Sylius\Bundle\ResourceBundle\Form\Type\ResourceTranslationsType;
use Sylius\MolliePlugin\Documentation\DocumentationLinksInterface;
use Sylius\MolliePlugin\Entity\MollieGatewayConfigInterface;
use Sylius\MolliePlugin\Entity\MollieGatewayConfigTranslationInterface;
use Sylius\MolliePlugin\Entity\ProductType;
use Sylius\MolliePlugin\Factory\MollieSubscriptionGatewayFactory;
use Sylius\MolliePlugin\Form\Type\Translation\MollieGatewayConfigTranslationType;
use Sylius\MolliePlugin\Options\Country\Options as CountryOptions;
use Sylius\MolliePlugin\Payments\Methods\AbstractMethod;
use Sylius\MolliePlugin\Payments\PaymentTerms\Options;
use Sylius\MolliePlugin\Payments\ApiTypeRestrictedPaymentMethods;
use Sylius\MolliePlugin\Payments\PaymentType;
use Sylius\MolliePlugin\Validator\Constraints\PaymentSurchargeType;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
Expand All @@ -39,9 +37,8 @@ final class MollieGatewayConfigType extends AbstractResourceType
{
public function __construct(
string $dataClass,
private readonly DocumentationLinksInterface $documentationLinks,
array $validationGroups,
private readonly string $defaultLocale,
array $validationGroups = [],
) {
parent::__construct($dataClass, $validationGroups);
}
Expand All @@ -67,12 +64,7 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
'label' => 'sylius_mollie_plugin.ui.payment_name',
'entry_type' => MollieGatewayConfigTranslationType::class,
])
->add('paymentType', ChoiceType::class, [
'label' => 'sylius_mollie_plugin.ui.payment_type',
'choices' => Options::getAvailablePaymentType(),
'help' => $this->documentationLinks->getPaymentMethodDoc(),
'help_html' => true,
])
->add('paymentType', PaymentTypeChoiceType::class)
->add('qrCodeEnabled', CheckboxType::class, [
'label' => 'sylius_mollie_plugin.ui.qr_code',
])
Expand All @@ -95,10 +87,7 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
->add('customizeMethodImage', CustomizeMethodImageType::class, [
'label' => false,
])
->add('country_restriction', ChoiceType::class, [
'label' => 'sylius_mollie_plugin.ui.country_level_restriction',
'choices' => CountryOptions::getCountriesConfigOptions(),
])
->add('country_restriction', CountriesRestrictionChoiceType::class)
->add('countryLevel_excluded', CountryType::class, [
'label' => 'sylius_mollie_plugin.ui.country_level_exclude',
'required' => false,
Expand All @@ -125,9 +114,8 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
->add('loggerEnabled', CheckboxType::class, [
'label' => 'sylius_mollie_plugin.ui.debug_level_enabled',
])
->add('loggerLevel', ChoiceType::class, [
'label' => 'sylius_mollie_plugin.ui.debug_level_log',
'choices' => Options::getDebugLevels(),
->add('loggerLevel', LoggerLevelChoiceType::class, [
'log_type' => LoggerLevelChoiceType::TYPE_DEBUG,
])
->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event): void {
/** @var MollieGatewayConfigInterface $object */
Expand All @@ -139,12 +127,8 @@ public function buildForm(FormBuilderInterface $builder, array $options): void

if (MollieSubscriptionGatewayFactory::FACTORY_NAME === $factoryName) {
$form->remove('paymentType');
$form->add('paymentType', ChoiceType::class, [
'label' => 'sylius_mollie_plugin.ui.payment_type',
'choices' => Options::getAvailablePaymentType(),
'help' => $this->documentationLinks->getPaymentMethodDoc(),
'help_html' => true,
'empty_data' => Options::PAYMENT_API_VALUE,
$form->add('paymentType', PaymentTypeChoiceType::class, [
'empty_data' => PaymentType::PAYMENT_API_VALUE,
'attr' => [
'disabled' => 'disabled',
],
Expand All @@ -163,13 +147,9 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
$object = $form->getData();
$data = $event->getData();

if (in_array($object->getMethodId(), Options::getOnlyOrderAPIMethods(), true)) {
if (in_array($object->getMethodId(), ApiTypeRestrictedPaymentMethods::onlyOrderApi(), true)) {
$form->remove('paymentType');
$form->add('paymentType', ChoiceType::class, [
'label' => 'sylius_mollie_plugin.ui.payment_type',
'choices' => Options::getAvailablePaymentType(),
'help' => $this->documentationLinks->getPaymentMethodDoc(),
'help_html' => true,
$form->add('paymentType', PaymentTypeChoiceType::class, [
'attr' => [
'disabled' => 'disabled',
],
Expand All @@ -183,12 +163,12 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
$object = $event->getForm()->getData();
$data = $event->getData();

if (in_array($object->getMethodId(), Options::getOnlyOrderAPIMethods(), true)) {
$data['paymentType'] = AbstractMethod::ORDER_API;
if (in_array($object->getMethodId(), ApiTypeRestrictedPaymentMethods::onlyOrderApi(), true)) {
$data['paymentType'] = PaymentType::ORDER_API_VALUE;
}

if (in_array($object->getMethodId(), Options::getOnlyPaymentAPIMethods(), true)) {
$data['paymentType'] = AbstractMethod::PAYMENT_API;
if (in_array($object->getMethodId(), ApiTypeRestrictedPaymentMethods::onlyPaymentApi(), true)) {
$data['paymentType'] = PaymentType::PAYMENT_API_VALUE;
}

$event->setData($data);
Expand Down
6 changes: 2 additions & 4 deletions src/Form/Type/MollieGatewayConfigurationType.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

use Sylius\MolliePlugin\Client\MollieApiClient;
use Sylius\MolliePlugin\Documentation\DocumentationLinksInterface;
use Sylius\MolliePlugin\Payments\PaymentTerms\Options;
use Sylius\MolliePlugin\Validator\Constraints\LiveApiKeyIsNotBlank;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
Expand Down Expand Up @@ -100,9 +99,8 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
range(1, 200, 1),
),
])
->add('loggerLevel', ChoiceType::class, [
'label' => 'sylius_mollie_plugin.ui.debug_level_log',
'choices' => Options::getDebugLevels(),
->add('loggerLevel', LoggerLevelChoiceType::class, [
'log_type' => LoggerLevelChoiceType::TYPE_DEBUG,
])
->add('components', CheckboxType::class, [
'label' => 'sylius_mollie_plugin.ui.enable_components',
Expand Down
Loading