Completed
Push — 2.1 ( f59067...a01579 )
by
unknown
21:13 queued 09:54
created

VerifyCodeGeneratorTrait::generateVerifyCode()   D

Complexity

Conditions 9
Paths 24

Size

Total Lines 26
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 9.5338

Importance

Changes 0
Metric Value
dl 0
loc 26
ccs 13
cts 16
cp 0.8125
rs 4.909
c 0
b 0
f 0
cc 9
eloc 17
nc 24
nop 0
crap 9.5338
1
<?php
2
/**
3
 * @link http://www.yiiframework.com/
4
 * @copyright Copyright (c) 2008 Yii Software LLC
5
 * @license http://www.yiiframework.com/license/
6
 */
7
8
namespace yii\captcha;
9
10
/**
11
 * VerifyCodeGeneratorTrait provides configurable implementation for [[DriverInterface::generateVerifyCode()]].
12
 * This trait should be used at the class, which implements [[DriverInterface]].
13
 *
14
 * @author Qiang Xue <[email protected]>
15
 * @author Paul Klimov <[email protected]>
16
 * @since 2.1.0
17
 */
18
trait VerifyCodeGeneratorTrait
19
{
20
    /**
21
     * @var int the minimum length for randomly generated word. Defaults to 6.
22
     */
23
    public $minLength = 6;
24
    /**
25
     * @var int the maximum length for randomly generated word. Defaults to 7.
26
     */
27
    public $maxLength = 7;
28
29
30
    /**
31
     * Generates new CAPTCHA code.
32
     * @return string CAPTCHA code.
33
     */
34 3
    public function generateVerifyCode()
35
    {
36 3
        if ($this->minLength > $this->maxLength) {
37
            $this->maxLength = $this->minLength;
38
        }
39 3
        if ($this->minLength < 3) {
40
            $this->minLength = 3;
41
        }
42 3
        if ($this->maxLength > 20) {
43
            $this->maxLength = 20;
44
        }
45 3
        $length = mt_rand($this->minLength, $this->maxLength);
46
47 3
        $letters = 'bcdfghjklmnpqrstvwxyz';
48 3
        $vowels = 'aeiou';
49 3
        $code = '';
50 3
        for ($i = 0; $i < $length; ++$i) {
51 3
            if ($i % 2 && mt_rand(0, 10) > 2 || !($i % 2) && mt_rand(0, 10) > 9) {
52 3
                $code .= $vowels[mt_rand(0, 4)];
53
            } else {
54 3
                $code .= $letters[mt_rand(0, 20)];
55
            }
56
        }
57
58 3
        return $code;
59
    }
60
}