Completed
Push — master ( 91fdab...75a7b9 )
by
unknown
13: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
namespace Kunstmaan\AdminBundle\Validator\Constraints;
3
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
 * @package Kunstmaan\AdminBundle\Validator\Constraints
12
 */
13
class PasswordRestrictionsValidator extends ConstraintValidator
14
{
15
16
    /**
17
     * @var integer
18
     */
19
    private $minDigits;
20
    /**
21
     * @var integer
22
     */
23
    private $minUppercase;
24
    /**
25
     * @var integer
26
     */
27
    private $minSpecialCharacters;
28
    /**
29
     * @var integer
30
     */
31
    private $minLength;
32
    /**
33
     * @var integer
34
     */
35
    private $maxLength;
36
37
    /**
38
     * PasswordRestrictionsValidator constructor.
39
     * @param int $minDigits
40
     * @param int $minUpperCase
41
     * @param int $minSpecialCharacters
42
     * @param int $minLength
43
     * @param int $maxLength
44
     */
45
    public function __construct( $minDigits, $minUpperCase, $minSpecialCharacters, $minLength, $maxLength )
46
    {
47
        $this->minDigits = $minDigits;
48
        $this->minUppercase = $minUpperCase;
49
        $this->minSpecialCharacters = $minSpecialCharacters;
50
        $this->minLength = $minLength;
51
        $this->maxLength = $maxLength;
52
    }
53
54
    /**
55
     * Checks if the passed value is valid.
56
     *
57
     * @param string $value The value that should be validated
58
     * @param Constraint $constraint The constraint for the validation
59
     */
60
    public function validate($value, Constraint $constraint)
61
    {
62
63
        if (!$constraint instanceof PasswordRestrictions) {
64
            throw new UnexpectedTypeException($constraint, __NAMESPACE__ . '\PasswordRestrictions');
65
        }
66
67
        if (null === $value ) {
68
            return;
69
        }
70
71
        if (null !== $this->minLength) {
72
            $this->validateMinLength($value);
73
        }
74
75
        if (null !== $this->maxLength) {
76
            $this->validateMaxLength($value);
77
        }
78
79
        if (null !== $this->minDigits) {
80
            $this->validateMinDigits($value);
81
        }
82
83
        if (null !== $this->minUppercase) {
84
            $this->validateMinUppercase($value);
85
        }
86
87
        if (null !== $this->minSpecialCharacters) {
88
            $this->validateMinSpecialCharacters($value);
89
        }
90
91
    }
92
93
    /**
94
     * @param string $value
95
     */
96 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...
97
    {
98
99
        if ( strlen($value) < $this->minLength) {
100
101
            $this->context->buildViolation(PasswordRestrictions::MESSAGE_MIN_LENGTH)
102
                ->setParameter('{{ min_length }}', $this->minLength)
103
                ->setCode(PasswordRestrictions::INVALID_MIN_LENGTH_ERROR)
104
                ->addViolation();
105
106
        }
107
108
    }
109
110
    /**
111
     * @param string $value
112
     */
113 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...
114
    {
115
116
        if ( strlen($value) > $this->maxLength) {
117
118
            $this->context->buildViolation(PasswordRestrictions::MESSAGE_MAX_LENGTH)
119
                ->setParameter('{{ max_length }}', $this->maxLength)
120
                ->setCode(PasswordRestrictions::INVALID_MAX_LENGTH_ERROR)
121
                ->addViolation();
122
123
        }
124
125
    }
126
127
    /**
128
     * @param string $value
129
     */
130 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...
131
    {
132
133
        if (preg_match_all('/\d/', $value) < $this->minDigits) {
134
135
            $this->context->buildViolation(PasswordRestrictions::MESSAGE_MIN_DIGITS)
136
                ->setParameter('{{ min_digits }}', $this->minDigits)
137
                ->setCode(PasswordRestrictions::INVALID_MIN_DIGITS_ERROR)
138
                ->addViolation();
139
140
        }
141
142
    }
143
144
    /**
145
     * @param string $value
146
     */
147 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...
148
    {
149
150
        if (preg_match_all('/[A-Z]/', $value ) < $this->minUppercase ) {
151
152
            $this->context->buildViolation(PasswordRestrictions::MESSAGE_MIN_UPPERCASE)
153
                ->setParameter('{{ min_uppercase }}', $this->minUppercase)
154
                ->setCode(PasswordRestrictions::INVALID_MIN_UPPERCASE_ERROR)
155
                ->addViolation();
156
157
        }
158
159
    }
160
161
    /**
162
     * @param string $value
163
     */
164 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...
165
    {
166
167
        if (preg_match_all('/[^a-zA-Z0-9]/', $value) < $this->minSpecialCharacters) {
168
169
            $this->context->buildViolation(PasswordRestrictions::MESSAGE_MIN_SPECIAL_CHARACTERS)
170
                ->setParameter('{{ min_special_characters }}', $this->minSpecialCharacters)
171
                ->setCode(PasswordRestrictions::INVALID_MIN_SPECIAL_CHARACTERS_ERROR)
172
                ->addViolation();
173
174
        }
175
176
    }
177
178
}