ValidatorException   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Test Coverage

Coverage 85.71%

Importance

Changes 0
Metric Value
wmc 10
eloc 18
dl 0
loc 45
ccs 18
cts 21
cp 0.8571
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getViolations() 0 3 1
A __construct() 0 4 1
A propertyViolationExists() 0 11 4
A buildMessage() 0 14 4
1
<?php
2
3
namespace Larapie\DataTransferObject\Exceptions;
4
5
use Symfony\Component\Validator\ConstraintViolationListInterface;
6
7
class ValidatorException extends \Symfony\Component\Validator\Exception\ValidatorException
8
{
9
    /**
10
     * @var ConstraintViolationListInterface[]
11
     */
12
    protected $violations;
13
14 11
    public function __construct($violations)
15
    {
16 11
        $this->violations = $violations;
17 11
        parent::__construct($this->buildMessage($violations));
18 11
    }
19
20 11
    protected function buildMessage($violations): string
21
    {
22 11
        $message = '';
23 11
        foreach ($violations as $propertyName => $propertyViolations) {
24 11
            if (! empty($propertyViolations)) {
25 11
                $message = $message."Exception on property '".$propertyName."': ";
26
            }
27 11
            foreach ($propertyViolations as $violation) {
28 11
                $message = $message.$violation->getMessage().'.';
29
            }
30 11
            $message = $message."\n";
31
        }
32
33 11
        return $message;
34
    }
35
36 10
    public function propertyViolationExists(string $property, string $violationClass)
37
    {
38 10
        if (array_key_exists($property, $this->violations)) {
39 10
            foreach ($this->violations[$property] as $violation) {
40 10
                if ($violation instanceof $violationClass) {
41 10
                    return true;
42
                }
43
            }
44
        }
45
46
        return false;
47
    }
48
49
    public function getViolations(): array
50
    {
51
        return $this->violations;
52
    }
53
}
54