Passed
Push — master ( 6444d8...6b2101 )
by Petr
03:48
created

ApiValidationError::__construct()   B

Complexity

Conditions 5
Paths 2

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 5.0187

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 21
c 1
b 0
f 0
ccs 10
cts 11
cp 0.9091
rs 8.7624
cc 5
eloc 12
nc 2
nop 1
crap 5.0187
1
<?php
2
3
namespace AppBundle\Response;
4
5
use Symfony\Component\Form\FormError;
6
use Symfony\Component\Form\FormInterface;
7
use Symfony\Component\HttpFoundation\Response;
8
9
/**
10
 * @author Vehsamrak
11
 */
12
class ApiValidationError extends ApiError
13
{
14
15
    /** {@inheritDoc} */
16 5
    public function __construct($errorData)
17
    {
18
        /** @var string[] $errors */
19 5
        $errors = [];
20
21 5
        if ($errorData instanceof FormInterface) {
22
            /** @var FormError $error */
23 5
            foreach ($errorData->getErrors(true) as $error) {
24 5
                $parametersString = join(',', $error->getMessageParameters());
25 5
                if (!$parametersString || in_array($parametersString, ['null', 'array'])) {
26 5
                    $errors[] = $error->getMessage();
27
                } else {
28 5
                    $errors[] = sprintf('%s - %s', $parametersString, $error->getMessage());
29
                }
30
            }
31
        } else {
32
            $errors = (array) $errorData;
33
        }
34
35 5
        parent::__construct($errors, Response::HTTP_BAD_REQUEST);
36 5
    }
37
}
38