|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Simple\Queue; |
|
6
|
|
|
|
|
7
|
|
|
use RuntimeException; |
|
8
|
|
|
use InvalidArgumentException; |
|
9
|
|
|
use Simple\Queue\Serializer\BaseSerializer; |
|
10
|
|
|
use Simple\Queue\Serializer\SerializerInterface; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Class Config |
|
14
|
|
|
* @package Simple\Queue |
|
15
|
|
|
*/ |
|
16
|
|
|
class Config |
|
17
|
|
|
{ |
|
18
|
|
|
/** @var int */ |
|
19
|
|
|
private int $redeliveryTimeInSeconds = 180; |
|
20
|
|
|
|
|
21
|
|
|
/** @var int */ |
|
22
|
|
|
private int $numberOfAttemptsBeforeFailure = 5; |
|
23
|
|
|
|
|
24
|
|
|
/** @var array */ |
|
25
|
|
|
private array $jobs = []; |
|
26
|
|
|
|
|
27
|
|
|
/** @var SerializerInterface|null */ |
|
28
|
|
|
private ?SerializerInterface $serializer = null; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Config constructor. |
|
32
|
|
|
*/ |
|
33
|
24 |
|
public function __construct() |
|
34
|
|
|
{ |
|
35
|
24 |
|
if ($this->serializer === null) { |
|
36
|
24 |
|
$this->serializer = new BaseSerializer(); |
|
37
|
|
|
} |
|
38
|
24 |
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @return static |
|
42
|
|
|
*/ |
|
43
|
20 |
|
public static function getDefault(): self |
|
44
|
|
|
{ |
|
45
|
20 |
|
return new self; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @return int |
|
50
|
|
|
*/ |
|
51
|
4 |
|
public function getRedeliveryTimeInSeconds(): int |
|
52
|
|
|
{ |
|
53
|
4 |
|
return $this->redeliveryTimeInSeconds; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @return int |
|
58
|
|
|
*/ |
|
59
|
2 |
|
public function getNumberOfAttemptsBeforeFailure(): int |
|
60
|
|
|
{ |
|
61
|
2 |
|
return $this->numberOfAttemptsBeforeFailure; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @return array |
|
66
|
|
|
*/ |
|
67
|
2 |
|
public function getJobs(): array |
|
68
|
|
|
{ |
|
69
|
2 |
|
return $this->jobs; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @param string $key |
|
74
|
|
|
* @return string|null |
|
75
|
|
|
*/ |
|
76
|
4 |
|
public function getJob(string $key): ?string |
|
77
|
|
|
{ |
|
78
|
4 |
|
if ($this->hasJob($key) === false) { |
|
79
|
1 |
|
throw new InvalidArgumentException(sprintf('Job "%s" doesn\'t exists.', $key)); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
3 |
|
return $this->jobs[$key]; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @param string $key |
|
87
|
|
|
* @return bool |
|
88
|
|
|
*/ |
|
89
|
7 |
|
public function hasJob(string $key): bool |
|
90
|
|
|
{ |
|
91
|
7 |
|
return isset($this->jobs[$key]); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @param int $seconds |
|
96
|
|
|
* @return $this |
|
97
|
|
|
*/ |
|
98
|
1 |
|
public function changeRedeliveryTimeInSeconds(int $seconds): self |
|
99
|
|
|
{ |
|
100
|
1 |
|
$this->redeliveryTimeInSeconds = $seconds; |
|
101
|
|
|
|
|
102
|
1 |
|
return $this; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* @param int $attempt |
|
107
|
|
|
* @return $this |
|
108
|
|
|
*/ |
|
109
|
1 |
|
public function changeNumberOfAttemptsBeforeFailure(int $attempt): self |
|
110
|
|
|
{ |
|
111
|
1 |
|
$this->numberOfAttemptsBeforeFailure = $attempt; |
|
112
|
|
|
|
|
113
|
1 |
|
return $this; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @param string $alias |
|
118
|
|
|
* @param string $class |
|
119
|
|
|
* @return $this |
|
120
|
|
|
*/ |
|
121
|
6 |
|
public function registerJobAlias(string $alias, string $class): self |
|
122
|
|
|
{ |
|
123
|
6 |
|
if ((bool)preg_match('/^[a-zA-Z0-9_.-]*$/u', $alias) === false) { |
|
124
|
1 |
|
throw new InvalidArgumentException(sprintf('The job alias "%s" contains invalid characters.', $alias)); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
5 |
|
if (isset($this->jobs[$alias])) { |
|
128
|
1 |
|
throw new RuntimeException(sprintf('Job "%s" is already registered in the jobs.', $alias)); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
// TODO: job class |
|
132
|
|
|
|
|
133
|
5 |
|
$this->jobs[$alias] = $class; |
|
134
|
|
|
|
|
135
|
5 |
|
return $this; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* @return SerializerInterface |
|
140
|
|
|
*/ |
|
141
|
4 |
|
public function getSerializer(): SerializerInterface |
|
142
|
|
|
{ |
|
143
|
4 |
|
return $this->serializer; |
|
|
|
|
|
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* @param SerializerInterface $serializer |
|
148
|
|
|
* @return $this |
|
149
|
|
|
*/ |
|
150
|
1 |
|
public function withSerializer(SerializerInterface $serializer): self |
|
151
|
|
|
{ |
|
152
|
1 |
|
$this->serializer = $serializer; |
|
153
|
|
|
|
|
154
|
1 |
|
return $this; |
|
155
|
|
|
} |
|
156
|
|
|
} |
|
157
|
|
|
|