Completed
Push — master ( c79e6a...05965b )
by Markus
11s
created

ValidatorStep::process()   B

Complexity

Conditions 5
Paths 10

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 5

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 23
rs 8.5906
ccs 15
cts 15
cp 1
cc 5
eloc 13
nc 10
nop 2
crap 5
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 = 1;
34
35
    /**
36
     * @var ValidatorInterface
37
     */
38
    private $validator;
39
40
    /**
41
     * @param ValidatorInterface $validator
42
     */
43 7
    public function __construct(ValidatorInterface $validator)
44
    {
45 7
        $this->validator = $validator;
46 7
    }
47
48
    /**
49
     * @param string     $field
50
     * @param Constraint $constraint
51
     *
52
     * @return $this
53
     */
54 3
    public function add($field, Constraint $constraint)
55
    {
56 3
        if (!isset($this->constraints[$field])) {
57 3
            $this->constraints[$field] = [];
58 3
        }
59
60 3
        $this->constraints[$field][] = $constraint;
61
62 3
        return $this;
63
    }
64
65
    /**
66
     * @param boolean $flag
67
     */
68 1
    public function throwExceptions($flag = true)
69
    {
70 1
        $this->throwExceptions = $flag;
71 1
    }
72
73
    /**
74
     * @return array
75
     */
76 2
    public function getViolations()
77
    {
78 2
        return $this->violations;
79
    }
80
81
    /**
82
     * {@inheritdoc}
83
     */
84 4
    public function process($item, callable $next)
85
    {
86 4
        if (count($this->constraints) > 0) {
87 3
            $constraints = new Constraints\Collection($this->constraints);
88 3
            $list = $this->validator->validate($item, $constraints);
89 3
        } else {
90 1
            $list = $this->validator->validate($item);
91
        }
92
93 4
        if (count($list) > 0) {
94 3
            $this->violations[$this->line] = $list;
95
96 3
            if ($this->throwExceptions) {
97 1
                throw new ValidationException($list, $this->line);
98
            }
99 2
        }
100
101 3
        $this->line++;
102
103 3
        if (0 === count($list)) {
104 1
            return $next($item);
105
        }
106 2
    }
107
108
    /**
109
     * {@inheritdoc}
110
     */
111 1
    public function getPriority()
112
    {
113 1
        return 128;
114
    }
115
}
116