Completed
Push — master ( ab6ee6...8da333 )
by Kamil
32:30 queued 09:03
created

ZoneCannotContainItselfValidator   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 19
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 4
dl 0
loc 19
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A validate() 0 16 4
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\AddressingBundle\Validator\Constraints;
15
16
use Sylius\Component\Addressing\Model\ZoneMemberInterface;
17
use Symfony\Component\Validator\Constraint;
18
use Symfony\Component\Validator\ConstraintValidator;
19
use Webmozart\Assert\Assert;
20
21
final class ZoneCannotContainItselfValidator extends ConstraintValidator
22
{
23
    public function validate($value, Constraint $constraint): void
24
    {
25
        if ($value === null) {
26
            return;
27
        }
28
29
        /** @var ZoneCannotContainItself $constraint */
30
        Assert::isInstanceOf($constraint, ZoneCannotContainItself::class);
31
32
        /** @var ZoneMemberInterface $zoneMember */
33
        foreach ($value as $zoneMember) {
34
            if ($zoneMember->getCode() === $zoneMember->getBelongsTo()->getCode()) {
35
                $this->context->addViolation($constraint->message);
36
            }
37
        }
38
    }
39
}
40