Passed
Push — v2 ( c3152c...b7bda3 )
by Daniel
05:06
created

NewEmailAddressValidator::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
/*
4
 * This file is part of the Silverback API Component Bundle Project
5
 *
6
 * (c) Daniel West <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Silverback\ApiComponentBundle\Validator\Constraints;
15
16
use Silverback\ApiComponentBundle\Entity\User\AbstractUser;
17
use Silverback\ApiComponentBundle\Repository\User\UserRepository;
18
use Symfony\Component\Validator\Constraint;
19
use Symfony\Component\Validator\ConstraintValidator;
20
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
21
22
class NewEmailAddressValidator extends ConstraintValidator
23
{
24
    private UserRepository $userRepository;
25
26 5
    public function __construct(
27
        UserRepository $userRepository
28
    ) {
29 5
        $this->userRepository = $userRepository;
30 5
    }
31
32 5
    public function validate($user, Constraint $constraint): void
33
    {
34 5
        if (!$user instanceof AbstractUser) {
35 1
            throw new UnexpectedTypeException($user, AbstractUser::class);
36
        }
37
38 4
        if (!$user->getNewEmailAddress()) {
39 1
            return;
40
        }
41
42 3
        if ($user->getNewEmailAddress() === $user->getEmailAddress()) {
43 1
            $this->context->buildViolation($constraint->differentMessage)
44 1
                ->addViolation();
45
46 1
            return;
47
        }
48
49 2
        if ($this->userRepository->findOneBy(['email_address' => $user->getNewEmailAddress()])) {
50 1
            $this->context->buildViolation($constraint->uniqueMessage)
51 1
                ->addViolation();
52
53 1
            return;
54
        }
55 1
    }
56
}
57