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

Upping robustness #252

Merged
merged 2 commits into from
Oct 9, 2023
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
5 changes: 4 additions & 1 deletion .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,16 @@ jobs:
php-version: "${{ matrix.php-version }}"
tools: "composer-require-checker, composer-unused, flex"

- name: "Remove require-dev section in composer.json"
run: "composer config --unset require-dev"

- name: "Install composer dependencies"
uses: "ramsey/composer-install@v2"
env:
SYMFONY_REQUIRE: "${{ matrix.symfony }}"
with:
dependency-versions: "${{ matrix.dependencies }}"

- name: "Run maglnet/composer-require-checker"
run: "composer-require-checker check --config-file=$(pwd)/composer-require-checker.json"

Expand Down
9 changes: 4 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
"knplabs/knp-snappy": "^1.4",
"knplabs/knp-snappy-bundle": "^1.9",
"setono/doctrine-object-manager-trait": "^1.1",
"setono/symfony-main-request-trait": "^1.0",
"sylius/resource-bundle": "^1.8",
"symfony/config": "^5.4 || ^6.0",
"symfony/dependency-injection": "^5.4 || ^6.0",
Expand All @@ -42,13 +41,13 @@
"lexik/jwt-authentication-bundle": "^2.16",
"matthiasnoback/symfony-config-test": "^4.3",
"matthiasnoback/symfony-dependency-injection-test": "^4.3",
"php-http/message-factory": "^1.1",
"phpspec/phpspec": "^7.2",
"phpspec/prophecy-phpunit": "^2.0",
"phpunit/phpunit": "^9.5",
"phpunit/phpunit": "^9.6",
"psalm/plugin-phpunit": "^0.18",
"psalm/plugin-symfony": "^4.0",
"roave/security-advisories": "dev-latest",
"setono/code-quality-pack": "^2.3",
"psalm/plugin-symfony": "^5.0",
"setono/code-quality-pack": "^2.4",
"setono/sylius-behat-pack": "^0.2.2",
"setono/sylius-catalog-promotion-plugin": "^0.4",
"sylius/sylius": "~1.10.14",
Expand Down
9 changes: 9 additions & 0 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="5.15.0@5c774aca4746caf3d239d9c8cadb9f882ca29352">
<file src="src/Api/Controller/Action/UploadGiftCardConfigurationImageAction.php">
<DeprecatedInterface>
<code>LegacyIriConverterInterface|IriConverterInterface</code>
<code>LegacyIriConverterInterface|IriConverterInterface</code>
</DeprecatedInterface>
</file>
</files>
16 changes: 12 additions & 4 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
findUnusedPsalmSuppress="false"
findUnusedBaselineEntry="true"
findUnusedVariablesAndParams="false"
findUnusedCode="false"
errorLevel="1"
errorBaseline="psalm-baseline.xml"
>
<projectFiles>
<directory name="src"/>
Expand All @@ -16,10 +21,6 @@
</projectFiles>
<plugins>
<pluginClass class="Psalm\SymfonyPsalmPlugin\Plugin">
<!-- Symfony 4 container XML -->
<containerXml>tests/Application/var/cache/test/ApplicationTests_Setono_SyliusGiftCardPlugin_Application_KernelTestDebugContainer.xml</containerXml>

<!-- Symfony 5 container XML -->
<containerXml>tests/Application/var/cache/test/Tests_Setono_SyliusGiftCardPlugin_Application_KernelTestDebugContainer.xml</containerXml>
</pluginClass>
<pluginClass class="Psalm\PhpUnitPlugin\Plugin"/>
Expand All @@ -28,9 +29,16 @@
<forbiddenFunctions>
<function name="dd"/>
<function name="dump"/>
<function name="var_dump"/>
<function name="print_r"/>
</forbiddenFunctions>
<issueHandlers>
<PluginIssue name="QueryBuilderSetParameter" errorLevel="suppress"/>
<RedundantConditionGivenDocblockType errorLevel="suppress"/>
<MissingTemplateParam>
<errorLevel type="suppress">
<directory name="src/Form"/>
</errorLevel>
</MissingTemplateParam>
</issueHandlers>
</psalm>
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

namespace Setono\SyliusGiftCardPlugin\Api\Controller\Action;

use ApiPlatform\Core\Api\IriConverterInterface;
use ApiPlatform\Api\IriConverterInterface;
use ApiPlatform\Core\Api\IriConverterInterface as LegacyIriConverterInterface;
use Setono\SyliusGiftCardPlugin\Model\GiftCardConfigurationImageInterface;
use Setono\SyliusGiftCardPlugin\Model\GiftCardConfigurationInterface;
use Sylius\Component\Core\Uploader\ImageUploaderInterface;
use Sylius\Component\Resource\Factory\FactoryInterface;
use Sylius\Component\Resource\Model\ResourceInterface;
use Sylius\Component\Resource\Repository\RepositoryInterface;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\HttpFoundation\Request;
Expand All @@ -23,13 +23,17 @@ final class UploadGiftCardConfigurationImageAction

private ImageUploaderInterface $imageUploader;

private IriConverterInterface $iriConverter;
/** @var LegacyIriConverterInterface|IriConverterInterface */
private $iriConverter;

/**
* @param LegacyIriConverterInterface|IriConverterInterface $iriConverter
*/
public function __construct(
FactoryInterface $giftCardConfigurationImageFactory,
RepositoryInterface $giftCardConfigurationImageRepository,
ImageUploaderInterface $imageUploader,
IriConverterInterface $iriConverter
$iriConverter
) {
$this->giftCardConfigurationImageFactory = $giftCardConfigurationImageFactory;
$this->giftCardConfigurationImageRepository = $giftCardConfigurationImageRepository;
Expand All @@ -52,13 +56,10 @@ public function __invoke(Request $request): GiftCardConfigurationImageInterface

$image->setType($imageType);

/** @var string $ownerIri */
$ownerIri = $request->request->get('owner') ?? $request->query->get('type');
Assert::notEmpty($ownerIri);
Assert::stringNotEmpty($ownerIri);

/** @var ResourceInterface|GiftCardConfigurationInterface $owner */
$owner = $this->iriConverter->getItemFromIri($ownerIri);
Assert::isInstanceOf($owner, GiftCardConfigurationInterface::class);
$owner = $this->getOwner($ownerIri);

$oldImages = $owner->getImagesByType($imageType);
foreach ($oldImages as $oldImage) {
Expand All @@ -71,4 +72,17 @@ public function __invoke(Request $request): GiftCardConfigurationImageInterface

return $image;
}

private function getOwner(string $ownerIri): GiftCardConfigurationInterface
{
if ($this->iriConverter instanceof LegacyIriConverterInterface) {
$owner = $this->iriConverter->getItemFromIri($ownerIri);
} else {
$owner = $this->iriConverter->getResourceFromIri($ownerIri);
}

Assert::isInstanceOf($owner, GiftCardConfigurationInterface::class);

return $owner;
}
}
1 change: 0 additions & 1 deletion src/Controller/Action/DownloadGiftCardPdfAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ private function sendErrorResponse(Request $request, string $redirectUrl, string
{
$session = $request->getSession();

/** @psalm-suppress RedundantConditionGivenDocblockType */
if ($session instanceof Session) {
$flashBag = $session->getFlashBag();
$flashBag->add('error', $message);
Expand Down
18 changes: 5 additions & 13 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,8 @@
use Setono\SyliusGiftCardPlugin\Form\Type\GiftCardType;
use Setono\SyliusGiftCardPlugin\Model\GiftCard;
use Setono\SyliusGiftCardPlugin\Model\GiftCardChannelConfiguration;
use Setono\SyliusGiftCardPlugin\Model\GiftCardChannelConfigurationInterface;
use Setono\SyliusGiftCardPlugin\Model\GiftCardConfiguration;
use Setono\SyliusGiftCardPlugin\Model\GiftCardConfigurationImage;
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;
Expand All @@ -37,7 +33,7 @@ public function getConfigTreeBuilder(): TreeBuilder
$rootNode = $treeBuilder->getRootNode();

/**
* @psalm-suppress MixedMethodCall,PossiblyNullReference,PossiblyUndefinedMethod
* @psalm-suppress MixedMethodCall,PossiblyNullReference,PossiblyUndefinedMethod,UndefinedInterfaceMethod
*/
$rootNode
->addDefaultsIfNotSet()
Expand Down Expand Up @@ -95,7 +91,7 @@ private function addResourcesSection(ArrayNodeDefinition $node): void

private function addGiftCardSection(NodeBuilder $nodeBuilder): void
{
/** @psalm-suppress MixedMethodCall,PossiblyNullReference,PossiblyUndefinedMethod */
/** @psalm-suppress MixedMethodCall,PossiblyNullReference,UndefinedInterfaceMethod */
$nodeBuilder
->arrayNode('gift_card')
->addDefaultsIfNotSet()
Expand All @@ -105,7 +101,6 @@ private function addGiftCardSection(NodeBuilder $nodeBuilder): void
->addDefaultsIfNotSet()
->children()
->scalarNode('model')->defaultValue(GiftCard::class)->cannotBeEmpty()->end()
->scalarNode('interface')->defaultValue(GiftCardInterface::class)->cannotBeEmpty()->end()
->scalarNode('controller')->defaultValue(ResourceController::class)->cannotBeEmpty()->end()
->scalarNode('repository')->defaultValue(GiftCardRepository::class)->cannotBeEmpty()->end()
->scalarNode('form')->defaultValue(GiftCardType::class)->end()
Expand All @@ -115,7 +110,7 @@ private function addGiftCardSection(NodeBuilder $nodeBuilder): void

private function addGiftCardConfigurationSection(NodeBuilder $nodeBuilder): void
{
/** @psalm-suppress MixedMethodCall,PossiblyNullReference,PossiblyUndefinedMethod */
/** @psalm-suppress MixedMethodCall,PossiblyNullReference,UndefinedInterfaceMethod */
$nodeBuilder
->arrayNode('gift_card_configuration')
->addDefaultsIfNotSet()
Expand All @@ -125,7 +120,6 @@ private function addGiftCardConfigurationSection(NodeBuilder $nodeBuilder): void
->addDefaultsIfNotSet()
->children()
->scalarNode('model')->defaultValue(GiftCardConfiguration::class)->cannotBeEmpty()->end()
->scalarNode('interface')->defaultValue(GiftCardConfigurationInterface::class)->cannotBeEmpty()->end()
->scalarNode('controller')->defaultValue(ResourceController::class)->cannotBeEmpty()->end()
->scalarNode('repository')->defaultValue(GiftCardConfigurationRepository::class)->cannotBeEmpty()->end()
->scalarNode('form')->defaultValue(GiftCardConfigurationType::class)->end()
Expand All @@ -135,7 +129,7 @@ private function addGiftCardConfigurationSection(NodeBuilder $nodeBuilder): void

private function addGiftCardConfigurationImageSection(NodeBuilder $nodeBuilder): void
{
/** @psalm-suppress MixedMethodCall,PossiblyNullReference,PossiblyUndefinedMethod */
/** @psalm-suppress MixedMethodCall,PossiblyNullReference,UndefinedInterfaceMethod */
$nodeBuilder
->arrayNode('gift_card_configuration_image')
->addDefaultsIfNotSet()
Expand All @@ -145,7 +139,6 @@ private function addGiftCardConfigurationImageSection(NodeBuilder $nodeBuilder):
->addDefaultsIfNotSet()
->children()
->scalarNode('model')->defaultValue(GiftCardConfigurationImage::class)->cannotBeEmpty()->end()
->scalarNode('interface')->defaultValue(GiftCardConfigurationImageInterface::class)->cannotBeEmpty()->end()
->scalarNode('controller')->defaultValue(ResourceController::class)->cannotBeEmpty()->end()
->scalarNode('repository')->defaultValue(EntityRepository::class)->cannotBeEmpty()->end()
->scalarNode('form')->defaultValue(GiftCardConfigurationImageType::class)->end()
Expand All @@ -155,7 +148,7 @@ private function addGiftCardConfigurationImageSection(NodeBuilder $nodeBuilder):

private function addChannelConfigurationSection(NodeBuilder $nodeBuilder): void
{
/** @psalm-suppress MixedMethodCall,PossiblyNullReference,PossiblyUndefinedMethod */
/** @psalm-suppress MixedMethodCall,PossiblyNullReference,UndefinedInterfaceMethod */
$nodeBuilder
->arrayNode('gift_card_channel_configuration')
->addDefaultsIfNotSet()
Expand All @@ -165,7 +158,6 @@ private function addChannelConfigurationSection(NodeBuilder $nodeBuilder): void
->addDefaultsIfNotSet()
->children()
->scalarNode('model')->defaultValue(GiftCardChannelConfiguration::class)->cannotBeEmpty()->end()
->scalarNode('interface')->defaultValue(GiftCardChannelConfigurationInterface::class)->cannotBeEmpty()->end()
->scalarNode('controller')->defaultValue(ResourceController::class)->cannotBeEmpty()->end()
->scalarNode('repository')->defaultValue(EntityRepository::class)->cannotBeEmpty()->end()
->scalarNode('form')->defaultValue(GiftCardChannelConfigurationType::class)->end()
Expand Down
8 changes: 3 additions & 5 deletions src/DependencyInjection/SetonoSyliusGiftCardExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
use Webmozart\Assert\Assert;

final class SetonoSyliusGiftCardExtension extends AbstractResourceExtension
{
Expand Down Expand Up @@ -65,12 +66,9 @@ public function load(array $configs, ContainerBuilder $container): void
$loader->load('services.xml');

if ($container->hasParameter('kernel.bundles')) {
/**
* @var array $bundles
*
* @psalm-suppress UndefinedDocblockClass
*/
$bundles = $container->getParameter('kernel.bundles');
Assert::isArray($bundles);

if (array_key_exists('SetonoSyliusCatalogPromotionPlugin', $bundles)) {
$loader->load('services/conditional/catalog_promotion_rule.xml');
}
Expand Down
3 changes: 3 additions & 0 deletions src/Form/Type/AddGiftCardToOrderType.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

/**
* @extends AbstractType<AddGiftCardToOrderCommand>
*/
final class AddGiftCardToOrderType extends AbstractType
{
private DataTransformerInterface $giftCardToCodeDataTransformer;
Expand Down
3 changes: 3 additions & 0 deletions src/Model/GiftCardBalanceCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
use OutOfBoundsException;
use Webmozart\Assert\Assert;

/**
* @implements Iterator<array-key, GiftCardBalance>
*/
final class GiftCardBalanceCollection implements Countable, Iterator
{
/** @var GiftCardBalance[] */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Setono\SyliusGiftCardPlugin\Serializer\Normalizer;

use ArrayObject;
use Setono\MainRequestTrait\MainRequestTrait;
use Setono\SyliusGiftCardPlugin\Exception\UnexpectedTypeException;
use Setono\SyliusGiftCardPlugin\Model\GiftCardConfigurationInterface;
use Symfony\Component\HttpFoundation\RequestStack;
Expand All @@ -15,8 +14,6 @@

final class GiftCardConfigurationNormalizer implements ContextAwareNormalizerInterface
{
use MainRequestTrait;

private ObjectNormalizer $objectNormalizer;

private RequestStack $requestStack;
Expand Down Expand Up @@ -50,7 +47,7 @@ public function normalize($object, $format = null, array $context = [])

$data['image'] = '';

$request = $this->getMainRequestFromRequestStack($this->requestStack);
$request = $this->requestStack->getMainRequest();
if (null !== $request) {
$data['image'] = $request->getSchemeAndHttpHost() . '/bundles/setonosyliusgiftcardplugin/setono-logo.png';
}
Expand Down
8 changes: 0 additions & 8 deletions tests/Unit/DependencyInjection/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,8 @@
use Setono\SyliusGiftCardPlugin\Form\Type\GiftCardType;
use Setono\SyliusGiftCardPlugin\Model\GiftCard;
use Setono\SyliusGiftCardPlugin\Model\GiftCardChannelConfiguration;
use Setono\SyliusGiftCardPlugin\Model\GiftCardChannelConfigurationInterface;
use Setono\SyliusGiftCardPlugin\Model\GiftCardConfiguration;
use Setono\SyliusGiftCardPlugin\Model\GiftCardConfigurationImage;
use Setono\SyliusGiftCardPlugin\Model\GiftCardConfigurationImageInterface;
use Setono\SyliusGiftCardPlugin\Model\GiftCardConfigurationInterface;
use Setono\SyliusGiftCardPlugin\Model\GiftCardInterface;
use Sylius\Bundle\ResourceBundle\Controller\ResourceController;
use Sylius\Bundle\ResourceBundle\Doctrine\ORM\EntityRepository;
use Sylius\Bundle\ResourceBundle\SyliusResourceBundle;
Expand Down Expand Up @@ -94,7 +90,6 @@ public function processed_configuration_for_array_node_1(): void
'gift_card' => [
'classes' => [
'model' => GiftCard::class,
'interface' => GiftCardInterface::class,
'controller' => ResourceController::class,
'repository' => GiftCardRepository::class,
'form' => GiftCardType::class,
Expand All @@ -104,7 +99,6 @@ public function processed_configuration_for_array_node_1(): void
'gift_card_configuration' => [
'classes' => [
'model' => GiftCardConfiguration::class,
'interface' => GiftCardConfigurationInterface::class,
'controller' => ResourceController::class,
'repository' => GiftCardConfigurationRepository::class,
'form' => GiftCardConfigurationType::class,
Expand All @@ -114,7 +108,6 @@ public function processed_configuration_for_array_node_1(): void
'gift_card_configuration_image' => [
'classes' => [
'model' => GiftCardConfigurationImage::class,
'interface' => GiftCardConfigurationImageInterface::class,
'controller' => ResourceController::class,
'repository' => EntityRepository::class,
'form' => GiftCardConfigurationImageType::class,
Expand All @@ -124,7 +117,6 @@ public function processed_configuration_for_array_node_1(): void
'gift_card_channel_configuration' => [
'classes' => [
'model' => GiftCardChannelConfiguration::class,
'interface' => GiftCardChannelConfigurationInterface::class,
'controller' => ResourceController::class,
'repository' => EntityRepository::class,
'form' => GiftCardChannelConfigurationType::class,
Expand Down
Loading