Completed
Pull Request — master (#2)
by Samuel
06:02
created

ValidationExceptionFormatter   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 3
dl 0
loc 25
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A onErrorFormatting() 0 19 3
1
<?php
2
3
namespace App\Common\App\Formatter;
4
5
use App\Common\Domain\ValidationException;
6
use Overblog\GraphQLBundle\Event\ErrorFormattingEvent;
7
8
class ValidationExceptionFormatter
9
{
10
    /**
11
     * @param ErrorFormattingEvent $event
12
     */
13
    public function onErrorFormatting(ErrorFormattingEvent $event) :void
14
    {
15
        $error = $event->getError()->getPrevious();
16
17
        if ($error instanceof ValidationException) {
18
            $errors = [];
19
20
            $violations = $error->getViolations();
21
            foreach($violations as $violation) {
22
                $errors[] = [
23
                    'field' => $violation->getPropertyPath(),
24
                    'message' => $violation->getMessage(),
25
                ];
26
            }
27
28
            $formattedError = $event->getFormattedError();
29
            $formattedError->offsetSet('fields', $errors);
30
        }
31
    }
32
}
33