Completed
Push — master ( e50189...eaf36c )
by Marcel
02:55
created

PasswordStrengthHelper::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 2
b 0
f 0
nc 1
nop 2
dl 0
loc 3
ccs 3
cts 3
cp 1
crap 1
rs 10
1
<?php
2
3
namespace App\Security;
4
5
use Rollerworks\Component\PasswordStrength\Validator\Constraints\PasswordRequirements;
6
use Symfony\Component\Validator\Constraint;
7
use Symfony\Component\Validator\Constraints\NotCompromisedPassword;
8
use Symfony\Component\Validator\ConstraintViolationListInterface;
9
use Symfony\Component\Validator\Validator\ValidatorInterface;
10
11
class PasswordStrengthHelper {
12
    private $enableCompromisedCheck;
13
    private $validator;
14
15 2
    public function __construct(bool $enableCompromisedCheck, ValidatorInterface $validator) {
16 2
        $this->enableCompromisedCheck = $enableCompromisedCheck;
17 2
        $this->validator = $validator;
18 2
    }
19
20 2
    public function getConstraints() {
21
        $constraints = [
22 2
            new PasswordRequirements([
23 2
                'minLength' => 8,
24
                'requireLetters' => true,
25
                'requireCaseDiff' => true,
26
                'requireNumbers' => true,
27
                'requireSpecialCharacter' => true
28
            ])
29
        ];
30
31 2
        if($this->enableCompromisedCheck) {
32 2
            $constraints[] = new NotCompromisedPassword();
33
        }
34
        
35 2
        return $constraints;
36
    }
37
38
    /**
39
     * @param string $password
40
     * @return ConstraintViolationListInterface
41
     */
42 2
    public function validatePassword(string $password): ConstraintViolationListInterface {
43 2
        $constraints = $this->getConstraints();
44 2
        return $this->validator->validate($password, $constraints);
45
    }
46
}