Completed
Push — master ( ae5e03...0447ee )
by Jeroen
10:35 queued 04:37
created

Constraints/PasswordRestrictionsValidator.php (5 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Kunstmaan\AdminBundle\Validator\Constraints;
4
5
use Symfony\Component\Validator\Constraint;
6
use Symfony\Component\Validator\ConstraintValidator;
7
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
8
9
/**
10
 * Class PasswordRestrictionsValidator
11
 */
12
class PasswordRestrictionsValidator extends ConstraintValidator
13
{
14
    /**
15
     * @var int
16
     */
17
    private $minDigits;
18
19
    /**
20
     * @var int
21
     */
22
    private $minUppercase;
23
24
    /**
25
     * @var int
26
     */
27
    private $minSpecialCharacters;
28
29
    /**
30
     * @var int
31
     */
32
    private $minLength;
33
34
    /**
35
     * @var int
36
     */
37
    private $maxLength;
38
39
    /**
40
     * PasswordRestrictionsValidator constructor.
41
     *
42
     * @param int $minDigits
43
     * @param int $minUpperCase
44
     * @param int $minSpecialCharacters
45
     * @param int $minLength
46
     * @param int $maxLength
47
     */
48 20
    public function __construct($minDigits, $minUpperCase, $minSpecialCharacters, $minLength, $maxLength)
49
    {
50 20
        $this->minDigits = $minDigits;
51 20
        $this->minUppercase = $minUpperCase;
52 20
        $this->minSpecialCharacters = $minSpecialCharacters;
53 20
        $this->minLength = $minLength;
54 20
        $this->maxLength = $maxLength;
55 20
    }
56
57
    /**
58
     * Checks if the passed value is valid.
59
     *
60
     * @param string     $value      The value that should be validated
61
     * @param Constraint $constraint The constraint for the validation
62
     */
63 20
    public function validate($value, Constraint $constraint)
64
    {
65 20
        if (!$constraint instanceof PasswordRestrictions) {
66
            throw new UnexpectedTypeException($constraint, __NAMESPACE__ . '\PasswordRestrictions');
67
        }
68
69 20
        if (null === $value) {
70
            return;
71
        }
72
73 20
        if (null !== $this->minLength) {
74 12
            $this->validateMinLength($value);
75
        }
76
77 20
        if (null !== $this->maxLength) {
78 11
            $this->validateMaxLength($value);
79
        }
80
81 20
        if (null !== $this->minDigits) {
82 8
            $this->validateMinDigits($value);
83
        }
84
85 20
        if (null !== $this->minUppercase) {
86 8
            $this->validateMinUppercase($value);
87
        }
88
89 20
        if (null !== $this->minSpecialCharacters) {
90 8
            $this->validateMinSpecialCharacters($value);
91
        }
92 20
    }
93
94
    /**
95
     * @param string $value
96
     */
97 12 View Code Duplication
    private function validateMinLength($value)
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
98
    {
99 12
        if (\strlen($value) < $this->minLength) {
100 4
            $this->context->buildViolation(PasswordRestrictions::MESSAGE_MIN_LENGTH)
101 4
                ->setParameter('{{ min_length }}', $this->minLength)
102 4
                ->setCode(PasswordRestrictions::INVALID_MIN_LENGTH_ERROR)
103 4
                ->addViolation();
104
        }
105 12
    }
106
107
    /**
108
     * @param string $value
109
     */
110 11 View Code Duplication
    private function validateMaxLength($value)
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
111
    {
112 11
        if (\strlen($value) > $this->maxLength) {
113 3
            $this->context->buildViolation(PasswordRestrictions::MESSAGE_MAX_LENGTH)
114 3
                ->setParameter('{{ max_length }}', $this->maxLength)
115 3
                ->setCode(PasswordRestrictions::INVALID_MAX_LENGTH_ERROR)
116 3
                ->addViolation();
117
        }
118 11
    }
119
120
    /**
121
     * @param string $value
122
     */
123 8 View Code Duplication
    private function validateMinDigits($value)
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
124
    {
125 8
        if (preg_match_all('/\d/', $value) < $this->minDigits) {
126 2
            $this->context->buildViolation(PasswordRestrictions::MESSAGE_MIN_DIGITS)
127 2
                ->setParameter('{{ min_digits }}', $this->minDigits)
128 2
                ->setCode(PasswordRestrictions::INVALID_MIN_DIGITS_ERROR)
129 2
                ->addViolation();
130
        }
131 8
    }
132
133
    /**
134
     * @param string $value
135
     */
136 8 View Code Duplication
    private function validateMinUppercase($value)
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
137
    {
138 8
        if (preg_match_all('/[A-Z]/', $value) < $this->minUppercase) {
139 2
            $this->context->buildViolation(PasswordRestrictions::MESSAGE_MIN_UPPERCASE)
140 2
                ->setParameter('{{ min_uppercase }}', $this->minUppercase)
141 2
                ->setCode(PasswordRestrictions::INVALID_MIN_UPPERCASE_ERROR)
142 2
                ->addViolation();
143
        }
144 8
    }
145
146
    /**
147
     * @param string $value
148
     */
149 8 View Code Duplication
    private function validateMinSpecialCharacters($value)
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
150
    {
151 8
        if (preg_match_all('/[^a-zA-Z0-9]/', $value) < $this->minSpecialCharacters) {
152 2
            $this->context->buildViolation(PasswordRestrictions::MESSAGE_MIN_SPECIAL_CHARACTERS)
153 2
                ->setParameter('{{ min_special_characters }}', $this->minSpecialCharacters)
154 2
                ->setCode(PasswordRestrictions::INVALID_MIN_SPECIAL_CHARACTERS_ERROR)
155 2
                ->addViolation();
156
        }
157 8
    }
158
}
159