|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Bdf\QueueBundle\Consumption; |
|
4
|
|
|
|
|
5
|
|
|
use Bdf\Queue\Consumer\Receiver\Builder\ReceiverBuilder; |
|
6
|
|
|
use Bdf\Queue\Consumer\Receiver\Builder\ReceiverFactory; |
|
7
|
|
|
use Bdf\Queue\Consumer\Receiver\Builder\ReceiverLoaderInterface; |
|
8
|
|
|
use Psr\Container\ContainerInterface; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Loader for receiver configuration |
|
12
|
|
|
*/ |
|
13
|
|
|
class ReceiverLoader implements ReceiverLoaderInterface |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var ContainerInterface |
|
17
|
|
|
*/ |
|
18
|
|
|
private $container; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var ReceiverFactory |
|
22
|
|
|
*/ |
|
23
|
|
|
private $factory; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var array[] |
|
27
|
|
|
*/ |
|
28
|
|
|
private $configuration; |
|
29
|
|
|
|
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* ReceiverLoader constructor. |
|
33
|
|
|
* |
|
34
|
|
|
* @param ContainerInterface $container |
|
35
|
|
|
* @param array[] $configuration |
|
36
|
|
|
* @param ReceiverFactory|null $factory |
|
37
|
|
|
*/ |
|
38
|
2 |
|
public function __construct(ContainerInterface $container, array $configuration, ReceiverFactory $factory = null) |
|
39
|
|
|
{ |
|
40
|
2 |
|
$this->container = $container; |
|
41
|
2 |
|
$this->configuration = $configuration; |
|
42
|
2 |
|
$this->factory = $factory; |
|
43
|
2 |
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Load the receiver build from the configuration |
|
47
|
|
|
* |
|
48
|
|
|
* @param string $name The destination name |
|
49
|
|
|
* |
|
50
|
|
|
* @return ReceiverBuilder |
|
51
|
|
|
*/ |
|
52
|
2 |
|
public function load(string $name): ReceiverBuilder |
|
53
|
|
|
{ |
|
54
|
2 |
|
$builder = new ReceiverBuilder($this->container, null, $this->factory); |
|
55
|
|
|
|
|
56
|
2 |
|
if (!isset($this->configuration[$name])) { |
|
57
|
1 |
|
return $builder; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
1 |
|
$this->configure($builder, $this->configuration[$name]); |
|
61
|
|
|
|
|
62
|
1 |
|
return $builder; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @param ReceiverBuilder $builder |
|
67
|
|
|
* |
|
68
|
|
|
* @param array $config |
|
69
|
|
|
*/ |
|
70
|
1 |
|
private function configure(ReceiverBuilder $builder, array $config) |
|
71
|
|
|
{ |
|
72
|
1 |
|
$builder->log(); |
|
73
|
|
|
|
|
74
|
1 |
|
if ($config['middlewares'] ?? false) { |
|
75
|
|
|
foreach ($config['middlewares'] as $middleware => $parameters) { |
|
76
|
|
|
if (is_int($middleware)) { |
|
77
|
|
|
$middleware = $parameters; |
|
78
|
|
|
$parameters = []; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
$builder->add($middleware, $parameters); |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
1 |
|
if ($config['retry'] ?? false) { |
|
86
|
1 |
|
$builder->retry($config['retry']); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
1 |
|
if ($config['save'] ?? false) { |
|
90
|
|
|
$builder->store(); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
1 |
|
if ($config['limit'] ?? false) { |
|
94
|
1 |
|
$builder->limit($config['limit']); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
1 |
|
if (($config['no_failure'] ?? false)) { |
|
98
|
1 |
|
$builder->noFailure(); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
1 |
|
if ($config['stop_when_empty'] ?? false) { |
|
102
|
|
|
$builder->stopWhenEmpty(); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
1 |
|
if ($config['max'] ?? false) { |
|
106
|
1 |
|
$builder->max($config['max']); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
1 |
|
if ($config['memory'] ?? false) { |
|
110
|
1 |
|
$builder->memory($this->convertToBytes($config['memory'])); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
1 |
|
if ($config['handler'] ?? false) { |
|
114
|
|
|
$builder->handler($config['handler']); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
1 |
|
if ($config['auto_handle'] ?? false) { |
|
118
|
|
|
$builder->jobProcessor(); |
|
119
|
|
|
} |
|
120
|
1 |
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Convert the given string value in bytes. |
|
124
|
|
|
* |
|
125
|
|
|
* @param string $value |
|
126
|
|
|
* |
|
127
|
|
|
* @return int |
|
128
|
|
|
*/ |
|
129
|
1 |
|
public function convertToBytes(string $value): int |
|
130
|
|
|
{ |
|
131
|
1 |
|
$value = strtolower(trim($value)); |
|
132
|
1 |
|
$unit = substr($value, -1); |
|
133
|
1 |
|
$bytes = (int) $value; |
|
134
|
|
|
|
|
135
|
|
|
switch ($unit) { |
|
136
|
1 |
|
case 't': $bytes *= 1024; |
|
137
|
|
|
// no break |
|
138
|
1 |
|
case 'g': $bytes *= 1024; |
|
139
|
|
|
// no break |
|
140
|
1 |
|
case 'm': $bytes *= 1024; |
|
141
|
|
|
// no break |
|
142
|
1 |
|
case 'k': $bytes *= 1024; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
1 |
|
return $bytes; |
|
146
|
|
|
} |
|
147
|
|
|
} |
|
148
|
|
|
|