Test Failed
Push — master ( a9de71...77d308 )
by Artem
01:39
created

Configuration::getCreationCodeThreshold()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
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 8
    public function __construct($container, array $config = [])
74
    {
75 8
        $this->container = $container;
76
77 8
        if (empty($config['code_repository'])) {
78
            throw new \InvalidArgumentException('Укажите класс-репозиторий данных');
79
        }
80
81 8
        $this->codeRepo = $config['code_repository'];
82
83 8
        $transportConfig = $config['transport_config'] ?? ['sms' => DebugTransport::class];
84 8
        $this->transportFactory = new TransportFactory($transportConfig);
85
86 8
        $messageConfig = $config['messages'] ?? ['sms' => SmsMessage::class];
87 8
        $this->messageFactory = new MessageFactory($messageConfig);
88
89 8
        $this->allowedSymbols = $config['allowed_symbols'] ?? range(0, 9);
90 8
        $this->passLength = $config['pass_length'] ?? 4;
91 8
        $this->creationCodeThreshold = $config['creation_code_threshold'] ?? 60;
92 8
        $this->limitPerHour = $config['limit_per_hour'] ?? 60;
93 8
        $this->attempts = $config['attempts'] ?? 3;
94 8
        $this->passwordValidationPeriod = $config['password_validation_period'] ?? 3600;
95 8
    }
96
97
    /**
98
     * Get the value of codeRepo
99
     *
100
     * @access	public
101
     * @return	CodeRepositoryInterface
102
     */
103 8
    public function getCodeRepo(): CodeRepositoryInterface
104
    {
105 8
        if (is_object($this->codeRepo) && ($this->codeRepo instanceof CodeRepositoryInterface)) {
106 8
            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 2
    public function getAllowedSymbols(): array
119
    {
120 2
        return $this->allowedSymbols;
121
    }
122
123
    /**
124
     * getPassLength.
125
     *
126
     * @access	public
127
     * @return	int
128
     */
129 2
    public function getPassLength(): int
130
    {
131 2
        return $this->passLength;
132
    }
133
134
    /**
135
     * Returns seconds threshold
136
     * 
137
     * @access	public
138
     * @return	integer
139
     */
140 4
    public function getCreationCodeThreshold(): int
141
    {
142 4
        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 2
    public function getAttempts(): int
163
    {
164 2
        return $this->attempts;
165
    }
166
167
    /**
168
     * Returns seconds threshold
169
     * 
170
     * @access	public
171
     * @return	integer
172
     */
173 4
    public function getPasswordValidationPeriod(): int
174
    {
175 4
        return $this->passwordValidationPeriod;
176
    }
177
178
    /**
179
     * getTransportFactory.
180
     *
181
     * @access	public
182
     * @return	TransportFactory
183
     */
184
    public function getTransportFactory(): TransportFactory
185
    {
186
        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