Passed
Push — master ( 5f130d...e16573 )
by Antonio Carlos
02:26
created

Recovery::collectionFunction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
3
namespace PragmaRX\Recovery;
4
5
use PragmaRX\Random\Random;
6
7
class Recovery
8
{
9
    protected $codes = [];
10
11
    protected $count = 8;
12
13
    protected $blocks = 2;
14
15
    protected $chars = 10;
16
17
    protected $random;
18
19
    protected $blockSeparator = '-';
20
21
    private $collectionFunction = 'collect';
22
23
    /**
24
     * Recovery constructor.
25
     *
26
     * @param Random $random
27
     */
28 11
    public function __construct(Random $random = null)
29
    {
30 11
        if (is_null($random)) {
31 1
            $random = new Random();
32
        }
33
34 11
        $this->random = $random;
35 11
    }
36
37
    /**
38
     * Set to alpha codes.
39
     *
40
     * @return Recovery
41
     */
42 1
    public function alpha()
43
    {
44 1
        $this->random->alpha();
45
46 1
        return $this;
47
    }
48
49
    /**
50
     * Generate the recovery codes.
51
     *
52
     * @return array
53
     */
54 10
    protected function generate()
55
    {
56 10
        $this->reset();
57
58 10
        foreach (range(1, $this->getCount()) as $counter) {
59 10
            $this->codes[] = $this->generateBlocks();
60
        }
61
62 10
        return $this->codes;
63
    }
64
65
    /**
66
     * Generate all blocks.
67
     *
68
     * @return array
69
     */
70 10
    protected function generateBlocks()
71
    {
72 10
        $blocks = [];
73
74 10
        foreach (range(1, $this->getBlocks()) as $counter) {
75 10
            $blocks[] = $this->generateChars();
76
        }
77
78 10
        return implode($this->blockSeparator, $blocks);
79
    }
80
81
    /**
82
     * Generate random chars.
83
     *
84
     * @return string
85
     */
86 10
    protected function generateChars()
87
    {
88 10
        return $this->random->size($this->getChars())->get();
89
    }
90
91
    /**
92
     * Check if codes must be generated.
93
     *
94
     * @return bool
95
     */
96 10
    protected function mustGenerate()
97
    {
98 10
        return count($this->codes) == 0;
99
    }
100
101
    /**
102
     * Set lowercase codes state.
103
     *
104
     * @param bool $state
105
     * @return Recovery
106
     */
107 1
    public function lowercase($state = true)
108
    {
109 1
        $this->random->lowercase($state);
110
111 1
        return $this;
112
    }
113
114
    /**
115
     * Set the block separator.
116
     *
117
     * @param string $blockSeparator
118
     * @return Recovery
119
     */
120 1
    public function setBlockSeparator($blockSeparator)
121
    {
122 1
        $this->blockSeparator = $blockSeparator;
123
124 1
        return $this;
125
    }
126
127
    /**
128
     * Set the collection function.
129
     *
130
     * @param string $collectionFunction
131
     * @return Recovery
132
     */
133 1
    public function collectionFunction($collectionFunction)
134
    {
135 1
        $this->collectionFunction = $collectionFunction;
136
137 1
        return $this;
138
    }
139
140
    /**
141
     * Set uppercase codes state.
142
     *
143
     * @param bool $state
144
     * @return Recovery
145
     */
146 1
    public function uppercase($state = true)
147
    {
148 1
        $this->random->uppercase($state);
149
150 1
        return $this;
151
    }
152
153
    /**
154
     * Set mixedcase codes state.
155
     *
156
     * @return Recovery
157
     */
158 1
    public function mixedcase()
159
    {
160 1
        $this->random->mixedcase();
161
162 1
        return $this;
163
    }
164
165
    /**
166
     * Set to numeric codes.
167
     *
168
     * @return Recovery
169
     */
170 1
    public function numeric()
171
    {
172 1
        $this->random->numeric();
173
174 1
        return $this;
175
    }
176
177
    /**
178
     * Get an array of recovery codes.
179
     *
180
     * @return array
181
     */
182 10
    public function toArray()
183
    {
184 10
        if ($this->mustGenerate()) {
185 10
            return $this->generate();
186
        }
187
188 1
        return $this->getCodes();
189
    }
190
191
    /**
192
     * Get a collection of recovery codes.
193
     *
194
     * @return array
195
     * @throws \Exception
196
     */
197 2
    public function toCollection()
198
    {
199 2
        if (function_exists($this->collectionFunction)) {
200 1
            return call_user_func($this->collectionFunction, $this->toArray());
201
        }
202
203 1
        throw new \Exception("Function {$this->collectionFunction}() was not found. You probably need to install a suggested package?");
204
    }
205
206
    /**
207
     * Get a json of recovery codes.
208
     *
209
     * @return array
210
     */
211 1
    public function toJson()
212
    {
213 1
        return json_encode($this->toArray());
214
    }
215
216
    /**
217
     * Get the blocks size.
218
     *
219
     * @return int
220
     */
221 10
    public function getBlocks()
222
    {
223 10
        return $this->blocks;
224
    }
225
226
    /**
227
     * Get the chars count.
228
     *
229
     * @return int
230
     */
231 10
    public function getChars()
232
    {
233 10
        return $this->chars;
234
    }
235
236
    /**
237
     * Get the codes.
238
     *
239
     * @return array
240
     */
241 1
    public function getCodes()
242
    {
243 1
        return $this->codes;
244
    }
245
246
    /**
247
     * Get the codes count.
248
     *
249
     * @return int
250
     */
251 10
    public function getCount()
252
    {
253 10
        return $this->count;
254
    }
255
256
    /**
257
     * Reset generated codes.
258
     *
259
     */
260 10
    protected function reset()
261
    {
262 10
        $this->codes = [];
263 10
    }
264
265
    /**
266
     * Set the blocks size.
267
     *
268
     * @param int $blocks
269
     * @return Recovery
270
     */
271 5
    public function setBlocks($blocks)
272
    {
273 5
        $this->blocks = $blocks;
274
275 5
        $this->reset();
276
277 5
        return $this;
278
    }
279
280
    /**
281
     * Set the chars count.
282
     *
283
     * @param int $chars
284
     * @return Recovery
285
     */
286 4
    public function setChars($chars)
287
    {
288 4
        $this->chars = $chars;
289
290 4
        $this->reset();
291
292 4
        return $this;
293
    }
294
295
    /**
296
     * Set the codes count.
297
     *
298
     * @param int $count
299
     * @return Recovery
300
     */
301 6
    public function setCount($count)
302
    {
303 6
        $this->count = $count;
304
305 6
        $this->reset();
306
307 6
        return $this;
308
    }
309
}
310