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