Skip to content

Commit

Permalink
Make GiftCardConfigurationProvider always return GiftCardConfiguration
Browse files Browse the repository at this point in the history
  • Loading branch information
loevgaard committed May 6, 2022
1 parent 5ae31ba commit f0cd8b8
Show file tree
Hide file tree
Showing 18 changed files with 274 additions and 188 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"knplabs/knp-menu": "^3.2",
"knplabs/knp-snappy": "^1.2",
"knplabs/knp-snappy-bundle": "^1.7",
"setono/doctrine-object-manager-trait": "^1.1",
"sylius/resource-bundle": "^1.6",
"symfony/config": "^4.4 || ^5.0",
"symfony/dependency-injection": "^4.4 || ^5.0",
Expand Down
66 changes: 0 additions & 66 deletions spec/Provider/GiftCardChannelConfigurationProviderSpec.php

This file was deleted.

71 changes: 71 additions & 0 deletions spec/Provider/GiftCardConfigurationProviderSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

declare(strict_types=1);

namespace spec\Setono\SyliusGiftCardPlugin\Provider;

use Doctrine\ORM\EntityManagerInterface;
use Doctrine\Persistence\ManagerRegistry;
use PhpSpec\ObjectBehavior;
use Setono\SyliusGiftCardPlugin\Factory\GiftCardConfigurationFactoryInterface;
use Setono\SyliusGiftCardPlugin\Model\GiftCardChannelConfiguration;
use Setono\SyliusGiftCardPlugin\Model\GiftCardConfiguration;
use Setono\SyliusGiftCardPlugin\Provider\GiftCardConfigurationProvider;
use Setono\SyliusGiftCardPlugin\Provider\GiftCardConfigurationProviderInterface;
use Setono\SyliusGiftCardPlugin\Repository\GiftCardConfigurationRepositoryInterface;
use Sylius\Component\Channel\Model\ChannelInterface;
use Sylius\Component\Locale\Context\LocaleContextInterface;
use Sylius\Component\Locale\Model\LocaleInterface;
use Sylius\Component\Resource\Repository\RepositoryInterface;

final class GiftCardConfigurationProviderSpec extends ObjectBehavior
{
public function let(
GiftCardConfigurationRepositoryInterface $giftCardConfigurationRepository,
GiftCardConfigurationFactoryInterface $giftCardConfigurationFactory,
LocaleContextInterface $localeContext,
RepositoryInterface $localeRepository,
ManagerRegistry $managerRegistry
) {
$this->beConstructedWith($giftCardConfigurationRepository, $giftCardConfigurationFactory, $localeContext, $localeRepository, $managerRegistry);
}

public function it_is_initializable(): void
{
$this->shouldBeAnInstanceOf(GiftCardConfigurationProvider::class);
}

public function it_implements_gift_card_configuration_provider_interface(): void
{
$this->shouldImplement(GiftCardConfigurationProviderInterface::class);
}

public function it_provides_configuration_from_channel_and_locale(
GiftCardConfigurationRepositoryInterface $giftCardConfigurationRepository,
ChannelInterface $channel,
LocaleInterface $locale
): void {
$configuration = new GiftCardConfiguration();
$giftCardConfigurationRepository->findOneByChannelAndLocale($channel, $locale)->willReturn($configuration);

$this->getConfiguration($channel, $locale)->shouldReturn($configuration);
}

public function it_provides_default_configuration_if_none_found(
GiftCardConfigurationRepositoryInterface $giftCardConfigurationRepository,
GiftCardConfigurationFactoryInterface $giftCardConfigurationFactory,
ChannelInterface $channel,
LocaleInterface $locale,
ManagerRegistry $managerRegistry,
EntityManagerInterface $entityManager
) {
$managerRegistry->getManagerForClass(GiftCardConfiguration::class)->willReturn($entityManager);
$giftCardConfigurationRepository->findOneByChannelAndLocale($channel, $locale)->willReturn(null);

$configuration = new GiftCardConfiguration();
$configuration->addChannelConfiguration(new GiftCardChannelConfiguration());
$giftCardConfigurationRepository->findDefault()->willReturn($configuration);

$this->getConfiguration($channel, $locale)->shouldReturn($configuration);
}
}
6 changes: 3 additions & 3 deletions src/Api/Controller/Action/DownloadGiftCardPdfAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@
use Setono\SyliusGiftCardPlugin\Generator\GiftCardPdfGeneratorInterface;
use Setono\SyliusGiftCardPlugin\Model\GiftCardConfigurationInterface;
use Setono\SyliusGiftCardPlugin\Model\GiftCardInterface;
use Setono\SyliusGiftCardPlugin\Provider\GiftCardChannelConfigurationProviderInterface;
use Setono\SyliusGiftCardPlugin\Provider\GiftCardConfigurationProviderInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

final class DownloadGiftCardPdfAction
{
private GiftCardChannelConfigurationProviderInterface $configurationProvider;
private GiftCardConfigurationProviderInterface $configurationProvider;

private GiftCardPdfGeneratorInterface $giftCardPdfGenerator;

public function __construct(
GiftCardChannelConfigurationProviderInterface $configurationProvider,
GiftCardConfigurationProviderInterface $configurationProvider,
GiftCardPdfGeneratorInterface $giftCardPdfGenerator
) {
$this->configurationProvider = $configurationProvider;
Expand Down
6 changes: 3 additions & 3 deletions src/Controller/Action/DownloadGiftCardPdfAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use Setono\SyliusGiftCardPlugin\Generator\GiftCardPdfGeneratorInterface;
use Setono\SyliusGiftCardPlugin\Model\GiftCardConfigurationInterface;
use Setono\SyliusGiftCardPlugin\Model\GiftCardInterface;
use Setono\SyliusGiftCardPlugin\Provider\GiftCardChannelConfigurationProviderInterface;
use Setono\SyliusGiftCardPlugin\Provider\GiftCardConfigurationProviderInterface;
use Setono\SyliusGiftCardPlugin\Repository\GiftCardRepositoryInterface;
use Setono\SyliusGiftCardPlugin\Security\GiftCardVoter;
use Symfony\Component\HttpFoundation\RedirectResponse;
Expand All @@ -26,7 +26,7 @@ final class DownloadGiftCardPdfAction

private FlashBagInterface $flashBag;

private GiftCardChannelConfigurationProviderInterface $configurationProvider;
private GiftCardConfigurationProviderInterface $configurationProvider;

private GiftCardPdfGeneratorInterface $giftCardPdfGenerator;

Expand All @@ -36,7 +36,7 @@ public function __construct(
GiftCardRepositoryInterface $giftCardRepository,
AuthorizationCheckerInterface $authChecker,
FlashBagInterface $flashBag,
GiftCardChannelConfigurationProviderInterface $configurationProvider,
GiftCardConfigurationProviderInterface $configurationProvider,
GiftCardPdfGeneratorInterface $giftCardPdfGenerator,
UrlGeneratorInterface $urlGenerator
) {
Expand Down
3 changes: 2 additions & 1 deletion src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Setono\SyliusGiftCardPlugin\DependencyInjection;

use Setono\SyliusGiftCardPlugin\Doctrine\ORM\GiftCardConfigurationRepository;
use Setono\SyliusGiftCardPlugin\Doctrine\ORM\GiftCardRepository;
use Setono\SyliusGiftCardPlugin\Form\Type\GiftCardChannelConfigurationType;
use Setono\SyliusGiftCardPlugin\Form\Type\GiftCardConfigurationImageType;
Expand Down Expand Up @@ -123,7 +124,7 @@ private function addGiftCardConfigurationSection(NodeBuilder $nodeBuilder): void
->scalarNode('model')->defaultValue(GiftCardConfiguration::class)->cannotBeEmpty()->end()
->scalarNode('interface')->defaultValue(GiftCardConfigurationInterface::class)->cannotBeEmpty()->end()
->scalarNode('controller')->defaultValue(ResourceController::class)->cannotBeEmpty()->end()
->scalarNode('repository')->defaultValue(EntityRepository::class)->cannotBeEmpty()->end()
->scalarNode('repository')->defaultValue(GiftCardConfigurationRepository::class)->cannotBeEmpty()->end()
->scalarNode('form')->defaultValue(GiftCardConfigurationType::class)->end()
->scalarNode('factory')->defaultValue(Factory::class)->end()
;
Expand Down
41 changes: 41 additions & 0 deletions src/Doctrine/ORM/GiftCardConfigurationRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

namespace Setono\SyliusGiftCardPlugin\Doctrine\ORM;

use Setono\SyliusGiftCardPlugin\Model\GiftCardConfigurationInterface;
use Setono\SyliusGiftCardPlugin\Repository\GiftCardConfigurationRepositoryInterface;
use Sylius\Bundle\ResourceBundle\Doctrine\ORM\EntityRepository;
use Sylius\Component\Channel\Model\ChannelInterface;
use Sylius\Component\Locale\Model\LocaleInterface;
use Webmozart\Assert\Assert;

class GiftCardConfigurationRepository extends EntityRepository implements GiftCardConfigurationRepositoryInterface
{
public function findOneByChannelAndLocale(ChannelInterface $channel, LocaleInterface $locale): ?GiftCardConfigurationInterface
{
$obj = $this->createQueryBuilder('o')
->join('o.channelConfigurations', 'c')
->andWhere('c.channel = :channel')
->andWhere('c.locale = :locale')
->setParameters([
'channel' => $channel,
'locale' => $locale,
])
->getQuery()
->getOneOrNullResult()
;

Assert::nullOrIsInstanceOf($obj, GiftCardConfigurationInterface::class);

return $obj;
}

public function findDefault(): ?GiftCardConfigurationInterface
{
return $this->findOneBy([
'default' => true,
]);
}
}
30 changes: 14 additions & 16 deletions src/Factory/GiftCardFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use Setono\SyliusGiftCardPlugin\Generator\GiftCardCodeGeneratorInterface;
use Setono\SyliusGiftCardPlugin\Model\GiftCardInterface;
use Setono\SyliusGiftCardPlugin\Model\OrderItemUnitInterface;
use Setono\SyliusGiftCardPlugin\Provider\GiftCardChannelConfigurationProviderInterface;
use Setono\SyliusGiftCardPlugin\Provider\GiftCardConfigurationProviderInterface;
use Sylius\Bundle\ShippingBundle\Provider\DateTimeProvider;
use Sylius\Component\Core\Model\ChannelInterface;
use Sylius\Component\Core\Model\CustomerInterface;
Expand All @@ -24,7 +24,7 @@ final class GiftCardFactory implements GiftCardFactoryInterface

private GiftCardCodeGeneratorInterface $giftCardCodeGenerator;

private GiftCardChannelConfigurationProviderInterface $giftCardChannelConfigurationProvider;
private GiftCardConfigurationProviderInterface $giftCardConfigurationProvider;

private DateTimeProvider $dateTimeProvider;

Expand All @@ -33,13 +33,13 @@ final class GiftCardFactory implements GiftCardFactoryInterface
public function __construct(
FactoryInterface $decoratedFactory,
GiftCardCodeGeneratorInterface $giftCardCodeGenerator,
GiftCardChannelConfigurationProviderInterface $giftCardChannelConfigurationProvider,
GiftCardConfigurationProviderInterface $giftCardConfigurationProvider,
DateTimeProvider $dateTimeProvider,
CurrencyContextInterface $currencyContext
) {
$this->decoratedFactory = $decoratedFactory;
$this->giftCardCodeGenerator = $giftCardCodeGenerator;
$this->giftCardChannelConfigurationProvider = $giftCardChannelConfigurationProvider;
$this->giftCardConfigurationProvider = $giftCardConfigurationProvider;
$this->dateTimeProvider = $dateTimeProvider;
$this->currencyContext = $currencyContext;
}
Expand All @@ -58,18 +58,16 @@ public function createForChannel(ChannelInterface $channel): GiftCardInterface
$giftCard = $this->createNew();
$giftCard->setChannel($channel);

$channelConfiguration = $this->giftCardChannelConfigurationProvider->getConfigurationForGiftCard($giftCard);
if (null !== $channelConfiguration) {
$validityPeriod = $channelConfiguration->getDefaultValidityPeriod();
if (null !== $validityPeriod) {
$today = $this->dateTimeProvider->today();
// Since the interface is types to DateTimeInterface, the modify method does not exist
// whereas it does in DateTime and DateTimeImmutable
Assert::isInstanceOf($today, DateTimeImmutable::class);
/** @var DateTimeInterface $today */
$today = $today->modify('+' . $validityPeriod);
$giftCard->setExpiresAt($today);
}
$channelConfiguration = $this->giftCardConfigurationProvider->getConfigurationForGiftCard($giftCard);
$validityPeriod = $channelConfiguration->getDefaultValidityPeriod();
if (null !== $validityPeriod) {
$today = $this->dateTimeProvider->today();
// Since the interface is types to DateTimeInterface, the modify method does not exist
// whereas it does in DateTime and DateTimeImmutable
Assert::isInstanceOf($today, DateTimeImmutable::class);
/** @var DateTimeInterface $today */
$today = $today->modify('+' . $validityPeriod);
$giftCard->setExpiresAt($today);
}

return $giftCard;
Expand Down
8 changes: 4 additions & 4 deletions src/Model/GiftCardChannelConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@

namespace Setono\SyliusGiftCardPlugin\Model;

use Sylius\Component\Channel\Model\ChannelInterface as SyliusChannelInterface;
use Sylius\Component\Channel\Model\ChannelInterface;
use Sylius\Component\Locale\Model\LocaleInterface;
use Webmozart\Assert\Assert;

class GiftCardChannelConfiguration implements GiftCardChannelConfigurationInterface
{
protected ?int $id = null;

protected ?SyliusChannelInterface $channel = null;
protected ?ChannelInterface $channel = null;

protected ?LocaleInterface $locale = null;

Expand All @@ -23,12 +23,12 @@ public function getId(): ?int
return $this->id;
}

public function getChannel(): ?SyliusChannelInterface
public function getChannel(): ?ChannelInterface
{
return $this->channel;
}

public function setChannel(?SyliusChannelInterface $channel): void
public function setChannel(?ChannelInterface $channel): void
{
Assert::notNull($channel);
$this->channel = $channel;
Expand Down
Loading

0 comments on commit f0cd8b8

Please sign in to comment.