Skip to content

Commit

Permalink
Refactor into Plugin configuration + validate input
Browse files Browse the repository at this point in the history
  • Loading branch information
Roshyo committed Jan 26, 2022
1 parent cf1f729 commit 9592a6b
Show file tree
Hide file tree
Showing 29 changed files with 399 additions and 282 deletions.
6 changes: 6 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@
]
},
"config": {
"allow-plugins": {
"composer/package-versions-deprecated": true,
"dealerdirect/phpcodesniffer-composer-installer": true,
"ergebnis/composer-normalize": true,
"symfony/thanks": true
},
"sort-packages": true
},
"extra": {
Expand Down
124 changes: 0 additions & 124 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,130 +21,6 @@
<code>$giftCard-&gt;getOrder()</code>
</PossiblyNullArgument>
</file>
<file src="src/DependencyInjection/Configuration.php">
<MixedMethodCall occurrences="101">
<code>addDefaultsIfNotSet</code>
<code>addDefaultsIfNotSet</code>
<code>addDefaultsIfNotSet</code>
<code>addDefaultsIfNotSet</code>
<code>cannotBeEmpty</code>
<code>cannotBeEmpty</code>
<code>cannotBeEmpty</code>
<code>cannotBeEmpty</code>
<code>cannotBeEmpty</code>
<code>cannotBeEmpty</code>
<code>cannotBeEmpty</code>
<code>cannotBeEmpty</code>
<code>cannotBeEmpty</code>
<code>cannotBeEmpty</code>
<code>cannotBeEmpty</code>
<code>cannotBeEmpty</code>
<code>cannotBeEmpty</code>
<code>cannotBeEmpty</code>
<code>cannotBeEmpty</code>
<code>cannotBeEmpty</code>
<code>children</code>
<code>children</code>
<code>children</code>
<code>children</code>
<code>defaultValue</code>
<code>defaultValue</code>
<code>defaultValue</code>
<code>defaultValue</code>
<code>defaultValue</code>
<code>defaultValue</code>
<code>defaultValue</code>
<code>defaultValue</code>
<code>defaultValue</code>
<code>defaultValue</code>
<code>defaultValue</code>
<code>defaultValue</code>
<code>defaultValue</code>
<code>defaultValue</code>
<code>defaultValue</code>
<code>defaultValue</code>
<code>defaultValue</code>
<code>defaultValue</code>
<code>defaultValue</code>
<code>defaultValue</code>
<code>defaultValue</code>
<code>defaultValue</code>
<code>defaultValue</code>
<code>defaultValue</code>
<code>defaultValue</code>
<code>end</code>
<code>end</code>
<code>end</code>
<code>end</code>
<code>end</code>
<code>end</code>
<code>end</code>
<code>end</code>
<code>end</code>
<code>end</code>
<code>end</code>
<code>end</code>
<code>end</code>
<code>end</code>
<code>end</code>
<code>end</code>
<code>end</code>
<code>end</code>
<code>end</code>
<code>end</code>
<code>end</code>
<code>end</code>
<code>end</code>
<code>end</code>
<code>example</code>
<code>info</code>
<code>max</code>
<code>min</code>
<code>scalarNode</code>
<code>scalarNode</code>
<code>scalarNode</code>
<code>scalarNode</code>
<code>scalarNode</code>
<code>scalarNode</code>
<code>scalarNode</code>
<code>scalarNode</code>
<code>scalarNode</code>
<code>scalarNode</code>
<code>scalarNode</code>
<code>scalarNode</code>
<code>scalarNode</code>
<code>scalarNode</code>
<code>scalarNode</code>
<code>scalarNode</code>
<code>scalarNode</code>
<code>scalarNode</code>
<code>scalarNode</code>
<code>scalarNode</code>
<code>scalarNode</code>
<code>scalarNode</code>
<code>scalarNode</code>
<code>scalarNode</code>
</MixedMethodCall>
<PossiblyNullReference occurrences="6">
<code>arrayNode</code>
<code>arrayNode</code>
<code>arrayNode</code>
<code>arrayNode</code>
<code>end</code>
<code>integerNode</code>
</PossiblyNullReference>
<PossiblyUndefinedMethod occurrences="6">
<code>arrayNode</code>
<code>arrayNode</code>
<code>arrayNode</code>
<code>arrayNode</code>
<code>end</code>
<code>integerNode</code>
</PossiblyUndefinedMethod>
<UnusedMethodCall occurrences="1">
<code>end</code>
</UnusedMethodCall>
</file>
<file src="src/Doctrine/ORM/GiftCardRepository.php">
<MixedReturnTypeCoercion occurrences="2">
<code>array</code>
Expand Down
1 change: 1 addition & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<directory name="src/Fixture"/>
<directory name="tests/Application"/>
<directory name="vendor"/>
<file name="src/DependencyInjection/Configuration.php"/>
</ignoreFiles>
</projectFiles>
<plugins>
Expand Down
49 changes: 49 additions & 0 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,55 @@ public function getConfigTreeBuilder(): TreeBuilder
$rootNode
->addDefaultsIfNotSet()
->children()
->arrayNode('pdf_rendering')
->addDefaultsIfNotSet()
->children()
->scalarNode('default_orientation')->defaultValue('Portrait')->end()
->arrayNode('available_orientations')
->scalarPrototype()->end()
->defaultValue([
'Portrait',
'Landscape',
])
->end()
->scalarNode('default_page_size')->defaultValue('A4')->end()
->arrayNode('available_page_sizes')
->scalarPrototype()->end()
->defaultValue([
'A0',
'A1',
'A2',
'A3',
'A4',
'A5',
'A6',
'A7',
'A8',
'A9',
'B0',
'B1',
'B2',
'B3',
'B4',
'B5',
'B6',
'B7',
'B8',
'B9',
'B10',
'C5E',
'Comm10E',
'DLE',
'Executive',
'Folio',
'Ledger',
'Legal',
'Letter',
'Tabloid',
])
->end()
->end()
->end()
->scalarNode('driver')->defaultValue(SyliusResourceBundle::DRIVER_DOCTRINE_ORM)->end()
->integerNode('code_length')
->defaultValue(20)
Expand Down
28 changes: 27 additions & 1 deletion src/DependencyInjection/SetonoSyliusGiftCardExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,39 @@ final class SetonoSyliusGiftCardExtension extends AbstractResourceExtension
public function load(array $configs, ContainerBuilder $container): void
{
/**
* @var array{code_length: int, driver: string, resources: array<string, mixed>} $config
* @var array{
* pdf_rendering: array{
* default_orientation: string,
* available_orientations: array<array-key, string>,
* default_page_size: string,
* available_page_sizes: array<array-key, string>,
* },
* code_length: int,
* driver: string,
* resources: array<string, mixed>
* } $config
* @psalm-suppress PossiblyNullArgument
*/
$config = $this->processConfiguration($this->getConfiguration([], $container), $configs);
$loader = new XmlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));

$container->setParameter('setono_sylius_gift_card.code_length', $config['code_length']);
$container->setParameter(
'setono_sylius_gift_card.pdf_rendering.default_orientation',
$config['pdf_rendering']['default_orientation']
);
$container->setParameter(
'setono_sylius_gift_card.pdf_rendering.available_orientations',
$config['pdf_rendering']['available_orientations']
);
$container->setParameter(
'setono_sylius_gift_card.pdf_rendering.default_page_size',
$config['pdf_rendering']['default_page_size']
);
$container->setParameter(
'setono_sylius_gift_card.pdf_rendering.available_page_sizes',
$config['pdf_rendering']['available_page_sizes']
);

$this->registerResources('setono_sylius_gift_card', $config['driver'], $config['resources'], $container);

Expand Down
17 changes: 15 additions & 2 deletions src/Factory/GiftCardConfigurationFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,20 @@ final class GiftCardConfigurationFactory implements GiftCardConfigurationFactory

private FactoryInterface $imageFactory;

public function __construct(FactoryInterface $decoratedFactory, FactoryInterface $imageFactory)
{
private string $defaultOrientation;

private string $defaultPageSize;

public function __construct(
FactoryInterface $decoratedFactory,
FactoryInterface $imageFactory,
string $defaultOrientation,
string $defaultPageSize
) {
$this->decoratedFactory = $decoratedFactory;
$this->imageFactory = $imageFactory;
$this->defaultOrientation = $defaultOrientation;
$this->defaultPageSize = $defaultPageSize;
}

public function createNew(): GiftCardConfigurationInterface
Expand All @@ -29,6 +39,9 @@ public function createNew(): GiftCardConfigurationInterface
$backgroundImage = $this->imageFactory->createNew();
$configuration->setBackgroundImage($backgroundImage);

$configuration->setOrientation($this->defaultOrientation);
$configuration->setPageSize($this->defaultPageSize);

return $configuration;
}
}
24 changes: 10 additions & 14 deletions src/Form/Type/GiftCardConfigurationType.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

namespace Setono\SyliusGiftCardPlugin\Form\Type;

use Setono\SyliusGiftCardPlugin\Provider\PdfOrientationProviderInterface;
use Setono\SyliusGiftCardPlugin\Provider\PdfPageSizeProviderInterface;
use Sylius\Bundle\ResourceBundle\Form\Type\AbstractResourceType;
use Symfony\Component\Form\CallbackTransformer;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
Expand All @@ -16,23 +14,23 @@

final class GiftCardConfigurationType extends AbstractResourceType
{
private PdfPageSizeProviderInterface $pdfFormatProvider;
private array $availableOrientations;

private PdfOrientationProviderInterface $pdfOrientationProvider;
private array $availablePageSizes;

/**
* @psalm-param array<array-key, string> $validationGroups
*/
public function __construct(
PdfPageSizeProviderInterface $pdfFormatProvider,
PdfOrientationProviderInterface $pdfOrientationProvider,
array $availableOrientations,
array $availablePageSizes,
string $dataClass,
array $validationGroups
array $validationGroups = []
) {
parent::__construct($dataClass, $validationGroups);
$this->availableOrientations = $availableOrientations;
$this->availablePageSizes = $availablePageSizes;

$this->pdfFormatProvider = $pdfFormatProvider;
$this->pdfOrientationProvider = $pdfOrientationProvider;
parent::__construct($dataClass, $validationGroups);
}

public function buildForm(FormBuilderInterface $builder, array $options): void
Expand Down Expand Up @@ -64,17 +62,15 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
'label' => 'setono_sylius_gift_card.form.gift_card_configuration.default_validity_period',
]);
$builder->add('pageSize', ChoiceType::class, [
'choices' => $this->pdfFormatProvider->getAvailablePageSizes(),
'choices' => $this->availablePageSizes,
'label' => 'setono_sylius_gift_card.form.gift_card_configuration.page_size',
'choice_translation_domain' => false,
'choice_label' => function (string $value) {
return $value;
},
'placeholder' => PdfPageSizeProviderInterface::FORMAT_A5,
'empty_data' => PdfPageSizeProviderInterface::FORMAT_A5,
]);
$builder->add('orientation', ChoiceType::class, [
'choices' => $this->pdfOrientationProvider->getAvailableOrientations(),
'choices' => $this->availableOrientations,
'label' => 'setono_sylius_gift_card.form.gift_card_configuration.orientation',
'choice_translation_domain' => false,
'choice_label' => function (string $value) {
Expand Down
6 changes: 2 additions & 4 deletions src/Model/GiftCardConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Setono\SyliusGiftCardPlugin\Provider\PdfOrientationProviderInterface;
use Setono\SyliusGiftCardPlugin\Provider\PdfPageSizeProviderInterface;
use Sylius\Component\Core\Model\ImageInterface;
use Sylius\Component\Resource\Model\TimestampableTrait;
use Sylius\Component\Resource\Model\ToggleableTrait;
Expand Down Expand Up @@ -40,9 +38,9 @@ class GiftCardConfiguration implements GiftCardConfigurationInterface

protected ?string $defaultValidityPeriod = null;

protected ?string $pageSize = PdfPageSizeProviderInterface::FORMAT_A5;
protected ?string $pageSize = null;

protected ?string $orientation = PdfOrientationProviderInterface::ORIENTATION_PORTRAIT;
protected ?string $orientation = null;

public function __construct()
{
Expand Down
16 changes: 0 additions & 16 deletions src/Provider/PdfOrientationProvider.php

This file was deleted.

14 changes: 0 additions & 14 deletions src/Provider/PdfOrientationProviderInterface.php

This file was deleted.

Loading

0 comments on commit 9592a6b

Please sign in to comment.