UniqueEmailValidator   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 21
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 7
c 0
b 0
f 0
dl 0
loc 21
rs 10
ccs 9
cts 9
cp 1
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A validate() 0 9 3
A __construct() 0 3 1
1
<?php
2
declare(strict_types = 1);
3
/**
4
 * /src/App/Validator/Constraints/UniqueEmailValidator.php
5
 *
6
 * @author TLe, Tarmo Leppänen <[email protected]>
7
 */
8
9
namespace App\Validator\Constraints;
10
11
use App\Entity\Interfaces\UserInterface;
12
use App\Repository\UserRepository;
13
use Doctrine\ORM\NonUniqueResultException;
14
use Symfony\Component\Validator\Constraint;
15
use Symfony\Component\Validator\ConstraintValidator;
16
17
/**
18
 * Class UniqueEmailValidator
19
 *
20
 * @package App\Validator\Constraints
21
 * @author TLe, Tarmo Leppänen <[email protected]>
22
 */
23
class UniqueEmailValidator extends ConstraintValidator
24
{
25 40
    public function __construct(
26
        private readonly UserRepository $repository,
27
    ) {
28 40
    }
29
30
    /**
31
     * {@inheritdoc}
32
     *
33
     * @throws NonUniqueResultException
34
     */
35 40
    public function validate(mixed $value, Constraint $constraint): void
36
    {
37 40
        if ($value instanceof UserInterface
38 40
            && !$this->repository->isEmailAvailable($value->getEmail(), $value->getId())
39
        ) {
40 1
            $this->context
41 1
                ->buildViolation(UniqueEmail::MESSAGE)
42 1
                ->setCode(UniqueEmail::IS_UNIQUE_EMAIL_ERROR)
43 1
                ->addViolation();
44
        }
45
    }
46
}
47