Passed
Pull Request — master (#21)
by Pavel
06:33
created

EntityProcessingException::fromViolationList()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 0
cts 16
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 11
nc 2
nop 1
crap 6
1
<?php
2
3
namespace ScayTrase\Api\Cruds\Exception;
4
5
use Symfony\Component\Validator\ConstraintViolationInterface;
6
use Symfony\Component\Validator\ConstraintViolationListInterface;
7
use Symfony\Component\Yaml\Yaml;
8
9
class EntityProcessingException extends \RuntimeException implements CrudsExceptionInterface
10
{
11
    /**
12
     * @param string $message
13
     *
14
     * @return static
15
     */
16
    public static function invalidDataSubmitted($message)
17
    {
18
        return new static(
19
            sprintf('Data submitted to processor is invalid: %s', $message)
20
        );
21
    }
22
23
    public static function fromViolationList(ConstraintViolationListInterface $list)
24
    {
25
        $errors = [];
26
        foreach ($list as $violation) {
27
            /** @var ConstraintViolationInterface $violation */
28
            $errors[$violation->getPropertyPath()] = [
29
                'message'       => $violation->getMessage(),
30
                'code'          => $violation->getCode(),
31
                'root'          => $violation->getRoot(),
32
                'invalid_value' => $violation->getInvalidValue(),
33
            ];
34
        }
35
36
        return new static(
37
            'Invalid data submitted for entity: ' . PHP_EOL .
38
            Yaml::dump($errors)
39
        );
40
    }
41
42
}
43