Completed
Pull Request — master (#7)
by Gareth
03:55
created

ValidatorStep   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 10
c 0
b 0
f 0
lcom 1
cbo 3
dl 0
loc 102
rs 10
ccs 30
cts 30
cp 1

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A add() 0 10 2
A throwExceptions() 0 4 1
A getViolations() 0 4 1
B process() 0 22 4
A getPriority() 0 4 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 = 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 4
    public function add($field, Constraint $constraint)
55
    {
56 4
        if (!isset($this->constraints[$field])) {
57 4
            $this->constraints[$field] = [];
58 4
        }
59
60 4
        $this->constraints[$field][] = $constraint;
61
62 4
        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 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
        $constraints = new Constraints\Collection($this->constraints);
87 4
        $list = $this->validator->validate($item, $constraints);
88
89 4
        if (count($list) > 0) {
90 3
            $this->violations[$this->line] = $list;
91
92 3
            if ($this->throwExceptions) {
93 2
                $exception = new ValidationException($list, $this->line);
94 2
                $this->line++;
95
96 2
                throw $exception;
97
            }
98 1
        }
99
100 2
        $this->line++;
101
102 2
        if (0 === count($list)) {
103 1
            return $next($item);
104
        }
105 1
    }
106
107
    /**
108
     * {@inheritdoc}
109
     */
110 1
    public function getPriority()
111
    {
112 1
        return 128;
113
    }
114
}
115