Passed
Push — master ( 63b45b...921011 )
by Petr
04:21
created

LinksCollectionDTO::validate()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 10
cts 10
cp 1
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 10
nc 8
nop 1
crap 5
1
<?php
2
3
namespace AppBundle\Form\Event;
4
5
use Symfony\Component\Validator\Constraints as Assert;
6
use Symfony\Component\Validator\Context\ExecutionContextInterface;
7
8
/**
9
 * @author Vehsamrak
10
 */
11
class LinksCollectionDTO
12
{
13
14
    /** @var array */
15
    public $links;
16
17
    /**
18
     * @Assert\Callback
19
     */
20 3
    public function validate(ExecutionContextInterface $context)
21
    {
22 3
        if (empty($this->links)) {
23 1
            $context->buildViolation('Parameter is mandatory: links.')->addViolation();
24
        }
25
26 3
        if (!is_array($this->links)) {
27 1
            $context->buildViolation('Links must be an array.')->addViolation();
28
        } else {
29 2
            foreach ($this->links as $linkKey => $link) {
30 2
                $url = $link['url'] ?? null;
31
32 2
                if (!$url) {
33 2
                    $context->buildViolation(sprintf('Parameter is mandatory: links[%s][url].', $linkKey))->addViolation();
34
                }
35
            }
36
        }
37 3
    }
38
}
39