Test Setup Failed
Pull Request — master (#4271)
by Craig
09:06 queued 03:44
created

ValidUserFieldsValidator::validateUniqueUname()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
nc 2
nop 2
dl 0
loc 6
rs 10
c 1
b 0
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 - 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\UsersModule\Validator\Constraints;
15
16
use Symfony\Component\Validator\Constraint;
17
use Symfony\Component\Validator\ConstraintValidator;
18
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
19
use Symfony\Contracts\Translation\TranslatorInterface;
20
use Zikula\UsersModule\Constant as UsersConstant;
21
use Zikula\UsersModule\Entity\Repository\UserRepository;
22
use Zikula\UsersModule\Entity\RepositoryInterface\UserRepositoryInterface;
23
use Zikula\UsersModule\Entity\UserEntity;
24
25
class ValidUserFieldsValidator extends ConstraintValidator
26
{
27
    public const DUP_EMAIL_ALT_AUTH = 'DuplicateEmailOfAlternativeAuthMethod';
28
29
    /**
30
     * @var TranslatorInterface
31
     */
32
    private $translator;
33
34
    /**
35
     * @var UserRepository
36
     */
37
    private $userRepository;
38
39
    public function __construct(
40
        TranslatorInterface $translator,
41
        UserRepositoryInterface $userRepository
42
    ) {
43
        $this->translator = $translator;
44
        $this->userRepository = $userRepository;
0 ignored issues
show
Documentation Bug introduced by
$userRepository is of type Zikula\UsersModule\Entit...UserRepositoryInterface, but the property $userRepository was declared to be of type Zikula\UsersModule\Entit...pository\UserRepository. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
45
    }
46
47
    public function validate($data, Constraint $constraint)
48
    {
49
        if (!$constraint instanceof ValidUserFields) {
50
            throw new UnexpectedTypeException($constraint, ValidUserFields::class);
51
        }
52
        // ensure unique uname
53
        $this->validateUniqueUname($data['uname'], $data['uid'] ?? null);
54
55
        // users registering duplicate email with different authentication method are invalid
56
        if ($data instanceof UserEntity && $data->hasAttribute(UsersConstant::AUTHENTICATION_METHOD_ATTRIBUTE_KEY)) {
57
            $this->validateEmailWithAuth($data);
58
        }
59
    }
60
61
    private function validateUniqueUname(string $uname, ?int $uid = null): void
62
    {
63
        if ($this->userRepository->countDuplicateUnames($uname, $uid) > 0) {
64
            $this->context->buildViolation($this->translator->trans('The user name you entered (%userName%) has already been registered.', ['%userName%' => $uname], 'validators'))
65
                ->atPath('uname')
66
                ->addViolation();
67
        }
68
    }
69
70
    private function validateEmailWithAuth(UserEntity $data): void
71
    {
72
        $authMethod = $data->getAttributeValue(UsersConstant::AUTHENTICATION_METHOD_ATTRIBUTE_KEY);
73
        $existing = $this->userRepository->getByEmailAndAuthMethod($data['email'], $authMethod);
0 ignored issues
show
Bug introduced by
It seems like $data['email'] can also be of type null; however, parameter $email of Zikula\UsersModule\Entit...tByEmailAndAuthMethod() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

73
        $existing = $this->userRepository->getByEmailAndAuthMethod(/** @scrutinizer ignore-type */ $data['email'], $authMethod);
Loading history...
74
        if (count($existing) > 0) {
75
            $this->context->buildViolation($this->translator->trans('This email is in use by another authentication method. Please login with that method instead.', [], 'validators'))
76
                ->atPath('email')
77
                ->setCode(self::DUP_EMAIL_ALT_AUTH)
78
                ->addViolation();
79
        }
80
    }
81
}
82