Skip to content

Commit 6a2ba31

Browse files
authored
Merge pull request #6 from BitBagCommerce/feature/UC-6-agreements-endpoint
Add agreements endpoint
2 parents 03b501f + e9861bf commit 6a2ba31

19 files changed

Lines changed: 425 additions & 8 deletions

config/services/assigner.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
3+
<container xmlns="http://symfony.com/schema/dic/services" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
4+
<services>
5+
<defaults autowire="false" autoconfigure="false"/>
6+
<service id="bit_bag.sylius_user_com_plugin.assigner.agreements_assigner" class="BitBag\SyliusUserComPlugin\Assigner\AgreementsAssigner">
7+
<argument type="service" id="monolog.logger"/>
8+
</service>
9+
</services>
10+
</container>

config/services/authenticator.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
3+
<container xmlns="http://symfony.com/schema/dic/services" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
4+
<services>
5+
<defaults autowire="false" autoconfigure="false"/>
6+
<service id="bit_bag.sylius_user_com_plugin.authenticator.request_authenticator" class="BitBag\SyliusUserComPlugin\Authenticator\RequestAuthenticator">
7+
<argument type="service" id="sylius.context.channel.composite"/>
8+
<argument type="service" id="monolog.logger"/>
9+
</service>
10+
</services>
11+
</container>

config/services/controller.xml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
3+
<container xmlns="http://symfony.com/schema/dic/services" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
4+
<services>
5+
<defaults autowire="false" autoconfigure="false" public="true"/>
6+
<service
7+
id="bit_bag.sylius_user_com_plugin.controller.user_com_agreements_controller"
8+
class="BitBag\SyliusUserComPlugin\Controller\UserComAgreementsController"
9+
public="true"
10+
>
11+
<argument type="service" id="bit_bag.sylius_user_com_plugin.authenticator.request_authenticator"/>
12+
<argument type="service" id="bit_bag.sylius_user_com_plugin.synchronizer.customer_agreements_synchronizer"/>
13+
<argument type="service" id="bit_bag.sylius_user_com_plugin.validator.payload_validator"/>
14+
<argument type="service" id="monolog.logger"/>
15+
</service>
16+
</services>
17+
</container>

config/services/synchronizer.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
3+
<container xmlns="http://symfony.com/schema/dic/services" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
4+
<services>
5+
<defaults autowire="false" autoconfigure="false"/>
6+
<service id="bit_bag.sylius_user_com_plugin.synchronizer.customer_agreements_synchronizer" class="BitBag\SyliusUserComPlugin\Synchronizer\CustomerAgreementsSynchronizer">
7+
<argument type="service" id="sylius.repository.customer"/>
8+
<argument type="service" id="bit_bag.sylius_user_com_plugin.assigner.agreements_assigner"/>
9+
</service>
10+
</services>
11+
</container>

config/services/validator.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
3+
<container xmlns="http://symfony.com/schema/dic/services" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
4+
<services>
5+
<defaults autowire="false" autoconfigure="false"/>
6+
<service id="bit_bag.sylius_user_com_plugin.validator.payload_validator" class="BitBag\SyliusUserComPlugin\Validator\PayloadValidator"/>
7+
</services>
8+
</container>

config/shop_routing.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
11
# Delete these routes and define your own shop routes here
2+
bitbag_user_com_customer_agreements_endpoint:
3+
path: /user-com/customer-agreements
4+
controller: bit_bag.sylius_user_com_plugin.controller.user_com_agreements_controller
5+
methods: [POST]
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
/*
4+
* This file has been created by developers from BitBag.
5+
* Feel free to contact us once you face any issues or want to start
6+
* You can find more information about us on https://bitbag.io and write us
7+
* an email on hello@bitbag.io.
8+
*/
9+
10+
declare(strict_types=1);
11+
12+
namespace BitBag\SyliusUserComPlugin\Assigner;
13+
14+
use Psr\Log\LoggerInterface;
15+
use Sylius\Component\Core\Model\CustomerInterface;
16+
17+
final class AgreementsAssigner implements AgreementsAssignerInterface
18+
{
19+
public function __construct(
20+
private readonly LoggerInterface $logger,
21+
) {
22+
}
23+
24+
public function assign(CustomerInterface $customer, array $agreements): void
25+
{
26+
foreach ($agreements as $key => $value) {
27+
match ($key) {
28+
'email_agreement' => $customer->setSubscribedToNewsletter($value),
29+
default => $this->logger->error(
30+
sprintf(
31+
'Agreement not found. Key = %s, Value = %s, CustomerId = %s',
32+
$key,
33+
$value,
34+
$customer->getId(),
35+
),
36+
),
37+
};
38+
}
39+
}
40+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
/*
4+
* This file has been created by developers from BitBag.
5+
* Feel free to contact us once you face any issues or want to start
6+
* You can find more information about us on https://bitbag.io and write us
7+
* an email on hello@bitbag.io.
8+
*/
9+
10+
declare(strict_types=1);
11+
12+
namespace BitBag\SyliusUserComPlugin\Assigner;
13+
14+
use Sylius\Component\Core\Model\CustomerInterface;
15+
16+
interface AgreementsAssignerInterface
17+
{
18+
public function assign(CustomerInterface $customer, array $agreements): void;
19+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
3+
/*
4+
* This file has been created by developers from BitBag.
5+
* Feel free to contact us once you face any issues or want to start
6+
* You can find more information about us on https://bitbag.io and write us
7+
* an email on hello@bitbag.io.
8+
*/
9+
10+
declare(strict_types=1);
11+
12+
namespace BitBag\SyliusUserComPlugin\Authenticator;
13+
14+
use BitBag\SyliusUserComPlugin\Trait\UserComApiAwareInterface;
15+
use Psr\Log\LoggerInterface;
16+
use Sylius\Component\Channel\Context\ChannelContextInterface;
17+
use Symfony\Component\HttpFoundation\Request;
18+
use Symfony\Component\HttpFoundation\Response;
19+
20+
final class RequestAuthenticator implements RequestAuthenticatorInterface
21+
{
22+
public function __construct(
23+
private readonly ChannelContextInterface $channelContext,
24+
private readonly LoggerInterface $logger,
25+
) {
26+
}
27+
28+
public function authenticate(Request $request): bool
29+
{
30+
$channel = $this->channelContext->getChannel();
31+
if (!$channel instanceof UserComApiAwareInterface) {
32+
return false;
33+
}
34+
35+
$apiKey = $channel->getUserComApiKey();
36+
$requestApiKey = $request->headers->get('X-User-Com-Signature');
37+
if (null === $requestApiKey) {
38+
return false;
39+
}
40+
41+
$content = $request->getContent(false);
42+
43+
if (is_resource($content)) {
44+
$content = stream_get_contents($content);
45+
}
46+
47+
if (!is_string($content)) {
48+
throw new \InvalidArgumentException('Invalid JSON payload', Response::HTTP_BAD_REQUEST);
49+
}
50+
51+
$content = json_encode(
52+
json_decode($content, true),
53+
\JSON_UNESCAPED_SLASHES | \JSON_UNESCAPED_UNICODE,
54+
);
55+
56+
if (false === is_string($content)) {
57+
$this->logger->warning('User.com - Invalid JSON content.');
58+
59+
throw new \InvalidArgumentException('Invalid JSON content.');
60+
}
61+
62+
if (null === $apiKey) {
63+
$this->logger->warning('User.com - Missing API key.');
64+
65+
return false;
66+
}
67+
68+
$signature = hash_hmac('sha256', $content, $apiKey);
69+
70+
return hash_equals($signature, $requestApiKey);
71+
}
72+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
/*
4+
* This file has been created by developers from BitBag.
5+
* Feel free to contact us once you face any issues or want to start
6+
* You can find more information about us on https://bitbag.io and write us
7+
* an email on hello@bitbag.io.
8+
*/
9+
10+
declare(strict_types=1);
11+
12+
namespace BitBag\SyliusUserComPlugin\Authenticator;
13+
14+
use Symfony\Component\HttpFoundation\Request;
15+
16+
interface RequestAuthenticatorInterface
17+
{
18+
public function authenticate(Request $request): bool;
19+
}

0 commit comments

Comments
 (0)