Skip to content
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

Allow to define custom rendering options for wkhtmltopdf #185

Merged
merged 9 commits into from
Mar 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
},
"config": {
"allow-plugins": {
"composer/package-versions-deprecated": true,
"dealerdirect/phpcodesniffer-composer-installer": false,
"ergebnis/composer-normalize": true,
"symfony/thanks": true
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: 0 additions & 1 deletion psalm.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?xml version="1.0"?>
<psalm
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
totallyTyped="true"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
errorBaseline="psalm-baseline.xml"
Expand Down
33 changes: 27 additions & 6 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Setono\SyliusGiftCardPlugin\Model\GiftCardConfigurationImageInterface;
use Setono\SyliusGiftCardPlugin\Model\GiftCardConfigurationInterface;
use Setono\SyliusGiftCardPlugin\Model\GiftCardInterface;
use Setono\SyliusGiftCardPlugin\Provider\PdfRenderingOptionsProviderInterface;
use Sylius\Bundle\ResourceBundle\Controller\ResourceController;
use Sylius\Bundle\ResourceBundle\Doctrine\ORM\EntityRepository;
use Sylius\Bundle\ResourceBundle\SyliusResourceBundle;
Expand All @@ -34,9 +35,31 @@ public function getConfigTreeBuilder(): TreeBuilder

$rootNode = $treeBuilder->getRootNode();

/**
* @psalm-suppress MixedMethodCall,PossiblyNullReference,PossiblyUndefinedMethod
*/
$rootNode
->addDefaultsIfNotSet()
->children()
->arrayNode('pdf_rendering')
->addDefaultsIfNotSet()
->children()
->scalarNode('default_orientation')
->defaultValue(PdfRenderingOptionsProviderInterface::ORIENTATION_PORTRAIT)
->end()
->arrayNode('available_orientations')
loevgaard marked this conversation as resolved.
Show resolved Hide resolved
->scalarPrototype()->end()
->defaultValue(PdfRenderingOptionsProviderInterface::AVAILABLE_ORIENTATIONS)
->end()
->scalarNode('default_page_size')
->defaultValue(PdfRenderingOptionsProviderInterface::PAGE_SIZE_A4)
->end()
->arrayNode('available_page_sizes')
->scalarPrototype()->end()
->defaultValue(PdfRenderingOptionsProviderInterface::AVAILABLE_PAGE_SIZES)
->end()
->end()
->end()
->scalarNode('driver')->defaultValue(SyliusResourceBundle::DRIVER_DOCTRINE_ORM)->end()
->integerNode('code_length')
->defaultValue(20)
Expand Down Expand Up @@ -64,16 +87,11 @@ private function addResourcesSection(ArrayNodeDefinition $node): void
$this->addGiftCardConfigurationSection($resourcesNode);
$this->addGiftCardConfigurationImageSection($resourcesNode);
$this->addChannelConfigurationSection($resourcesNode);

$resourcesNode
->end()
->end()
->end()
;
}

private function addGiftCardSection(NodeBuilder $nodeBuilder): void
{
/** @psalm-suppress MixedMethodCall,PossiblyNullReference,PossiblyUndefinedMethod */
$nodeBuilder
->arrayNode('gift_card')
->addDefaultsIfNotSet()
Expand All @@ -93,6 +111,7 @@ private function addGiftCardSection(NodeBuilder $nodeBuilder): void

private function addGiftCardConfigurationSection(NodeBuilder $nodeBuilder): void
{
/** @psalm-suppress MixedMethodCall,PossiblyNullReference,PossiblyUndefinedMethod */
$nodeBuilder
->arrayNode('gift_card_configuration')
->addDefaultsIfNotSet()
Expand All @@ -112,6 +131,7 @@ private function addGiftCardConfigurationSection(NodeBuilder $nodeBuilder): void

private function addGiftCardConfigurationImageSection(NodeBuilder $nodeBuilder): void
{
/** @psalm-suppress MixedMethodCall,PossiblyNullReference,PossiblyUndefinedMethod */
$nodeBuilder
->arrayNode('gift_card_configuration_image')
->addDefaultsIfNotSet()
Expand All @@ -131,6 +151,7 @@ private function addGiftCardConfigurationImageSection(NodeBuilder $nodeBuilder):

private function addChannelConfigurationSection(NodeBuilder $nodeBuilder): void
{
/** @psalm-suppress MixedMethodCall,PossiblyNullReference,PossiblyUndefinedMethod */
$nodeBuilder
->arrayNode('gift_card_channel_configuration')
->addDefaultsIfNotSet()
Expand Down
34 changes: 32 additions & 2 deletions src/DependencyInjection/SetonoSyliusGiftCardExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,50 @@ 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: list<string>,
* default_page_size: string,
* available_page_sizes: list<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);

$loader->load('services.xml');

if ($container->hasParameter('kernel.bundles')) {
/** @var array $bundles */
/**
* @var array $bundles
*
* @psalm-suppress UndefinedDocblockClass
*/
$bundles = $container->getParameter('kernel.bundles');
if (array_key_exists('SetonoSyliusCatalogPromotionPlugin', $bundles)) {
$loader->load('services/conditional/catalog_promotion_rule.xml');
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;
}
}
40 changes: 40 additions & 0 deletions src/Form/Type/GiftCardConfigurationType.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,36 @@
use Sylius\Bundle\ResourceBundle\Form\Type\AbstractResourceType;
use Symfony\Component\Form\CallbackTransformer;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;

final class GiftCardConfigurationType extends AbstractResourceType
{
/** @var list<string> */
private array $availableOrientations;
loevgaard marked this conversation as resolved.
Show resolved Hide resolved

/** @var list<string> */
private array $availablePageSizes;

/**
* @param list<string> $availableOrientations
* @param list<string> $availablePageSizes
* @param list<string> $validationGroups
*/
public function __construct(
array $availableOrientations,
array $availablePageSizes,
string $dataClass,
array $validationGroups = []
) {
$this->availableOrientations = $availableOrientations;
$this->availablePageSizes = $availablePageSizes;

parent::__construct($dataClass, $validationGroups);
}

public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder->add('code', TextType::class, [
Expand Down Expand Up @@ -42,6 +66,22 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
$builder->add('defaultValidityPeriod', DatePeriodType::class, [
'label' => 'setono_sylius_gift_card.form.gift_card_configuration.default_validity_period',
]);
$builder->add('pageSize', ChoiceType::class, [
'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;
},
]);
$builder->add('orientation', ChoiceType::class, [
'choices' => $this->availableOrientations,
'label' => 'setono_sylius_gift_card.form.gift_card_configuration.orientation',
'choice_translation_domain' => false,
'choice_label' => function (string $value) {
return $value;
},
]);
$builder->get('defaultValidityPeriod')->addModelTransformer(
new CallbackTransformer(
function (?string $period): array {
Expand Down
7 changes: 7 additions & 0 deletions src/Generator/GiftCardCodeGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,22 @@

use function preg_replace;
use Setono\SyliusGiftCardPlugin\Repository\GiftCardRepositoryInterface;
use Webmozart\Assert\Assert;

final class GiftCardCodeGenerator implements GiftCardCodeGeneratorInterface
{
private GiftCardRepositoryInterface $giftCardRepository;

/** @var positive-int */
private int $codeLength;

/**
* @param positive-int $codeLength
*/
public function __construct(GiftCardRepositoryInterface $giftCardRepository, int $codeLength)
{
Assert::greaterThan($codeLength, 0);

$this->giftCardRepository = $giftCardRepository;
$this->codeLength = $codeLength;
}
Expand Down
Loading