Failed Conditions
Push — issue#666 ( 82e9d5...91903a )
by Guilherme
08:00
created

PasswordHintService::getPasswordRequirements()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 29
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 5.009

Importance

Changes 0
Metric Value
cc 5
eloc 18
nc 5
nop 0
dl 0
loc 29
ccs 13
cts 14
cp 0.9286
crap 5.009
rs 8.439
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of the login-cidadao project or it's bundles.
4
 *
5
 * (c) Guilherme Donato <guilhermednt on github>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace LoginCidadao\CoreBundle\Service;
12
13
use Rollerworks\Bundle\PasswordStrengthBundle\Validator\Constraints\PasswordRequirements;
14
use Symfony\Component\Translation\TranslatorInterface;
15
use Symfony\Component\Validator\Constraint;
16
use Symfony\Component\Validator\Mapping\ClassMetadata;
17
use Symfony\Component\Validator\Validator\ValidatorInterface;
18
19
class PasswordHintService
20
{
21
    const TRANS_TYPE_RANGE = 'range';
22
    const TRANS_TYPE_MIN = 'min';
23
    const TRANS_TYPE_MAX = 'max';
24
    const TRANS_TYPE_NO_LIMIT = 'no_limit';
25
    const TRANS_WITH_REQS = 'with_reqs';
26
    const TRANS_NO_REQS = 'no_reqs';
27
28
    /** @var ValidatorInterface */
29
    private $validator;
30
31
    /** @var TranslatorInterface */
32
    private $translator;
33
34
    /** @var string */
35
    private $userClass;
36
37
    /**
38
     * PasswordHintService constructor.
39
     * @param ValidatorInterface $validator
40
     * @param TranslatorInterface $translator
41
     * @param string $userClass
42
     */
43 1
    public function __construct(ValidatorInterface $validator, TranslatorInterface $translator, $userClass)
44
    {
45 1
        $this->validator = $validator;
46 1
        $this->translator = $translator;
47 1
        $this->userClass = $userClass;
48 1
    }
49
50 1
    public function getPasswordRequirements()
51
    {
52
        $requirements = [
53 1
            'min' => 0,
54
            'max' => null,
55
            'requireLetters' => false,
56
            'requireNumbers' => false,
57
            'requireSpecialCharacter' => false,
58
        ];
59
60
        /** @var ClassMetadata $metadata */
61 1
        $metadata = $this->validator->getMetadataFor($this->userClass);
62
63 1
        if (!($metadata instanceof ClassMetadata) || false === $metadata->hasPropertyMetadata('plainPassword')) {
64
            return $requirements;
65
        }
66 1
        $propertyMetadata = $metadata->getPropertyMetadata('plainPassword');
67 1
        $constraints = [];
68 1
        foreach ($propertyMetadata as $propertymetadata) {
69 1
            $constraints = array_merge($constraints, $propertymetadata->getConstraints());
70
        }
71
72 1
        foreach ($constraints as $constraint) {
73 1
            $requirements = $this->checkMin($constraint, $requirements);
74 1
            $requirements = $this->checkMax($constraint, $requirements);
75 1
            $requirements = $this->checkCharacters($constraint, $requirements);
76
        }
77
78 1
        return $requirements;
79
    }
80
81 1
    public function getHintString(array $requirements = null)
82
    {
83 1
        if ($requirements === null) {
84 1
            $requirements = $this->getPasswordRequirements();
85
        }
86
87 1
        $reqs = [];
88 1
        if ($requirements['requireLetters']) {
89 1
            $reqs[] = $this->translator->trans('password_hint.requirements.letters');
90
        }
91 1
        if ($requirements['requireNumbers']) {
92 1
            $reqs[] = $this->translator->trans('password_hint.requirements.numbers');
93
        }
94 1
        if ($requirements['requireSpecialCharacter']) {
95
            $reqs[] = $this->translator->trans('password_hint.requirements.special');
96
        }
97
98 1
        if ($requirements['min'] > 0 && $requirements['max'] !== null) {
99 1
            $type = self::TRANS_TYPE_RANGE;
100
        } elseif ($requirements['min'] > 0 && $requirements['max'] === null) {
101
            $type = self::TRANS_TYPE_MIN;
102
        } elseif ($requirements['min'] <= 0 && $requirements['max'] !== null) {
103
            $type = self::TRANS_TYPE_MAX;
104
        } else {
105
            $type = self::TRANS_TYPE_NO_LIMIT;
106
        }
107
108 1
        if (count($reqs) > 1) {
109 1
            $reqString = sprintf(
110 1
                '%s %s %s',
111 1
                implode(', ', array_slice($reqs, 0, count($reqs) - 1)),
112 1
                $this->translator->trans('password_hint.and'),
113 1
                end($reqs)
114
            );
115
        } else {
116
            $reqString = reset($reqs);
117
        }
118
119 1
        $hasReqs = count($reqs) > 0 ? self::TRANS_WITH_REQS : self::TRANS_NO_REQS;
120
121
        $params = [
122 1
            '%reqs%' => $reqString,
123 1
            '%min%' => $requirements['min'],
124 1
            '%max%' => $requirements['max'],
125
        ];
126
127 1
        if ($type === self::TRANS_TYPE_NO_LIMIT && count($reqs) <= 0) {
128
            return false;
129
        }
130
131 1
        return $this->translator->trans("password_hint.{$type}.{$hasReqs}", $params);
132
    }
133
134 1
    private function checkMin(Constraint $constraint, array $requirements)
135
    {
136 1
        if (property_exists($constraint, 'min')) {
137 1
            $newMin = $constraint->min;
138 1
        } elseif (property_exists($constraint, 'minLength')) {
139 1
            $newMin = $constraint->minLength;
140
        } else {
141 1
            return $requirements;
142
        }
143 1
        if ($newMin > $requirements['min']) {
144 1
            $requirements['min'] = $newMin;
145
        }
146
147 1
        return $requirements;
148
    }
149
150 1
    private function checkMax(Constraint $constraint, array $requirements)
151
    {
152 1
        if (property_exists($constraint, 'max')) {
153 1
            $newMax = $constraint->max;
154
        } else {
155 1
            return $requirements;
156
        }
157 1
        if ($requirements['max'] === null || $newMax < $requirements['max']) {
158 1
            $requirements['max'] = $newMax;
159
        }
160
161 1
        return $requirements;
162
    }
163
164 1
    private function checkCharacters(Constraint $constraint, array $requirements)
165
    {
166 1
        if (!($constraint instanceof PasswordRequirements)) {
167 1
            return $requirements;
168
        }
169
170 1
        $requirements['requireLetters'] = $constraint->requireLetters;
171 1
        $requirements['requireNumbers'] = $constraint->requireNumbers;
172 1
        $requirements['requireSpecialCharacter'] = $constraint->requireSpecialCharacter;
173
174 1
        return $requirements;
175
    }
176
}
177