Completed
Push — master ( 0bc643...d7624f )
by David de
04:40
created

ValidationException::createMessage()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
ccs 6
cts 6
cp 1
cc 2
eloc 5
nc 2
nop 2
crap 2
1
<?php
2
3
namespace Port\Exception;
4
5
use Port\Exception;
6
use Symfony\Component\Validator\ConstraintViolationInterface;
7
use Symfony\Component\Validator\ConstraintViolationListInterface;
8
9
/**
10
 * @author Markus Bachmann <[email protected]>
11
 */
12
class ValidationException extends \Exception implements Exception
13
{
14
    /**
15
     * @var ConstraintViolationListInterface
16
     */
17
    private $violations;
18
19
    /**
20
     * @var integer
21
     */
22
    private $lineNumber;
23
24
    /**
25
     * @param ConstraintViolationListInterface $list
26
     * @param integer                          $line
27
     */
28 2
    public function __construct(ConstraintViolationListInterface $list, $line)
29
    {
30 2
        $this->violations = $list;
31 2
        $this->lineNumber = $line;
32
33 2
        $this->message = $this->createMessage($list, $line);
34 2
    }
35
36
    /**
37
     * @return ConstraintViolationListInterface
38
     */
39 2
    public function getViolations()
40
    {
41 2
        return $this->violations;
42
    }
43
44
    /**
45
     * @return integer
46
     */
47 2
    public function getLineNumber()
48
    {
49 2
        return $this->lineNumber;
50
    }
51
52
    /**
53
     * @param ConstraintViolationListInterface|ConstraintViolationInterface[] $list
54
     * @param integer $line
55
     *
56
     * @return string
57
     */
58 2
    private function createMessage(ConstraintViolationListInterface $list, $line)
59
    {
60 2
        $messages = [];
61 2
        foreach ($list as $violation) {
62 2
            $messages[] = $violation->getMessage();
63 2
        }
64
65 2
        return sprintf('Line %d: %s', $line, implode(', ', $messages));
66
    }
67
}
68