Completed
Push — master ( a752ac...3b9af8 )
by Kamil
41:58 queued 31:09
created

validate()   C

Complexity

Conditions 12
Paths 21

Size

Total Lines 44
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 44
rs 5.1612
c 0
b 0
f 0
cc 12
eloc 24
nc 21
nop 2

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\AttributeBundle\Validator\Constraints;
15
16
use Sylius\Component\Attribute\AttributeType\SelectAttributeType;
17
use Sylius\Component\Attribute\Model\AttributeInterface;
18
use Symfony\Component\Validator\Constraint;
19
use Symfony\Component\Validator\ConstraintValidator;
20
use Webmozart\Assert\Assert;
21
22
final class ValidSelectAttributeConfigurationValidator extends ConstraintValidator
23
{
24
    /**
25
     * {@inheritdoc}
26
     */
27
    public function validate($attribute, Constraint $constraint): void
28
    {
29
        /** @var AttributeInterface $attribute */
30
        Assert::isInstanceOf($attribute, AttributeInterface::class);
31
        Assert::isInstanceOf($constraint, ValidSelectAttributeConfiguration::class);
32
33
        if (SelectAttributeType::TYPE !== $attribute->getType()) {
34
            return;
35
        }
36
37
        $configuration = $attribute->getConfiguration();
38
39
        $min = null;
40
        if (!empty($configuration['min'])) {
41
            $min = $configuration['min'];
42
        }
43
44
        $max = null;
45
        if (!empty($configuration['max'])) {
46
            $max = $configuration['max'];
47
        }
48
49
        if (null === $min && null === $max) {
50
            return;
51
        }
52
53
        $multiple = $attribute->getConfiguration()['multiple'];
54
        if (!$multiple) {
55
            $this->context->addViolation($constraint->messageMultiple);
56
57
            return;
58
        }
59
60
        if (null !== $min && null !== $max && $min > $max) {
61
            $this->context->addViolation($constraint->messageMaxEntries);
62
63
            return;
64
        }
65
66
        $numberOfChoices = count($attribute->getConfiguration()['choices']);
67
        if (null !== $min && $min > $numberOfChoices) {
68
            $this->context->addViolation($constraint->messageMinEntries);
69
        }
70
    }
71
}
72