Skip to content

Commit

Permalink
Add channel support for Blik aliases
Browse files Browse the repository at this point in the history
  • Loading branch information
coldic3 committed Nov 4, 2024
1 parent 5347811 commit 8bf466d
Show file tree
Hide file tree
Showing 16 changed files with 128 additions and 18 deletions.
4 changes: 4 additions & 0 deletions config/doctrine/BlikAlias.orm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,9 @@
<one-to-one field="customer" target-entity="Sylius\Component\Core\Model\CustomerInterface">
<join-column name="customer_id" nullable="false" on-delete="CASCADE" />
</one-to-one>

<many-to-one field="channel" target-entity="Sylius\Component\Core\Model\ChannelInterface">
<join-column name="channel_id" nullable="false" on-delete="CASCADE" />
</many-to-one>
</mapped-superclass>
</doctrine-mapping>
1 change: 1 addition & 0 deletions config/services/resolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')
;
Expand Down
30 changes: 30 additions & 0 deletions migrations/Version20241104080950.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace CommerceWeaversSyliusTpayMigrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

final class Version20241104080950 extends AbstractMigration
{
public function getDescription(): string
{
return 'Add BlikAlias::channel relation.';
}

public function up(Schema $schema): void
{
$this->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');
}
}
13 changes: 13 additions & 0 deletions src/Entity/BlikAlias.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -19,6 +20,8 @@ class BlikAlias implements BlikAliasInterface

private ?CustomerInterface $customer = null;

private ?ChannelInterface $channel = null;

public function getId(): ?int
{
return $this->id;
Expand Down Expand Up @@ -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;
}
}
5 changes: 5 additions & 0 deletions src/Entity/BlikAliasInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
}
8 changes: 6 additions & 2 deletions src/Factory/BlikAliasFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}
Expand Down
3 changes: 2 additions & 1 deletion src/Factory/BlikAliasFactoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
9 changes: 7 additions & 2 deletions src/Repository/BlikAliasRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
;
Expand Down
6 changes: 5 additions & 1 deletion src/Repository/BlikAliasRepositoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@
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;

interface BlikAliasRepositoryInterface extends RepositoryInterface
{
public function findOneByValue(string $value): ?BlikAliasInterface;

public function findOneByCustomer(CustomerInterface $customer): ?BlikAliasInterface;
public function findOneByCustomerAndChannel(
CustomerInterface $customer,
ChannelInterface $channel,
): ?BlikAliasInterface;
}
10 changes: 8 additions & 2 deletions src/Resolver/BlikAliasResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,25 @@
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
{
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);
}
}
14 changes: 10 additions & 4 deletions tests/Api/DataFixtures/Factory/BlikAliasFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
1 change: 1 addition & 0 deletions tests/Api/DataFixtures/shop/blik_alias.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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: [ "<dateTimeBetween('+1 day', '+1 month')>" ]
9 changes: 8 additions & 1 deletion tests/Api/DataFixtures/shop/blik_ambiguous_alias.yml
Original file line number Diff line number Diff line change
@@ -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', "<dateTimeBetween('+1 day', '+1 month')>"] }
__factory: {
'Tests\CommerceWeavers\SyliusTpayPlugin\Api\DataFixtures\Factory\BlikAliasFactory::createRegistered': [
'@customer_john_doe',
'@channel_web',
'ambiguous_value',
"<dateTimeBetween('+1 day', '+1 month')>",
]
}
9 changes: 8 additions & 1 deletion tests/Api/DataFixtures/shop/blik_expired_alias.yml
Original file line number Diff line number Diff line change
@@ -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', "<dateTimeBetween('-1 month', '-1 day')>"] }
__factory: {
'Tests\CommerceWeavers\SyliusTpayPlugin\Api\DataFixtures\Factory\BlikAliasFactory::createRegistered': [
'@customer_john_doe',
'@channel_web',
'd576029b-a586-49f4-a355-85f5dccf5107',
"<dateTimeBetween('-1 month', '-1 day')>",
]
}
8 changes: 7 additions & 1 deletion tests/Api/DataFixtures/shop/blik_not_registered_alias.yml
Original file line number Diff line number Diff line change
@@ -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',
]
}
16 changes: 13 additions & 3 deletions tests/Unit/Resolver/BlikAliasResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -21,37 +23,45 @@ 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);
}

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);
Expand Down

0 comments on commit 8bf466d

Please sign in to comment.