Completed
Push — master ( e141a5...5d3704 )
by Antonio Carlos
02:55 queued 25s
created

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