Skip to content

Commit 03b501f

Browse files
authored
Disable password removal if absent in from & convert all email (#5)
* [UC-5] Disable password removal if absent in from & convert all email occurences to lowercase & extract resource provider * Add strict check of value
1 parent 2f97949 commit 03b501f

15 files changed

Lines changed: 184 additions & 50 deletions

config/services/handler.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
<argument type="service" id="bit_bag.sylius_user_com_plugin.api.deal_api"/>
1414
<argument type="service" id="bit_bag.sylius_user_com_plugin.api.product_api"/>
1515
<argument type="service" id="monolog.logger"/>
16+
<argument type="service" id="bit_bag.sylius_user_com_plugin.provider.user_com_api_aware_resource_provider"/>
1617
</service>
1718
</services>
1819
</container>

config/services/provider.xml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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
7+
id="bit_bag.sylius_user_com_plugin.provider.user_com_api_aware_resource_provider"
8+
class="BitBag\SyliusUserComPlugin\Provider\UserComApiAwareResourceProvider"
9+
>
10+
<argument type="service" id="sylius.repository.channel"/>
11+
<argument type="service" id="sylius.context.channel.composite"/>
12+
<argument type="service" id="monolog.logger"/>
13+
</service>
14+
</services>
15+
</container>

config/services/updater.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,19 @@
88
class="BitBag\SyliusUserComPlugin\Updater\CustomerWithoutKeyUpdater"
99
>
1010
<argument type="service" id="bit_bag.sylius_user_com_plugin.builder.payload.customer_payload_builder"/>
11-
<argument type="service" id="sylius.context.channel.composite"/>
1211
<argument type="service" id="bit_bag.sylius_user_com_plugin.api.user_api"/>
1312
<argument type="service" id="bit_bag.sylius_user_com_plugin.manager.cookie_manager"/>
13+
<argument type="service" id="bit_bag.sylius_user_com_plugin.provider.user_com_api_aware_resource_provider"/>
1414
</service>
1515

1616
<service
1717
id="bit_bag.sylius_user_com_plugin.updater.customer_with_key_updater"
1818
class="BitBag\SyliusUserComPlugin\Updater\CustomerWithKeyUpdater"
1919
>
2020
<argument type="service" id="bit_bag.sylius_user_com_plugin.builder.payload.customer_payload_builder"/>
21-
<argument type="service" id="sylius.context.channel.composite"/>
2221
<argument type="service" id="bit_bag.sylius_user_com_plugin.api.user_api"/>
2322
<argument type="service" id="bit_bag.sylius_user_com_plugin.manager.cookie_manager"/>
23+
<argument type="service" id="bit_bag.sylius_user_com_plugin.provider.user_com_api_aware_resource_provider"/>
2424
</service>
2525
</services>
2626
</container>

src/Builder/Payload/CustomerPayloadBuilder.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ public function build(string $email, ?CustomerInterface $customer = null, ?Addre
2323
}
2424

2525
$payload = [
26-
'custom_id' => $customer?->getEmail() ?? $email,
27-
'email' => $customer?->getEmail() ?? $email,
26+
'custom_id' => strtolower($customer?->getEmail() ?? $email),
27+
'email' => strtolower($customer?->getEmail() ?? $email),
2828
'firstName' => $customer?->getFirstName(),
2929
'lastName' => $customer?->getLastName(),
3030
'phone_number' => $customer?->getPhoneNumber(),

src/Builder/Payload/OrderPayloadBuilder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public function build(OrderInterface $order): array
2525
$orderPayload = [
2626
'custom_id' => $order->getNumber(),
2727
'name' => sprintf('#%s', $order->getNumber()),
28-
'user_custom_id' => $order->getCustomer()?->getEmail(),
28+
'user_custom_id' => strtolower($order->getCustomer()?->getEmail() ?? ''),
2929
'value' => (float) $order->getTotal() / 100,
3030
'currency' => $order->getCurrencyCode(),
3131
'stage' => $this->getStage($order),

src/EventSubscriber/CustomerProfileUpdatedSubscriber.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public function postCustomerProfileUpdated(FormEvent $event): void
4444
$eventName,
4545
$subject,
4646
$subject->getDefaultAddress(),
47-
$subject->getEmail(),
47+
strtolower($subject->getEmail() ?? ''),
4848
);
4949

5050
return;
@@ -59,7 +59,7 @@ public function postCustomerProfileUpdated(FormEvent $event): void
5959
$eventName,
6060
$customer,
6161
$subject->getShippingAddress(),
62-
$customer?->getEmail(),
62+
strtolower($customer?->getEmail() ?? ''),
6363
);
6464
}
6565
}

src/Form/Extension/UserComApiAwareTypeExtension.php

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,21 @@
1111

1212
namespace BitBag\SyliusUserComPlugin\Form\Extension;
1313

14+
use BitBag\SyliusUserComPlugin\Trait\UserComApiAwareInterface;
1415
use Sylius\Bundle\ChannelBundle\Form\Type\ChannelType;
1516
use Symfony\Component\Form\AbstractTypeExtension;
17+
use Symfony\Component\Form\Event\SubmitEvent;
1618
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
1719
use Symfony\Component\Form\Extension\Core\Type\TextType;
1820
use Symfony\Component\Form\FormBuilderInterface;
21+
use Symfony\Component\Form\FormEvents;
1922
use Symfony\Component\Validator\Constraints\NotBlank;
2023
use Symfony\Component\Validator\Constraints\Url;
2124

2225
final class UserComApiAwareTypeExtension extends AbstractTypeExtension
2326
{
27+
private const USER_COM_API_KEY_PROPERTY = 'userComApiKey';
28+
2429
public function buildForm(FormBuilderInterface $builder, array $options): void
2530
{
2631
$builder
@@ -30,9 +35,7 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
3035
[
3136
'label' => 'bitbag_sylius_user_com_plugin.ui.user_com_api_key',
3237
'required' => false,
33-
'constraints' => [
34-
new NotBlank(['groups' => ['sylius']]),
35-
],
38+
'mapped' => false,
3639
],
3740
)
3841
->add(
@@ -46,7 +49,21 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
4649
new Url(['groups' => ['sylius']]),
4750
],
4851
],
49-
);
52+
)
53+
->addEventListener(FormEvents::SUBMIT, function (SubmitEvent $event) {
54+
$data = $event->getData();
55+
$form = $event->getForm();
56+
57+
$newPassword = $form->get(self::USER_COM_API_KEY_PROPERTY)->getData();
58+
59+
if (
60+
null !== $newPassword
61+
&& is_string($newPassword)
62+
&& $data instanceof UserComApiAwareInterface
63+
) {
64+
$data->setUserComApiKey($newPassword);
65+
}
66+
});
5067
}
5168

5269
public static function getExtendedTypes(): iterable

src/Handler/OrderStateUpdateHandler.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use BitBag\SyliusUserComPlugin\Api\ProductApiInterface;
1616
use BitBag\SyliusUserComPlugin\Builder\Payload\OrderPayloadBuilderInterface;
1717
use BitBag\SyliusUserComPlugin\Builder\Payload\ProductEventPayloadBuilderInterface;
18+
use BitBag\SyliusUserComPlugin\Provider\UserComApiAwareResourceProviderInterface;
1819
use BitBag\SyliusUserComPlugin\Trait\UserComApiAwareInterface;
1920
use Psr\Log\LoggerInterface;
2021
use Sylius\Component\Core\Model\OrderInterface;
@@ -28,17 +29,14 @@ public function __construct(
2829
private readonly DealApiInterface $dealApi,
2930
private readonly ProductApiInterface $productApi,
3031
private readonly LoggerInterface $logger,
32+
private readonly UserComApiAwareResourceProviderInterface $provider,
3133
) {
3234
}
3335

3436
public function handle(OrderInterface $order): void
3537
{
36-
$channel = $order->getChannel();
37-
if (false === $channel instanceof UserComApiAwareInterface) {
38-
throw new \InvalidArgumentException('Channel must implement UserComApiAwareInterface');
39-
}
40-
41-
if (null === $channel->getUserComUrl() || null === $channel->getUserComApiKey()) {
38+
$resource = $this->provider->getApiAwareResourceByOrder($order);
39+
if (null === $resource || null === $resource->getUserComUrl() || null === $resource->getUserComApiKey()) {
4240
return;
4341
}
4442

@@ -51,10 +49,12 @@ public function handle(OrderInterface $order): void
5149

5250
$email = $customer->getEmail();
5351
Assert::notNull($email, 'Order customer email cannot be null while sending order data to User.com');
54-
$this->createProductEvents($order, $channel, $email);
52+
$email = strtolower($email);
53+
54+
$this->createProductEvents($order, $resource, $email);
5555

5656
if (OrderInterface::STATE_NEW === $order->getState()) {
57-
$this->dealApi->createDeal($channel, $this->orderPayloadBuilder->build($order), $email);
57+
$this->dealApi->createDeal($resource, $this->orderPayloadBuilder->build($order), $email);
5858

5959
return;
6060
}
@@ -64,7 +64,7 @@ public function handle(OrderInterface $order): void
6464
Assert::notNull($orderNumber, 'Order number cannot be null while sending order data to User.com');
6565

6666
$this->dealApi->updateDealByCustomId(
67-
$channel,
67+
$resource,
6868
$orderNumber,
6969
$this->orderPayloadBuilder->build($order),
7070
);
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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\Provider;
13+
14+
use BitBag\SyliusUserComPlugin\Trait\UserComApiAwareInterface;
15+
use Psr\Log\LoggerInterface;
16+
use Sylius\Component\Channel\Context\ChannelContextInterface;
17+
use Sylius\Component\Channel\Repository\ChannelRepositoryInterface;
18+
use Sylius\Component\Core\Model\OrderInterface;
19+
use Sylius\Component\Resource\Repository\RepositoryInterface;
20+
use Webmozart\Assert\Assert;
21+
22+
final class UserComApiAwareResourceProvider implements UserComApiAwareResourceProviderInterface
23+
{
24+
public function __construct(
25+
private readonly RepositoryInterface $channelRepository,
26+
private readonly ChannelContextInterface $channelContext,
27+
private readonly LoggerInterface $logger,
28+
) {
29+
}
30+
31+
public function getApiAwareResource(): ?UserComApiAwareInterface
32+
{
33+
$resource = $this->channelContext->getChannel();
34+
if (!$resource instanceof UserComApiAwareInterface) {
35+
$this->addLogMessage('getApiAwareResource');
36+
37+
return null;
38+
}
39+
40+
return $resource;
41+
}
42+
43+
public function getApiAwareResourceByOrder(OrderInterface $order): ?UserComApiAwareInterface
44+
{
45+
$resource = $order->getChannel();
46+
if (!$resource instanceof UserComApiAwareInterface) {
47+
$this->addLogMessage('getApiAwareResourceByOrder');
48+
49+
return null;
50+
}
51+
52+
return $resource;
53+
}
54+
55+
public function getApiAwareResourceByFormData(string $code): ?UserComApiAwareInterface
56+
{
57+
Assert::isInstanceOf($this->channelRepository, ChannelRepositoryInterface::class);
58+
$resource = $this->channelRepository->findOneByCode($code);
59+
60+
if (!$resource instanceof UserComApiAwareInterface) {
61+
$this->addLogMessage('getApiAwareResourceByFormData');
62+
63+
return null;
64+
}
65+
66+
return $resource;
67+
}
68+
69+
private function addLogMessage(string $method): void
70+
{
71+
$this->logger->warning('User.com API aware resource not found using ' . $method);
72+
}
73+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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\Provider;
13+
14+
use BitBag\SyliusUserComPlugin\Trait\UserComApiAwareInterface;
15+
use Sylius\Component\Core\Model\OrderInterface;
16+
17+
interface UserComApiAwareResourceProviderInterface
18+
{
19+
public function getApiAwareResource(): ?UserComApiAwareInterface;
20+
21+
public function getApiAwareResourceByOrder(OrderInterface $order): ?UserComApiAwareInterface;
22+
23+
public function getApiAwareResourceByFormData(string $code): ?UserComApiAwareInterface;
24+
}

0 commit comments

Comments
 (0)