Passed
Push — master ( 1ac390...22382b )
by DeGracia
02:33
created

Criteria::build()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 16
c 0
b 0
f 0
ccs 8
cts 8
cp 1
rs 9.4285
cc 2
eloc 8
nc 2
nop 0
crap 2
1
<?php
2
3
namespace DeGraciaMathieu\Riddler;
4
5
use DeGraciaMathieu\Riddler\Contracts;
6
7
class Criteria {
8
9
    protected $name;
10
	protected $dictionary;
11
	protected $occurrence;
12
13 30
    public function __construct($name, Contracts\Dictionary $dictionary, Contracts\Occurrence $occurrence)
14
    {
15 30
        $this->dictionary = $dictionary;
16 30
        $this->occurrence = $occurrence;
17 30
        $this->setName($name);
18 30
    }
19
20 6
    public function getName()
21
    {
22 6
        return $this->name;
23
    }
24
    
25 30
    public function setName($name)
26
    {
27 30
        if (! $name) {
28 29
             $name = $this->dictionary->getName() . '_' . $this->occurrence->getName();
29
        }
30
31 30
        return $this->name = $name;
32
    }
33
34 14
    public function build()
35
    {
36 14
        $dictionary = $this->dictionary->handle();
37 14
        $size = $this->occurrence->size();
38
39 14
        $tmp = [];
40
41 14
        for ($i=0; $i < $size; $i++) {
42
43 14
            shuffle($dictionary);
44
45 14
            $tmp[] = $dictionary[0];
46
        }
47
48 14
        return $tmp;
49
    }
50
51
    /**
52
     * Détermine si le mot de passe vérifie le critère
53
     *
54
     * @param string $password
55
     * @return boolean
56
     */
57 14
    public function passes($password)
58
    {
59 14
        $dictionary = $this->dictionary->handle();
60
61 14
        $regex = preg_quote(implode($dictionary), '#');
62
63 14
        $result = preg_match_all("#[" . $regex . "]#u", $password, $matches);
64
65 14
        return $this->occurrence->validate($result);
66
    }
67
}
68