-
Notifications
You must be signed in to change notification settings - Fork 23
/
SimpleRetryCompilerPass.php
88 lines (75 loc) · 2.53 KB
/
SimpleRetryCompilerPass.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<?php
/**
* PHP Service Bus (publish-subscribe pattern implementation).
*
* @author Maksim Masiukevich <contacts@desperado.dev>
* @license MIT
* @license https://opensource.org/licenses/MIT
*/
declare(strict_types = 0);
namespace ServiceBus\Application\DependencyInjection\Compiler\Retry;
use ServiceBus\Common\EntryPoint\Retry\RetryStrategy;
use ServiceBus\MessageSerializer\MessageSerializer;
use ServiceBus\Retry\SimpleRetryStrategy;
use ServiceBus\Storage\Common\DatabaseAdapter;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;
/**
*
*/
final class SimpleRetryCompilerPass implements CompilerPassInterface
{
/**
* @var int
*/
private $maxRetryCount;
/**
* Retry delay (in seconds)
*
* @var int
*/
private $retryDelay;
public function __construct(int $maxRetryCount, int $retryDelay)
{
$this->maxRetryCount = $maxRetryCount;
$this->retryDelay = $retryDelay;
}
public function process(ContainerBuilder $container): void
{
$this->checkConfiguration($container);
$this->injectParameters($container);
$definition = new Definition(SimpleRetryStrategy::class, [
new Reference(DatabaseAdapter::class),
new Reference(MessageSerializer::class),
'%service_bus.retry.simple.max_retry_count%',
'%service_bus.retry.simple.retry_delay%'
]);
$container->setDefinition(RetryStrategy::class, $definition);
}
private function injectParameters(ContainerBuilder $containerBuilder): void
{
$parameters = [
'service_bus.retry.simple.max_retry_count' => $this->maxRetryCount,
'service_bus.retry.simple.retry_delay' => $this->retryDelay
];
foreach ($parameters as $key => $value)
{
$containerBuilder->setParameter(
name: $key,
value: $value
);
}
}
private function checkConfiguration(ContainerBuilder $container): void
{
/** @noinspection ClassConstantCanBeUsedInspection */
if (
\interface_exists('ServiceBus\Storage\Common\DatabaseAdapter') === false ||
$container->has(DatabaseAdapter::class) === false
) {
throw new \LogicException('Module `php-service-bus/storage` must be installed and loaded');
}
}
}