Skip to content

Commit 3c10b2a

Browse files
committed
Remove try catch && add custom exception
1 parent ec0e694 commit 3c10b2a

6 files changed

Lines changed: 51 additions & 61 deletions

File tree

config/packages/messenger.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ framework:
55
dsn: "%env(MESSENGER_USER_COM_ASYNCHRONOUS)%"
66
retry_strategy:
77
max_retries: 5
8-
delay: 300000
8+
#1000 * 60 * 10
9+
delay: 600000
910
multiplier: 2
1011
routing:
1112
BitBag\SyliusUserComPlugin\Message\OrderSynchronization: user_com_asynchronous

config/services/handler.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
<argument type="service" id="sylius.repository.customer"/>
2323
<argument type="service" id="bit_bag.sylius_user_com_plugin.provider.user_com_api_aware_resource_provider"/>
2424
<argument type="service" id="bit_bag.sylius_user_com_plugin.manager.customer_update_manager"/>
25-
<argument type="service" id="monolog.logger.user_com"/>
2625
<tag name="messenger.message_handler"/>
2726
</service>
2827
</services>

src/Api/AbstractClient.php

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

@@ -68,7 +67,7 @@ protected function request(
6867
}
6968

7069
if ($status > 499 && $status < 600) {
71-
throw new RecoverableMessageHandlingException(
70+
throw new UserComServerException(
7271
sprintf(
7372
'Response status code : %s, response : %s',
7473
$status,
@@ -94,7 +93,7 @@ protected function request(
9493
'options' => $options,
9594
]);
9695

97-
if ($e instanceof RecoverableMessageHandlingException) {
96+
if ($e instanceof UserComServerException) {
9897
throw $e;
9998
}
10099

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace BitBag\SyliusUserComPlugin\Exception;
6+
7+
final class UserComServerException extends \Exception
8+
{
9+
}

src/Handler/OrderSynchronizationHandler.php

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -33,25 +33,17 @@ public function __construct(
3333

3434
public function __invoke(OrderSynchronization $orderSynchronization): void
3535
{
36-
try {
37-
Assert::isInstanceOf($this->orderRepository, OrderRepositoryInterface::class);
38-
$order = $this->orderRepository->find($orderSynchronization->getOrderId());
39-
if (!$order instanceof OrderInterface) {
40-
throw new UnrecoverableMessageHandlingException(
41-
sprintf(
42-
'Order with id %s has not been found.',
43-
$orderSynchronization->getOrderId(),
44-
),
45-
);
46-
}
47-
48-
$this->orderUpdateManager->handle($order);
49-
} catch (\Exception $exception) {
50-
if ($exception instanceof RecoverableMessageHandlingException) {
51-
throw $exception;
52-
}
53-
54-
throw new UnrecoverableMessageHandlingException($exception->getMessage());
36+
Assert::isInstanceOf($this->orderRepository, OrderRepositoryInterface::class);
37+
$order = $this->orderRepository->find($orderSynchronization->getOrderId());
38+
if (!$order instanceof OrderInterface) {
39+
throw new UnrecoverableMessageHandlingException(
40+
sprintf(
41+
'Order with id %s has not been found.',
42+
$orderSynchronization->getOrderId(),
43+
),
44+
);
5545
}
46+
47+
$this->orderUpdateManager->handle($order);
5648
}
5749
}

src/Handler/UserSynchronizationHandler.php

Lines changed: 27 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -31,49 +31,39 @@ public function __construct(
3131
private readonly RepositoryInterface $customerRepository,
3232
private readonly UserComApiAwareResourceProviderInterface $userComApiAwareResourceProvider,
3333
private readonly CustomerUpdateManagerInterface $updateManager,
34-
private readonly LoggerInterface $logger,
3534
) {
3635
}
3736

3837
public function __invoke(UserSynchronization $userSynchronization): void
3938
{
40-
try {
41-
Assert::isInstanceOf($this->customerRepository, CustomerRepositoryInterface::class);
42-
Assert::isInstanceOf($this->addressRepository, AddressRepositoryInterface::class);
43-
44-
$customer = $this->customerRepository->find($userSynchronization->getCustomerId());
45-
Assert::isInstanceOf($customer, CustomerInterface::class);
46-
47-
$email = $userSynchronization->getEmail();
48-
49-
$addressId = $userSynchronization->getAddressId();
50-
$address = $this->getAddress($addressId);
51-
52-
$resourceId = $userSynchronization->getUserComApiAwareResourceId();
53-
54-
$userComApiAwareResource = $this->userComApiAwareResourceProvider->getApiAwareResourceById($resourceId);
55-
if (null === $userComApiAwareResource) {
56-
throw new UnrecoverableMessageHandlingException('User.com API-aware resource not found');
57-
}
58-
59-
$eventName = $userSynchronization->getEventName();
60-
61-
$this->updateManager->manageChange(
62-
$eventName,
63-
$userComApiAwareResource,
64-
$customer,
65-
$address,
66-
$email,
67-
$userSynchronization->getUserComCookie(),
68-
);
69-
} catch (\Exception $exception) {
70-
$this->logger->critical(sprintf('User.com synchronization failed - %s', $exception->getMessage()));
71-
if ($exception instanceof RecoverableMessageHandlingException) {
72-
throw $exception;
73-
}
74-
75-
throw new UnrecoverableMessageHandlingException($exception->getMessage());
39+
Assert::isInstanceOf($this->customerRepository, CustomerRepositoryInterface::class);
40+
Assert::isInstanceOf($this->addressRepository, AddressRepositoryInterface::class);
41+
42+
$customer = $this->customerRepository->find($userSynchronization->getCustomerId());
43+
Assert::isInstanceOf($customer, CustomerInterface::class);
44+
45+
$email = $userSynchronization->getEmail();
46+
47+
$addressId = $userSynchronization->getAddressId();
48+
$address = $this->getAddress($addressId);
49+
50+
$resourceId = $userSynchronization->getUserComApiAwareResourceId();
51+
52+
$userComApiAwareResource = $this->userComApiAwareResourceProvider->getApiAwareResourceById($resourceId);
53+
if (null === $userComApiAwareResource) {
54+
throw new UnrecoverableMessageHandlingException('User.com API-aware resource not found');
7655
}
56+
57+
$eventName = $userSynchronization->getEventName();
58+
59+
$this->updateManager->manageChange(
60+
$eventName,
61+
$userComApiAwareResource,
62+
$customer,
63+
$address,
64+
$email,
65+
$userSynchronization->getUserComCookie(),
66+
);
7767
}
7868

7969
private function getAddress(?int $addressId): ?AddressInterface

0 commit comments

Comments
 (0)