1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Prozorov\DataVerification; |
6
|
|
|
|
7
|
|
|
use Prozorov\DataVerification\Contracts\CodeRepositoryInterface; |
8
|
|
|
use Prozorov\DataVerification\Factories\{TransportFactory, MessageFactory}; |
9
|
|
|
use Prozorov\DataVerification\Messages\SmsMessage; |
10
|
|
|
use Prozorov\DataVerification\Transport\DebugTransport; |
11
|
|
|
use Psr\EventDispatcher\EventDispatcherInterface; |
12
|
|
|
use Prozorov\DataVerification\Events\AbstractEvent; |
13
|
|
|
use Psr\Container\ContainerInterface; |
14
|
|
|
|
15
|
|
|
class Configuration |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var ContainerInterface $container |
19
|
|
|
*/ |
20
|
|
|
protected $container; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var EventDispatcherInterface $eventDispatcher |
24
|
|
|
*/ |
25
|
|
|
protected $eventDispatcher; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var CodeRepositoryInterface $codeRepo |
29
|
|
|
*/ |
30
|
|
|
protected $codeRepo; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var MessageFactory $messageFactory |
34
|
|
|
*/ |
35
|
|
|
protected $messageFactory; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var TransportFactory $transportFactory |
39
|
|
|
*/ |
40
|
|
|
protected $transportFactory; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var array $resolve |
44
|
|
|
*/ |
45
|
|
|
protected $resolved = []; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var array $passwords |
49
|
|
|
*/ |
50
|
|
|
protected $passwords = []; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* loadConfig. |
54
|
|
|
* |
55
|
|
|
* @access public |
56
|
|
|
* @param array $config Default: [] |
57
|
|
|
* @return void |
58
|
|
|
*/ |
59
|
13 |
|
public function __construct( |
60
|
|
|
ContainerInterface $container = null, |
61
|
|
|
array $config = [], |
62
|
|
|
EventDispatcherInterface $eventDispatcher = null |
63
|
|
|
) |
64
|
|
|
{ |
65
|
13 |
|
$this->container = $container; |
66
|
13 |
|
$this->eventDispatcher = $eventDispatcher; |
67
|
|
|
|
68
|
13 |
|
if (empty($config['code_repository'])) { |
69
|
|
|
throw new \InvalidArgumentException('Укажите класс-репозиторий данных'); |
70
|
|
|
} |
71
|
|
|
|
72
|
13 |
|
$this->codeRepo = $config['code_repository']; |
73
|
|
|
|
74
|
13 |
|
$transportConfig = $config['transport'] ?? ['sms' => DebugTransport::class]; |
75
|
13 |
|
$this->transportFactory = new TransportFactory($transportConfig, $container); |
76
|
|
|
|
77
|
13 |
|
$messageConfig = $config['messages'] ?? ['sms' => SmsMessage::class]; |
78
|
13 |
|
$this->messageFactory = new MessageFactory($messageConfig, $container); |
79
|
|
|
|
80
|
13 |
|
if (! empty($config['passwords'])) { |
81
|
1 |
|
foreach ($config['passwords'] as $code => $passwordConfig) { |
82
|
1 |
|
$this->passwords[$code] = $this->getPreparedPasswordConfig($passwordConfig); |
83
|
|
|
} |
84
|
|
|
} else { |
85
|
12 |
|
$this->passwords['default'] = [ |
86
|
12 |
|
'allowed_symbols' => $config['allowed_symbols'] ?? range(0, 9), |
87
|
12 |
|
'pass_length' => $config['pass_length'] ?? 4, |
88
|
12 |
|
'creation_code_threshold' => $config['creation_code_threshold'] ?? 60, |
89
|
12 |
|
'limit_per_hour' => $config['limit_per_hour'] ?? 60, |
90
|
12 |
|
'attempts' => $config['attempts'] ?? 3, |
91
|
12 |
|
'password_validation_period' => $config['password_validation_period'] ?? 3600, |
92
|
|
|
]; |
93
|
|
|
} |
94
|
13 |
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Get the value of codeRepo |
98
|
|
|
* |
99
|
|
|
* @access public |
100
|
|
|
* @return CodeRepositoryInterface |
101
|
|
|
*/ |
102
|
12 |
|
public function getCodeRepo(): CodeRepositoryInterface |
103
|
|
|
{ |
104
|
12 |
|
if (is_object($this->codeRepo) && ($this->codeRepo instanceof CodeRepositoryInterface)) { |
105
|
12 |
|
return $this->codeRepo; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return $this->getResolved('code_repository', $this->codeRepo); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* getAllowedSymbols. |
113
|
|
|
* |
114
|
|
|
* @access public |
115
|
|
|
* @param string $code Default: 'default' |
116
|
|
|
* @return array |
117
|
|
|
*/ |
118
|
4 |
|
public function getAllowedSymbols(string $code = 'default'): array |
119
|
|
|
{ |
120
|
4 |
|
return $this->passwords[$code]['allowed_symbols']; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* getPassLength. |
125
|
|
|
* |
126
|
|
|
* @access public |
127
|
|
|
* @param string $code Default: 'default' |
128
|
|
|
* @return int |
129
|
|
|
*/ |
130
|
5 |
|
public function getPassLength(string $code = 'default'): int |
131
|
|
|
{ |
132
|
5 |
|
return $this->passwords[$code]['pass_length']; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Returns seconds threshold |
137
|
|
|
* |
138
|
|
|
* @access public |
139
|
|
|
* @param string $code Default: 'default' |
140
|
|
|
* @return integer |
141
|
|
|
*/ |
142
|
7 |
|
public function getCreationCodeThreshold(string $code = 'default'): int |
143
|
|
|
{ |
144
|
7 |
|
return $this->passwords[$code]['creation_code_threshold']; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* getLimitPerHour. |
149
|
|
|
* |
150
|
|
|
* @access public |
151
|
|
|
* @param string $code Default: 'default' |
152
|
|
|
* @return int |
153
|
|
|
*/ |
154
|
2 |
|
public function getLimitPerHour(string $code = 'default'): int |
155
|
|
|
{ |
156
|
2 |
|
return $this->passwords[$code]['limit_per_hour']; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* getAttempts. |
161
|
|
|
* |
162
|
|
|
* @access public |
163
|
|
|
* @param string $code Default: 'default' |
164
|
|
|
* @return int |
165
|
|
|
*/ |
166
|
3 |
|
public function getAttempts(string $code = 'default'): int |
167
|
|
|
{ |
168
|
3 |
|
return $this->passwords[$code]['attempts']; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Returns seconds threshold |
173
|
|
|
* |
174
|
|
|
* @access public |
175
|
|
|
* @param string $code Default: 'default' |
176
|
|
|
* @return integer |
177
|
|
|
*/ |
178
|
6 |
|
public function getPasswordValidationPeriod(string $code = 'default'): int |
179
|
|
|
{ |
180
|
6 |
|
return $this->passwords[$code]['password_validation_period']; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* getTransportFactory. |
185
|
|
|
* |
186
|
|
|
* @access public |
187
|
|
|
* @return TransportFactory |
188
|
|
|
*/ |
189
|
1 |
|
public function getTransportFactory(): TransportFactory |
190
|
|
|
{ |
191
|
1 |
|
return $this->transportFactory; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* getMessageFactory. |
196
|
|
|
* |
197
|
|
|
* @access public |
198
|
|
|
* @return MessageFactory |
199
|
|
|
*/ |
200
|
|
|
public function getMessageFactory(): MessageFactory |
201
|
|
|
{ |
202
|
|
|
return $this->messageFactory; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* emitEvent. |
207
|
|
|
* |
208
|
|
|
* @access public |
209
|
|
|
* @param AbstractEvent $event |
210
|
|
|
* @return void |
211
|
|
|
*/ |
212
|
5 |
|
public function emitEvent(AbstractEvent $event): void |
213
|
|
|
{ |
214
|
5 |
|
if (empty($this->eventDispatcher)) { |
215
|
3 |
|
return; |
216
|
|
|
} |
217
|
|
|
|
218
|
2 |
|
$this->eventDispatcher->dispatch($event); |
219
|
2 |
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* getResolved. |
223
|
|
|
* |
224
|
|
|
* @access protected |
225
|
|
|
* @param string $definition |
226
|
|
|
* @param mixed $implementation |
227
|
|
|
* @return mixed |
228
|
|
|
*/ |
229
|
|
|
protected function getResolved(string $definition, $implementation) |
230
|
|
|
{ |
231
|
|
|
if (empty($this->resolved[$definition])) { |
232
|
|
|
$this->resolve($definition, $implementation); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
return $this->resolved[$definition]; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* resolve. |
240
|
|
|
* |
241
|
|
|
* @access protected |
242
|
|
|
* @param string $definition |
243
|
|
|
* @param mixed $implementation |
244
|
|
|
* @return void |
245
|
|
|
*/ |
246
|
|
|
protected function resolve(string $definition, $implementation): void |
247
|
|
|
{ |
248
|
|
|
if (is_callable($implementation)) { |
249
|
|
|
$this->resolved[$definition] = $implementation(); |
250
|
|
|
|
251
|
|
|
return; |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
if (is_object($implementation)) { |
255
|
|
|
$this->resolved[$definition] = $implementation; |
256
|
|
|
|
257
|
|
|
return; |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
$this->resolved[$definition] = $this->container->get($implementation); |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
/** |
264
|
|
|
* getPreparedPasswordConfig. |
265
|
|
|
* |
266
|
|
|
* @access protected |
267
|
|
|
* @param array $passwordConfig |
268
|
|
|
* @return array |
269
|
|
|
*/ |
270
|
1 |
|
protected function getPreparedPasswordConfig(array $passwordConfig): array |
271
|
|
|
{ |
272
|
|
|
$default = [ |
273
|
1 |
|
'pass_length' => 4, |
274
|
|
|
'creation_code_threshold' => 60, |
275
|
|
|
'limit_per_hour' => 10, |
276
|
|
|
'attempts' => 3, |
277
|
|
|
'password_validation_period' => 3600, |
278
|
|
|
]; |
279
|
|
|
|
280
|
1 |
|
return array_merge($default, $passwordConfig); |
281
|
|
|
} |
282
|
|
|
} |
283
|
|
|
|