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

PasswordStrengthHelper   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 17
c 3
b 0
f 0
dl 0
loc 34
ccs 13
cts 13
cp 1
rs 10
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A validatePassword() 0 3 1
A getConstraints() 0 16 2
A __construct() 0 3 1
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
}