ValidatorStep::getViolations()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 2
cts 2
cp 1
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Port\Steps\Step;
4
5
use Symfony\Component\Validator\Validator\ValidatorInterface;
6
use Symfony\Component\Validator\Constraints;
7
use Symfony\Component\Validator\Constraint;
8
use Port\Exception\ValidationException;
9
10
/**
11
 * @author Markus Bachmann <[email protected]>
12
 */
13
class ValidatorStep implements PriorityStep
14
{
15
    /**
16
     * @var array
17
     */
18
    private $constraints = [];
19
20
    /**
21
     * @var array
22
     */
23
    private $violations = [];
24
25
    /**
26
     * @var boolean
27
     */
28
    private $throwExceptions = false;
29
30
    /**
31
     * @var integer
32
     */
33
    private $line = 0;
34
35
    /**
36
     * @var ValidatorInterface
37
     */
38
    private $validator;
39
40
    /**
41
     * @param ValidatorInterface $validator
42
     */
43 10
    public function __construct(ValidatorInterface $validator)
44
    {
45 10
        $this->validator = $validator;
46 10
    }
47
48
    /**
49
     * @param string     $field
50
     * @param Constraint $constraint
51
     *
52
     * @return $this
53
     */
54 5
    public function add($field, Constraint $constraint)
55
    {
56 5
        if (!isset($this->constraints['fields'][$field])) {
57 5
            $this->constraints['fields'][$field] = [];
58 5
        }
59
60 5
        $this->constraints['fields'][$field][] = $constraint;
61
62 5
        return $this;
63
    }
64
65
    /**
66
     * @param boolean $flag
67
     */
68 2
    public function throwExceptions($flag = true)
69
    {
70 2
        $this->throwExceptions = $flag;
71 2
    }
72
73
    /**
74
     * @return array
75
     */
76 4
    public function getViolations()
77
    {
78 4
        return $this->violations;
79
    }
80
81
    /**
82
     * Add additional options to the Collection constraint.
83
     *
84
     * @param string $option
85
     * @param mixed  $optionValue
86
     *
87
     * @return $this
88
     */
89 1
    public function addOption($option, $optionValue)
90
    {
91 1
        $this->constraints[$option] = $optionValue;
92
93 1
        return $this;
94
    }
95
96
    /**
97
     * {@inheritdoc}
98
     */
99 7
    public function process($item, callable $next)
100
    {
101 7
        $this->line++;
102
103 7
        if (count($this->constraints) > 0) {
104 5
            $constraints = new Constraints\Collection($this->constraints);
105 4
            $list = $this->validator->validate($item, $constraints);
106 4
        } else {
107 2
            $list = $this->validator->validate($item);
108
        }
109
110 6
        if (count($list) > 0) {
111 5
            $this->violations[$this->line] = $list;
112
113 5
            if ($this->throwExceptions) {
114 2
                throw new ValidationException($list, $this->line);
115
            }
116 3
        }
117
118 4
        if (0 === count($list)) {
119 1
            return $next($item);
120
        }
121 3
    }
122
123
    /**
124
     * {@inheritdoc}
125
     */
126 1
    public function getPriority()
127
    {
128 1
        return 128;
129
    }
130
}
131