Completed
Push — master ( cbfed7...c39fb6 )
by Rafael
05:06
created

ConstraintViolationList::count()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
/*******************************************************************************
3
 *  This file is part of the GraphQL Bundle package.
4
 *
5
 *  (c) YnloUltratech <[email protected]>
6
 *
7
 *  For the full copyright and license information, please view the LICENSE
8
 *  file that was distributed with this source code.
9
 ******************************************************************************/
10
11
namespace Ynlo\GraphQLBundle\Validator;
12
13
use Symfony\Component\Validator\ConstraintViolationInterface;
14
use Symfony\Component\Validator\ConstraintViolationListInterface;
15
use Ynlo\GraphQLBundle\Model\ConstraintViolation;
16
17
/**
18
 * ConstraintViolationList
19
 */
20
class ConstraintViolationList
21
{
22
    /**
23
     * @var array
24
     */
25
    protected $violations = [];
26
27
    /**
28
     * @param ConstraintViolationInterface|ConstraintViolation $constraintViolation
29
     */
30 1
    public function addViolation($constraintViolation)
31
    {
32 1
        if ($constraintViolation instanceof ConstraintViolationInterface) {
33
            $internalViolation = new ConstraintViolation();
34
            $internalViolation->setPropertyPath($constraintViolation->getPropertyPath());
35
            $internalViolation->setInvalidValue($constraintViolation->getInvalidValue());
36
            $internalViolation->setMessageTemplate($constraintViolation->getMessageTemplate());
37
            $internalViolation->setMessage($constraintViolation->getMessage());
38
            $internalViolation->setCode($constraintViolation->getCode());
39
            $internalViolation->setPlural($constraintViolation->getPlural());
40
            $this->violations[] = $internalViolation;
41
        } else {
42 1
            $this->violations[] = $constraintViolation;
43
        }
44 1
    }
45
46
    /**
47
     * @param ConstraintViolationListInterface $violations
48
     */
49 2
    public function addViolationList(ConstraintViolationListInterface $violations)
50
    {
51 2
        foreach ($violations as $violation) {
52
            $this->addViolation($violation);
53
        }
54 2
    }
55
56
    /**
57
     * @return int
58
     */
59 7
    public function count(): int
60
    {
61 7
        return count($this->violations);
62
    }
63
64
    /**
65
     * @return array
66
     */
67 6
    public function all(): array
68
    {
69 6
        return $this->violations;
70
    }
71
}
72