Completed
Push — master ( e62fa2...4391c3 )
by Kristof
133:20 queued 117:59
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
    public function __construct($minDigits, $minUpperCase, $minSpecialCharacters, $minLength, $maxLength)
49
    {
50
        $this->minDigits = $minDigits;
51
        $this->minUppercase = $minUpperCase;
52
        $this->minSpecialCharacters = $minSpecialCharacters;
53
        $this->minLength = $minLength;
54
        $this->maxLength = $maxLength;
55
    }
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
    public function validate($value, Constraint $constraint)
64
    {
65
        if (!$constraint instanceof PasswordRestrictions) {
66
            throw new UnexpectedTypeException($constraint, __NAMESPACE__ . '\PasswordRestrictions');
67
        }
68
69
        if (null === $value) {
70
            return;
71
        }
72
73
        if (null !== $this->minLength) {
74
            $this->validateMinLength($value);
75
        }
76
77
        if (null !== $this->maxLength) {
78
            $this->validateMaxLength($value);
79
        }
80
81
        if (null !== $this->minDigits) {
82
            $this->validateMinDigits($value);
83
        }
84
85
        if (null !== $this->minUppercase) {
86
            $this->validateMinUppercase($value);
87
        }
88
89
        if (null !== $this->minSpecialCharacters) {
90
            $this->validateMinSpecialCharacters($value);
91
        }
92
    }
93
94
    /**
95
     * @param string $value
96
     */
97 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
        if (strlen($value) < $this->minLength) {
100
            $this->context->buildViolation(PasswordRestrictions::MESSAGE_MIN_LENGTH)
101
                ->setParameter('{{ min_length }}', $this->minLength)
102
                ->setCode(PasswordRestrictions::INVALID_MIN_LENGTH_ERROR)
103
                ->addViolation();
104
        }
105
    }
106
107
    /**
108
     * @param string $value
109
     */
110 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
        if (strlen($value) > $this->maxLength) {
113
            $this->context->buildViolation(PasswordRestrictions::MESSAGE_MAX_LENGTH)
114
                ->setParameter('{{ max_length }}', $this->maxLength)
115
                ->setCode(PasswordRestrictions::INVALID_MAX_LENGTH_ERROR)
116
                ->addViolation();
117
        }
118
    }
119
120
    /**
121
     * @param string $value
122
     */
123 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
        if (preg_match_all('/\d/', $value) < $this->minDigits) {
126
            $this->context->buildViolation(PasswordRestrictions::MESSAGE_MIN_DIGITS)
127
                ->setParameter('{{ min_digits }}', $this->minDigits)
128
                ->setCode(PasswordRestrictions::INVALID_MIN_DIGITS_ERROR)
129
                ->addViolation();
130
        }
131
    }
132
133
    /**
134
     * @param string $value
135
     */
136 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
        if (preg_match_all('/[A-Z]/', $value) < $this->minUppercase) {
139
            $this->context->buildViolation(PasswordRestrictions::MESSAGE_MIN_UPPERCASE)
140
                ->setParameter('{{ min_uppercase }}', $this->minUppercase)
141
                ->setCode(PasswordRestrictions::INVALID_MIN_UPPERCASE_ERROR)
142
                ->addViolation();
143
        }
144
    }
145
146
    /**
147
     * @param string $value
148
     */
149 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
        if (preg_match_all('/[^a-zA-Z0-9]/', $value) < $this->minSpecialCharacters) {
152
            $this->context->buildViolation(PasswordRestrictions::MESSAGE_MIN_SPECIAL_CHARACTERS)
153
                ->setParameter('{{ min_special_characters }}', $this->minSpecialCharacters)
154
                ->setCode(PasswordRestrictions::INVALID_MIN_SPECIAL_CHARACTERS_ERROR)
155
                ->addViolation();
156
        }
157
    }
158
}
159