Skip to content

Commit a6261bb

Browse files
committed
Add asynchronous handling
1 parent c4f1e40 commit a6261bb

32 files changed

Lines changed: 589 additions & 73 deletions

config/packages/messenger.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
framework:
2+
messenger:
3+
transports:
4+
user_com_asynchronous:
5+
dsn: "%env(MESSENGER_USER_COM_ASYNCHRONOUS)%"
6+
retry_strategy:
7+
max_retries: 5
8+
delay: 60000
9+
multiplier: 2
10+
routing:
11+
BitBag\SyliusUserComPlugin\Message\OrderSynchronization: user_com_asynchronous
12+
BitBag\SyliusUserComPlugin\Message\UserSynchronization: user_com_asynchronous

config/services/dispatcher.xml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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.dispatcher.order_message_dispatcher"
8+
class="BitBag\SyliusUserComPlugin\Dispatcher\OrderMessageDispatcher"
9+
public="true"
10+
>
11+
<argument type="service" id="messenger.default_bus"/>
12+
</service>
13+
14+
<service
15+
id="bit_bag.sylius_user_com_plugin.dispatcher.customer_message_dispatcher"
16+
class="BitBag\SyliusUserComPlugin\Dispatcher\CustomerMessageDispatcher"
17+
>
18+
<argument type="service" id="messenger.default_bus"/>
19+
</service>
20+
</services>
21+
</container>

config/services/event_subscriber.xml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,14 @@
66
<service
77
id="bit_bag.sylius_user_com_plugin.event_subscriber.customer_profile_updated_subscriber"
88
class="BitBag\SyliusUserComPlugin\EventSubscriber\CustomerProfileUpdatedSubscriber"
9+
public="true"
910
>
10-
<argument type="service" id="bit_bag.sylius_user_com_plugin.manager.customer_update_manager"/>
11+
<argument type="service" id="bit_bag.sylius_user_com_plugin.dispatcher.customer_message_dispatcher"/>
12+
<argument type="service" id="bit_bag.sylius_user_com_plugin.provider.user_com_api_aware_resource_provider"/>
13+
<argument type="service" id="bit_bag.sylius_user_com_plugin.manager.cookie_manager"/>
14+
<argument type="service" id="request_stack"/>
15+
<argument type="service" id="monolog.logger.user_com"/>
16+
<tag name="kernel.event_subscriber"/>
1117
</service>
1218
</services>
1319
</container>

config/services/form.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
<services>
66
<defaults public="true"/>
77
<service id="bit_bag.sylius_user_com_plugin.form.extension.user_com_customer_information_type_extension" class="BitBag\SyliusUserComPlugin\Form\Extension\UserComCustomerInformationTypeExtension">
8-
<argument type="service" id="bit_bag.sylius_user_com_plugin.event_subscriber.customer_profile_updated_subscriber"/>
98
<argument type="service" id="sylius.context.customer"/>
109
<tag name="form.type_extension" />
1110
</service>

config/services/handler.xml

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,26 @@
44
<services>
55
<defaults autowire="false" autoconfigure="false"/>
66
<service
7-
id="bit_bag.sylius_user_com_plugin.handler.order_state_update_handler"
8-
class="BitBag\SyliusUserComPlugin\Handler\OrderStateUpdateHandler"
7+
id="bit_bag.sylius_user_com_plugin.handler.order_synchronization_handler"
8+
class="BitBag\SyliusUserComPlugin\Handler\OrderSynchronizationHandler"
99
public="true"
1010
>
11-
<argument type="service" id="bit_bag.sylius_user_com_plugin.builder.payload.order_payload_builder"/>
12-
<argument type="service" id="bit_bag.sylius_user_com_plugin.builder.payload.product_event_payload_builder"/>
13-
<argument type="service" id="bit_bag.sylius_user_com_plugin.api.deal_api"/>
14-
<argument type="service" id="bit_bag.sylius_user_com_plugin.api.product_api"/>
15-
<argument type="service" id="monolog.logger.user_com"/>
11+
<argument type="service" id="sylius.repository.order"/>
12+
<argument type="service" id="bit_bag.sylius_user_com_plugin.manager.order_update_manager"/>
13+
<tag name="messenger.message_handler"/>
14+
</service>
15+
16+
<service
17+
id="bit_bag.sylius_user_com_plugin.handler.user_synchronization_handler"
18+
class="BitBag\SyliusUserComPlugin\Handler\UserSynchronizationHandler"
19+
public="true"
20+
>
21+
<argument type="service" id="sylius.repository.address"/>
22+
<argument type="service" id="sylius.repository.customer"/>
1623
<argument type="service" id="bit_bag.sylius_user_com_plugin.provider.user_com_api_aware_resource_provider"/>
24+
<argument type="service" id="bit_bag.sylius_user_com_plugin.manager.customer_update_manager"/>
25+
<argument type="service" id="monolog.logger.user_com"/>
26+
<tag name="messenger.message_handler"/>
1727
</service>
1828
</services>
1929
</container>

config/services/manager.xml

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,31 @@
88
class="BitBag\SyliusUserComPlugin\Manager\CookieManager"
99
>
1010
<argument type="service" id="request_stack"/>
11+
<argument type="service" id="security.helper"/>
1112
</service>
1213

1314
<service
1415
id="bit_bag.sylius_user_com_plugin.manager.customer_update_manager"
1516
class="BitBag\SyliusUserComPlugin\Manager\CustomerUpdateManager"
1617
>
17-
<argument type="service" id="bit_bag.sylius_user_com_plugin.manager.cookie_manager"/>
1818
<argument type="service" id="bit_bag.sylius_user_com_plugin.updater.customer_with_key_updater"/>
1919
<argument type="service" id="bit_bag.sylius_user_com_plugin.updater.customer_without_key_updater"/>
2020
<argument type="service" id="monolog.logger.user_com"/>
2121
</service>
2222

23+
<service
24+
id="bit_bag.sylius_user_com_plugin.manager.order_update_manager"
25+
class="BitBag\SyliusUserComPlugin\Manager\OrderUpdateManager"
26+
public="true"
27+
>
28+
<argument type="service" id="bit_bag.sylius_user_com_plugin.builder.payload.order_payload_builder"/>
29+
<argument type="service" id="bit_bag.sylius_user_com_plugin.builder.payload.product_event_payload_builder"/>
30+
<argument type="service" id="bit_bag.sylius_user_com_plugin.api.deal_api"/>
31+
<argument type="service" id="bit_bag.sylius_user_com_plugin.api.product_api"/>
32+
<argument type="service" id="monolog.logger.user_com"/>
33+
<argument type="service" id="bit_bag.sylius_user_com_plugin.provider.user_com_api_aware_resource_provider"/>
34+
</service>
35+
2336
<service
2437
id="bit_bag.sylius_user_com_plugin.manager.user_com_api_token_manager"
2538
class="BitBag\SyliusUserComPlugin\Manager\UserComApiTokenManager"

config/state_machine/order.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@ winzou_state_machine:
88
after:
99
order_user_com_update:
1010
on: ['create', 'cancel', 'fulfill']
11-
do: ['@bit_bag.sylius_user_com_plugin.handler.order_state_update_handler', 'handle']
11+
do: ['@bit_bag.sylius_user_com_plugin.dispatcher.order_message_dispatcher', 'dispatch']
1212
args: ['object']

src/Api/AbstractClient.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use BitBag\SyliusUserComPlugin\Trait\UserComApiAwareInterface;
1616
use Psr\Log\LoggerInterface;
1717
use Symfony\Component\HttpFoundation\Response;
18+
use Symfony\Component\Messenger\Exception\RecoverableMessageHandlingException;
1819
use Symfony\Contracts\HttpClient\HttpClientInterface;
1920
use Symfony\Contracts\HttpClient\ResponseInterface;
2021

@@ -66,6 +67,16 @@ protected function request(
6667
return $response->toArray();
6768
}
6869

70+
if ($status > 499 && $status < 600) {
71+
throw new RecoverableMessageHandlingException(
72+
sprintf(
73+
'Response status code : %s, response : %s',
74+
$status,
75+
$response->getContent(false),
76+
),
77+
);
78+
}
79+
6980
throw new \Exception(
7081
sprintf(
7182
'Response status code : %s, response : %s',
@@ -83,6 +94,10 @@ protected function request(
8394
'options' => $options,
8495
]);
8596

97+
if ($e instanceof RecoverableMessageHandlingException) {
98+
throw $e;
99+
}
100+
86101
if (isset($response) &&
87102
$response->getStatusCode() === Response::HTTP_NOT_FOUND
88103
) {
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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\Dispatcher;
13+
14+
use BitBag\SyliusUserComPlugin\Message\UserSynchronization;
15+
use BitBag\SyliusUserComPlugin\Trait\UserComApiAwareInterface;
16+
use Sylius\Component\Core\Model\AddressInterface;
17+
use Sylius\Component\Core\Model\CustomerInterface;
18+
use Symfony\Component\Messenger\MessageBusInterface;
19+
20+
final class CustomerMessageDispatcher implements CustomerMessageDispatcherInterface
21+
{
22+
public function __construct(
23+
private readonly MessageBusInterface $messageBus,
24+
) {
25+
}
26+
27+
public function dispatch(
28+
string $eventName,
29+
UserComApiAwareInterface $apiAwareResource,
30+
?string $cookie,
31+
?CustomerInterface $customer = null,
32+
?AddressInterface $address = null,
33+
?string $email = null,
34+
): void {
35+
$this->messageBus->dispatch(new UserSynchronization(
36+
$eventName,
37+
$apiAwareResource->getId(),
38+
$cookie,
39+
$customer?->getId(),
40+
$address?->getId(),
41+
$email,
42+
));
43+
}
44+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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\Dispatcher;
13+
14+
use BitBag\SyliusUserComPlugin\Trait\UserComApiAwareInterface;
15+
use Sylius\Component\Core\Model\AddressInterface;
16+
use Sylius\Component\Core\Model\CustomerInterface;
17+
18+
interface CustomerMessageDispatcherInterface
19+
{
20+
public function dispatch(
21+
string $eventName,
22+
UserComApiAwareInterface $apiAwareResource,
23+
?string $cookie,
24+
?CustomerInterface $customer = null,
25+
?AddressInterface $address = null,
26+
?string $email = null,
27+
): void;
28+
}

0 commit comments

Comments
 (0)