InvalidMessageException::getViolations()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
/**
4
 * GpsLab component.
5
 *
6
 * @author    Peter Gribanov <[email protected]>
7
 * @copyright Copyright (c) 2011, Peter Gribanov
8
 * @license   http://opensource.org/licenses/MIT
9
 */
10
11
namespace GpsLab\Component\Middleware\Exception;
12
13
use Symfony\Component\Validator\ConstraintViolationInterface;
14
use Symfony\Component\Validator\ConstraintViolationListInterface;
15
use Symfony\Component\Validator\Exception\ValidatorException;
16
17
class InvalidMessageException extends ValidatorException
18
{
19
    /**
20
     * @var ConstraintViolationListInterface
21
     */
22
    protected $violations;
23
24
    /**
25
     * @param ConstraintViolationListInterface $violations
26
     * @param int                              $code
27
     * @param \Exception|null                  $previous
28
     */
29 4
    public function __construct(ConstraintViolationListInterface $violations, $code = 0, \Exception $previous = null)
30
    {
31 4
        $this->violations = $violations;
32 4
        $message = '';
33
34
        // get first message from violations
35 4
        foreach ($violations as $violation) {
36
            /* @var $violation ConstraintViolationInterface */
37 2
            $message = $violation->getMessage();
38 2
            break;
39 4
        }
40
41 4
        parent::__construct($message, $code, $previous);
42 4
    }
43
44
    /**
45
     * @return ConstraintViolationListInterface
46
     */
47 3
    public function getViolations()
48
    {
49 3
        return $this->violations;
50
    }
51
52
    /**
53
     * @return string[]
54
     */
55 3
    public function getMessages()
56
    {
57 3
        $messages = [];
58
59 3
        foreach ($this->violations as $violation) {
60
            /* @var $violation ConstraintViolationInterface */
61 2
            $messages[$violation->getPropertyPath()] = $violation->getMessage();
62 3
        }
63
64 3
        return $messages;
65
    }
66
}
67