From 8bf466dc010cc5330025213a0239bbf87118c9cd Mon Sep 17 00:00:00 2001 From: Kevin Kaniaburka Date: Mon, 4 Nov 2024 09:13:58 +0100 Subject: [PATCH] Add channel support for Blik aliases --- config/doctrine/BlikAlias.orm.xml | 4 +++ config/services/resolver.php | 1 + migrations/Version20241104080950.php | 30 +++++++++++++++++++ src/Entity/BlikAlias.php | 13 ++++++++ src/Entity/BlikAliasInterface.php | 5 ++++ src/Factory/BlikAliasFactory.php | 8 +++-- src/Factory/BlikAliasFactoryInterface.php | 3 +- src/Repository/BlikAliasRepository.php | 9 ++++-- .../BlikAliasRepositoryInterface.php | 6 +++- src/Resolver/BlikAliasResolver.php | 10 +++++-- .../DataFixtures/Factory/BlikAliasFactory.php | 14 ++++++--- tests/Api/DataFixtures/shop/blik_alias.yml | 1 + .../shop/blik_ambiguous_alias.yml | 9 +++++- .../DataFixtures/shop/blik_expired_alias.yml | 9 +++++- .../shop/blik_not_registered_alias.yml | 8 ++++- tests/Unit/Resolver/BlikAliasResolverTest.php | 16 ++++++++-- 16 files changed, 128 insertions(+), 18 deletions(-) create mode 100644 migrations/Version20241104080950.php diff --git a/config/doctrine/BlikAlias.orm.xml b/config/doctrine/BlikAlias.orm.xml index 54b9560a..1047e452 100644 --- a/config/doctrine/BlikAlias.orm.xml +++ b/config/doctrine/BlikAlias.orm.xml @@ -21,5 +21,9 @@ + + + + diff --git a/config/services/resolver.php b/config/services/resolver.php index 90a55d42..d68ea325 100644 --- a/config/services/resolver.php +++ b/config/services/resolver.php @@ -14,6 +14,7 @@ ->args([ service('commerce_weavers_sylius_tpay.repository.blik_alias'), service('commerce_weavers_sylius_tpay.factory.blik_alias'), + service('sylius.context.channel'), ]) ->alias(BlikAliasResolverInterface::class, 'commerce_weavers_sylius_tpay.resolver.blik_alias') ; diff --git a/migrations/Version20241104080950.php b/migrations/Version20241104080950.php new file mode 100644 index 00000000..0cdeea90 --- /dev/null +++ b/migrations/Version20241104080950.php @@ -0,0 +1,30 @@ +addSql('ALTER TABLE cw_sylius_tpay_blik_alias ADD channel_id INT NOT NULL'); + $this->addSql('ALTER TABLE cw_sylius_tpay_blik_alias ADD CONSTRAINT FK_72E078BE72F5A1AA FOREIGN KEY (channel_id) REFERENCES sylius_channel (id) ON DELETE CASCADE'); + $this->addSql('CREATE INDEX IDX_72E078BE72F5A1AA ON cw_sylius_tpay_blik_alias (channel_id)'); + } + + public function down(Schema $schema): void + { + $this->addSql('ALTER TABLE cw_sylius_tpay_blik_alias DROP FOREIGN KEY FK_72E078BE72F5A1AA'); + $this->addSql('DROP INDEX IDX_72E078BE72F5A1AA ON cw_sylius_tpay_blik_alias'); + $this->addSql('ALTER TABLE cw_sylius_tpay_blik_alias DROP channel_id'); + } +} diff --git a/src/Entity/BlikAlias.php b/src/Entity/BlikAlias.php index 5b54c1ba..9d9ab9c9 100644 --- a/src/Entity/BlikAlias.php +++ b/src/Entity/BlikAlias.php @@ -4,6 +4,7 @@ namespace CommerceWeavers\SyliusTpayPlugin\Entity; +use Sylius\Component\Core\Model\ChannelInterface; use Sylius\Component\Core\Model\CustomerInterface; use Symfony\Component\Uid\Uuid; @@ -19,6 +20,8 @@ class BlikAlias implements BlikAliasInterface private ?CustomerInterface $customer = null; + private ?ChannelInterface $channel = null; + public function getId(): ?int { return $this->id; @@ -71,4 +74,14 @@ public function setCustomer(?CustomerInterface $customer): void { $this->customer = $customer; } + + public function getChannel(): ?ChannelInterface + { + return $this->channel; + } + + public function setChannel(?ChannelInterface $channel): void + { + $this->channel = $channel; + } } diff --git a/src/Entity/BlikAliasInterface.php b/src/Entity/BlikAliasInterface.php index 4bad7218..15e99fda 100644 --- a/src/Entity/BlikAliasInterface.php +++ b/src/Entity/BlikAliasInterface.php @@ -4,6 +4,7 @@ namespace CommerceWeavers\SyliusTpayPlugin\Entity; +use Sylius\Component\Core\Model\ChannelInterface; use Sylius\Component\Core\Model\CustomerInterface; use Sylius\Resource\Model\ResourceInterface; @@ -26,4 +27,8 @@ public function unregister(): void; public function getCustomer(): ?CustomerInterface; public function setCustomer(?CustomerInterface $customer): void; + + public function getChannel(): ?ChannelInterface; + + public function setChannel(?ChannelInterface $channel): void; } diff --git a/src/Factory/BlikAliasFactory.php b/src/Factory/BlikAliasFactory.php index 1b7e22e4..5e13cbd9 100644 --- a/src/Factory/BlikAliasFactory.php +++ b/src/Factory/BlikAliasFactory.php @@ -5,6 +5,7 @@ namespace CommerceWeavers\SyliusTpayPlugin\Factory; use CommerceWeavers\SyliusTpayPlugin\Entity\BlikAliasInterface; +use Sylius\Component\Core\Model\ChannelInterface; use Sylius\Component\Core\Model\CustomerInterface; final class BlikAliasFactory implements BlikAliasFactoryInterface @@ -27,10 +28,13 @@ public function createNew(): BlikAliasInterface return new $this->className(); } - public function createForCustomer(CustomerInterface $customer): BlikAliasInterface - { + public function createForCustomerAndChannel( + CustomerInterface $customer, + ChannelInterface $channel, + ): BlikAliasInterface { $blikAlias = $this->createNew(); $blikAlias->setCustomer($customer); + $blikAlias->setChannel($channel); return $blikAlias; } diff --git a/src/Factory/BlikAliasFactoryInterface.php b/src/Factory/BlikAliasFactoryInterface.php index 8f2a707e..f2dc280a 100644 --- a/src/Factory/BlikAliasFactoryInterface.php +++ b/src/Factory/BlikAliasFactoryInterface.php @@ -5,10 +5,11 @@ namespace CommerceWeavers\SyliusTpayPlugin\Factory; use CommerceWeavers\SyliusTpayPlugin\Entity\BlikAliasInterface; +use Sylius\Component\Core\Model\ChannelInterface; use Sylius\Component\Core\Model\CustomerInterface; use Sylius\Resource\Factory\FactoryInterface; interface BlikAliasFactoryInterface extends FactoryInterface { - public function createForCustomer(CustomerInterface $customer): BlikAliasInterface; + public function createForCustomerAndChannel(CustomerInterface $customer, ChannelInterface $channel): BlikAliasInterface; } diff --git a/src/Repository/BlikAliasRepository.php b/src/Repository/BlikAliasRepository.php index 8db14dbe..1e572a19 100644 --- a/src/Repository/BlikAliasRepository.php +++ b/src/Repository/BlikAliasRepository.php @@ -6,6 +6,7 @@ use CommerceWeavers\SyliusTpayPlugin\Entity\BlikAliasInterface; use Sylius\Bundle\ResourceBundle\Doctrine\ORM\EntityRepository; +use Sylius\Component\Core\Model\ChannelInterface; use Sylius\Component\Core\Model\CustomerInterface; final class BlikAliasRepository extends EntityRepository implements BlikAliasRepositoryInterface @@ -21,12 +22,16 @@ public function findOneByValue(string $value): ?BlikAliasInterface ; } - public function findOneByCustomer(CustomerInterface $customer): ?BlikAliasInterface - { + public function findOneByCustomerAndChannel( + CustomerInterface $customer, + ChannelInterface $channel, + ): ?BlikAliasInterface { /** @phpstan-var BlikAliasInterface|null */ return $this->createQueryBuilder('o') ->andWhere('o.customer = :customer') + ->andWhere('o.channel = :channel') ->setParameter('customer', $customer) + ->setParameter('channel', $channel) ->getQuery() ->getOneOrNullResult() ; diff --git a/src/Repository/BlikAliasRepositoryInterface.php b/src/Repository/BlikAliasRepositoryInterface.php index 48df6f96..474ee0ac 100644 --- a/src/Repository/BlikAliasRepositoryInterface.php +++ b/src/Repository/BlikAliasRepositoryInterface.php @@ -5,6 +5,7 @@ namespace CommerceWeavers\SyliusTpayPlugin\Repository; use CommerceWeavers\SyliusTpayPlugin\Entity\BlikAliasInterface; +use Sylius\Component\Core\Model\ChannelInterface; use Sylius\Component\Core\Model\CustomerInterface; use Sylius\Resource\Doctrine\Persistence\RepositoryInterface; @@ -12,5 +13,8 @@ interface BlikAliasRepositoryInterface extends RepositoryInterface { public function findOneByValue(string $value): ?BlikAliasInterface; - public function findOneByCustomer(CustomerInterface $customer): ?BlikAliasInterface; + public function findOneByCustomerAndChannel( + CustomerInterface $customer, + ChannelInterface $channel, + ): ?BlikAliasInterface; } diff --git a/src/Resolver/BlikAliasResolver.php b/src/Resolver/BlikAliasResolver.php index 4a4c6ceb..9415fa5d 100644 --- a/src/Resolver/BlikAliasResolver.php +++ b/src/Resolver/BlikAliasResolver.php @@ -7,6 +7,8 @@ use CommerceWeavers\SyliusTpayPlugin\Entity\BlikAliasInterface; use CommerceWeavers\SyliusTpayPlugin\Factory\BlikAliasFactoryInterface; use CommerceWeavers\SyliusTpayPlugin\Repository\BlikAliasRepositoryInterface; +use Sylius\Component\Channel\Context\ChannelContextInterface; +use Sylius\Component\Core\Model\ChannelInterface; use Sylius\Component\Core\Model\CustomerInterface; final class BlikAliasResolver implements BlikAliasResolverInterface @@ -14,12 +16,16 @@ final class BlikAliasResolver implements BlikAliasResolverInterface public function __construct( private readonly BlikAliasRepositoryInterface $blikAliasRepository, private readonly BlikAliasFactoryInterface $blikAliasFactory, + private readonly ChannelContextInterface $channelContext, ) { } public function resolve(CustomerInterface $customer): BlikAliasInterface { - return $this->blikAliasRepository->findOneByCustomer($customer) - ?? $this->blikAliasFactory->createForCustomer($customer); + /** @var ChannelInterface $channel */ + $channel = $this->channelContext->getChannel(); + + return $this->blikAliasRepository->findOneByCustomerAndChannel($customer, $channel) + ?? $this->blikAliasFactory->createForCustomerAndChannel($customer, $channel); } } diff --git a/tests/Api/DataFixtures/Factory/BlikAliasFactory.php b/tests/Api/DataFixtures/Factory/BlikAliasFactory.php index 1f2399b9..2d9036a8 100644 --- a/tests/Api/DataFixtures/Factory/BlikAliasFactory.php +++ b/tests/Api/DataFixtures/Factory/BlikAliasFactory.php @@ -5,23 +5,29 @@ namespace Tests\CommerceWeavers\SyliusTpayPlugin\Api\DataFixtures\Factory; use CommerceWeavers\SyliusTpayPlugin\Entity\BlikAlias; +use Sylius\Component\Core\Model\ChannelInterface; use Sylius\Component\Core\Model\CustomerInterface; final class BlikAliasFactory { - public static function create(CustomerInterface $customer, string $value): BlikAlias + public static function create(CustomerInterface $customer, ChannelInterface $channel, string $value): BlikAlias { $blikAlias = new BlikAlias(); $blikAlias->setCustomer($customer); + $blikAlias->setChannel($channel); $blikAlias->setValue($value); return $blikAlias; } - public static function createRegistered(CustomerInterface $customer, string $value, \DateTimeInterface $expirationDate): BlikAlias - { - $blikAlias = self::create($customer, $value); + public static function createRegistered( + CustomerInterface $customer, + ChannelInterface $channel, + string $value, + \DateTimeInterface $expirationDate + ): BlikAlias { + $blikAlias = self::create($customer, $channel, $value); $blikAlias->register($expirationDate); diff --git a/tests/Api/DataFixtures/shop/blik_alias.yml b/tests/Api/DataFixtures/shop/blik_alias.yml index 23787c39..e632b49c 100644 --- a/tests/Api/DataFixtures/shop/blik_alias.yml +++ b/tests/Api/DataFixtures/shop/blik_alias.yml @@ -2,6 +2,7 @@ CommerceWeavers\SyliusTpayPlugin\Entity\BlikAlias: blik_alias_john_doe: value: 'f6fcb158-4ceb-432f-8265-923cd45727b4' customer: '@customer_john_doe' + channel: '@channel_web' __calls: - redefine: [] - register: [ "" ] diff --git a/tests/Api/DataFixtures/shop/blik_ambiguous_alias.yml b/tests/Api/DataFixtures/shop/blik_ambiguous_alias.yml index 180e159b..64feb4e2 100644 --- a/tests/Api/DataFixtures/shop/blik_ambiguous_alias.yml +++ b/tests/Api/DataFixtures/shop/blik_ambiguous_alias.yml @@ -1,3 +1,10 @@ CommerceWeavers\SyliusTpayPlugin\Entity\BlikAlias: blik_alias_john_doe: - __factory: {'Tests\CommerceWeavers\SyliusTpayPlugin\Api\DataFixtures\Factory\BlikAliasFactory::createRegistered': ['@customer_john_doe', 'ambiguous_value', ""] } + __factory: { + 'Tests\CommerceWeavers\SyliusTpayPlugin\Api\DataFixtures\Factory\BlikAliasFactory::createRegistered': [ + '@customer_john_doe', + '@channel_web', + 'ambiguous_value', + "", + ] + } diff --git a/tests/Api/DataFixtures/shop/blik_expired_alias.yml b/tests/Api/DataFixtures/shop/blik_expired_alias.yml index db44cf34..c8bac5b2 100644 --- a/tests/Api/DataFixtures/shop/blik_expired_alias.yml +++ b/tests/Api/DataFixtures/shop/blik_expired_alias.yml @@ -1,3 +1,10 @@ CommerceWeavers\SyliusTpayPlugin\Entity\BlikAlias: blik_alias_john_doe: - __factory: {'Tests\CommerceWeavers\SyliusTpayPlugin\Api\DataFixtures\Factory\BlikAliasFactory::createRegistered': ['@customer_john_doe', 'd576029b-a586-49f4-a355-85f5dccf5107', ""] } + __factory: { + 'Tests\CommerceWeavers\SyliusTpayPlugin\Api\DataFixtures\Factory\BlikAliasFactory::createRegistered': [ + '@customer_john_doe', + '@channel_web', + 'd576029b-a586-49f4-a355-85f5dccf5107', + "", + ] + } diff --git a/tests/Api/DataFixtures/shop/blik_not_registered_alias.yml b/tests/Api/DataFixtures/shop/blik_not_registered_alias.yml index b79d3576..c96c85ce 100644 --- a/tests/Api/DataFixtures/shop/blik_not_registered_alias.yml +++ b/tests/Api/DataFixtures/shop/blik_not_registered_alias.yml @@ -1,3 +1,9 @@ CommerceWeavers\SyliusTpayPlugin\Entity\BlikAlias: blik_alias_john_doe: - __factory: {'Tests\CommerceWeavers\SyliusTpayPlugin\Api\DataFixtures\Factory\BlikAliasFactory::create': ['@customer_john_doe', 'd576029b-a586-49f4-a355-85f5dccf5107'] } + __factory: { + 'Tests\CommerceWeavers\SyliusTpayPlugin\Api\DataFixtures\Factory\BlikAliasFactory::create': [ + '@customer_john_doe', + '@channel_web', + 'd576029b-a586-49f4-a355-85f5dccf5107', + ] + } diff --git a/tests/Unit/Resolver/BlikAliasResolverTest.php b/tests/Unit/Resolver/BlikAliasResolverTest.php index 2c09e987..eacd57b4 100644 --- a/tests/Unit/Resolver/BlikAliasResolverTest.php +++ b/tests/Unit/Resolver/BlikAliasResolverTest.php @@ -11,6 +11,8 @@ use PHPUnit\Framework\TestCase; use Prophecy\PhpUnit\ProphecyTrait; use Prophecy\Prophecy\ObjectProphecy; +use Sylius\Component\Channel\Context\ChannelContextInterface; +use Sylius\Component\Core\Model\ChannelInterface; use Sylius\Component\Core\Model\CustomerInterface; final class BlikAliasResolverTest extends TestCase @@ -21,23 +23,29 @@ final class BlikAliasResolverTest extends TestCase private BlikAliasFactoryInterface|ObjectProphecy $blikAliasFactory; + private ChannelContextInterface|ObjectProphecy $channelContext; + private CustomerInterface|ObjectProphecy $customer; protected function setUp(): void { $this->blikAliasRepository = $this->prophesize(BlikAliasRepositoryInterface::class); $this->blikAliasFactory = $this->prophesize(BlikAliasFactoryInterface::class); + $this->channelContext = $this->prophesize(ChannelContextInterface::class); $this->customer = $this->prophesize(CustomerInterface::class); } public function test_it_resolves_blik_alias_from_repository(): void { + $channel = $this->prophesize(ChannelInterface::class); $blikAlias = $this->prophesize(BlikAliasInterface::class)->reveal(); - $this->blikAliasRepository->findOneByCustomer($this->customer)->willReturn($blikAlias); + $this->channelContext->getChannel()->willReturn($channel); + $this->blikAliasRepository->findOneByCustomerAndChannel($this->customer, $channel)->willReturn($blikAlias); $result = (new BlikAliasResolver( $this->blikAliasRepository->reveal(), $this->blikAliasFactory->reveal(), + $this->channelContext->reveal(), ))->resolve($this->customer->reveal()); $this->assertSame($blikAlias, $result); @@ -45,13 +53,15 @@ public function test_it_resolves_blik_alias_from_repository(): void public function test_it_resolves_blik_alias_from_factory(): void { + $channel = $this->prophesize(ChannelInterface::class); $blikAlias = $this->prophesize(BlikAliasInterface::class)->reveal(); - $this->blikAliasRepository->findOneByCustomer($this->customer)->willReturn(null); - $this->blikAliasFactory->createForCustomer($this->customer)->willReturn($blikAlias); + $this->blikAliasRepository->findOneByCustomerAndChannel($this->customer, $channel)->willReturn(null); + $this->blikAliasFactory->createForCustomerAndChannel($this->customer, $channel)->willReturn($blikAlias); $result = (new BlikAliasResolver( $this->blikAliasRepository->reveal(), $this->blikAliasFactory->reveal(), + $this->channelContext->reveal(), ))->resolve($this->customer->reveal()); $this->assertSame($blikAlias, $result);