Completed
Push — master ( 67a91e...657487 )
by Peter
06:52
created

InvalidMessageException::getMessages()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 0
cts 6
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 0
crap 6
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\Handler\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
    public function __construct(ConstraintViolationListInterface $violations, $code = 0, \Exception $previous = null)
30
    {
31
        $this->violations = $violations;
32
        $message = '';
33
34
        // get first message from violations
35
        foreach ($violations as $violation) {
36
            /* @var $violation ConstraintViolationInterface */
37
            $message = $violation->getMessage();
38
            break;
39
        }
40
41
        parent::__construct($message, $previous, $code);
42
    }
43
44
    /**
45
     * @return ConstraintViolationListInterface
46
     */
47
    public function getViolations()
48
    {
49
        return $this->violations;
50
    }
51
52
    /**
53
     * @return string[]
54
     */
55
    public function getMessages()
56
    {
57
        $messages = [];
58
59
        foreach ($this->violations as $violation) {
60
            /* @var $violation ConstraintViolationInterface */
61
            $messages[$violation->getPropertyPath()] = $violation->getMessage();
62
        }
63
64
        return $messages;
65
    }
66
}
67