Completed
Push — master ( 4b4e40...af8835 )
by Craig
10:40 queued 04:02
created

ValidPasswordValidator::validate()   B

Complexity

Conditions 7
Paths 8

Size

Total Lines 25
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 7
eloc 14
nc 8
nop 2
dl 0
loc 25
rs 8.8333
c 1
b 1
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Zikula package.
7
 *
8
 * Copyright Zikula Foundation - https://ziku.la/
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Zikula\ZAuthModule\Validator\Constraints;
15
16
use Symfony\Component\Validator\Constraint;
17
use Symfony\Component\Validator\Constraints\Length;
18
use Symfony\Component\Validator\Constraints\NotCompromisedPassword;
19
use Symfony\Component\Validator\Constraints\Type;
20
use Symfony\Component\Validator\ConstraintValidator;
21
use Symfony\Component\Validator\ConstraintViolationListInterface;
22
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
23
use Symfony\Component\Validator\Validator\ValidatorInterface;
24
use Zikula\ExtensionsModule\Api\ApiInterface\VariableApiInterface;
25
use Zikula\ZAuthModule\ZAuthConstant;
26
27
class ValidPasswordValidator extends ConstraintValidator
28
{
29
    /**
30
     * @var VariableApiInterface
31
     */
32
    private $variableApi;
33
34
    /**
35
     * @var ValidatorInterface
36
     */
37
    private $validator;
38
39
    public function __construct(VariableApiInterface $variableApi, ValidatorInterface $validator)
40
    {
41
        $this->variableApi = $variableApi;
42
        $this->validator = $validator;
43
    }
44
45
    public function validate($value, Constraint $constraint)
46
    {
47
        if (!$constraint instanceof ValidPassword) {
48
            throw new UnexpectedTypeException($constraint, ValidPassword::class);
49
        }
50
        if (null === $value || '' === $value) {
51
            // user created without password
52
            // needs to set one during verification
53
            return;
54
        }
55
        $validators = [
56
            new Type('string'),
57
            new Length([
58
                'min' => $this->variableApi->get('ZikulaZAuthModule', ZAuthConstant::MODVAR_PASSWORD_MINIMUM_LENGTH, ZAuthConstant::PASSWORD_MINIMUM_LENGTH)
59
            ])
60
        ];
61
        if ($this->variableApi->get('ZikulaZAuthModule', ZAuthConstant::MODVAR_REQUIRE_NON_COMPROMISED_PASSWORD, ZAuthConstant::DEFAULT_REQUIRE_UNCOMPROMISED_PASSWORD)) {
62
            $validators[] = new NotCompromisedPassword();
63
        }
64
        /** @var ConstraintViolationListInterface $errors */
65
        $errors = $this->validator->validate($value, $validators);
66
        if (count($errors) > 0) {
67
            foreach ($errors as $error) {
68
                // this method forces the error to appear at the form input location instead of at the top of the form
69
                $this->context->buildViolation($error->getMessage())->addViolation();
70
            }
71
        }
72
    }
73
}
74