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

ConstraintViolationList   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Test Coverage

Coverage 55%

Importance

Changes 0
Metric Value
dl 0
loc 50
ccs 11
cts 20
cp 0.55
rs 10
c 0
b 0
f 0
wmc 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A addViolationList() 0 4 2
A addViolation() 0 13 2
A all() 0 3 1
A count() 0 3 1
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