Criteria::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 3
crap 1
1
<?php
2
3
namespace DeGraciaMathieu\Riddler;
4
5
use DeGraciaMathieu\Riddler\Contracts;
6
7
class Criteria
8
{
9
10
    protected $name;
11
    protected $dictionary;
12
    protected $occurrence;
13
14 30
    public function __construct($name, Contracts\Dictionary $dictionary, Contracts\Occurrence $occurrence)
15
    {
16 30
        $this->dictionary = $dictionary;
17 30
        $this->occurrence = $occurrence;
18 30
        $this->setName($name);
19 30
    }
20
21 6
    public function getName()
22
    {
23 6
        return $this->name;
24
    }
25
    
26 30
    public function setName($name)
27
    {
28 30
        if (! $name) {
29 29
             $name = $this->dictionary->getName() . '_' . $this->occurrence->getName();
30 29
        }
31
32 30
        return $this->name = $name;
33
    }
34
35 14
    public function build()
36
    {
37 14
        $dictionary = $this->dictionary->handle();
38 14
        $size = $this->occurrence->size();
39
40 14
        $tmp = [];
41
42 14
        for ($i=0; $i < $size; $i++) {
43 14
            shuffle($dictionary);
44
45 14
            $tmp[] = $dictionary[0];
46 14
        }
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