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

InvalidMessageException   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 2
dl 0
loc 50
ccs 0
cts 17
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 14 2
A getViolations() 0 4 1
A getMessages() 0 11 2
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