Completed
Push — master ( b4619a...ab6ee6 )
by Kamil
12:06 queued 06:35
created

UniqueShopUserEmailValidator::validate()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.6666
c 0
b 0
f 0
cc 3
nc 3
nop 2
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
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 Sylius\Bundle\ApiBundle\Validator\Constraints;
15
16
use Sylius\Component\User\Canonicalizer\CanonicalizerInterface;
17
use Sylius\Component\User\Repository\UserRepositoryInterface;
18
use Symfony\Component\Validator\Constraint;
19
use Symfony\Component\Validator\ConstraintValidator;
20
use Webmozart\Assert\Assert;
21
22
final class UniqueShopUserEmailValidator extends ConstraintValidator
23
{
24
    /** @var CanonicalizerInterface */
25
    private $canonicalizer;
26
27
    /** @var UserRepositoryInterface */
28
    private $shopUserRepository;
29
30
    public function __construct(CanonicalizerInterface $canonicalizer, UserRepositoryInterface $shopUserRepository)
31
    {
32
        $this->canonicalizer = $canonicalizer;
33
        $this->shopUserRepository = $shopUserRepository;
34
    }
35
36
    public function validate($value, Constraint $constraint): void
37
    {
38
        if ($value === null) {
39
            return;
40
        }
41
42
        /** @var UniqueShopUserEmail $constraint */
43
        Assert::isInstanceOf($constraint, UniqueShopUserEmail::class);
44
45
        $emailCanonical = $this->canonicalizer->canonicalize($value);
46
        $shopUser = $this->shopUserRepository->findOneByEmail($emailCanonical);
47
48
        if ($shopUser === null) {
49
            return;
50
        }
51
52
        $this->context->addViolation($constraint->message);
53
    }
54
}
55