ValidationResponseFactory   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 2
lcom 0
cbo 4
dl 0
loc 33
ccs 12
cts 12
cp 1
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 22 2
1
<?php
2
declare(strict_types=1);
3
4
namespace SlayerBirden\DataFlowServer\Stdlib\Validation;
5
6
use Psr\Http\Message\ResponseInterface;
7
use SlayerBirden\DataFlowServer\Notification\DangerMessage;
8
use Zend\Diactoros\Response\JsonResponse;
9
use Zend\InputFilter\InputFilterInterface;
10
11
final class ValidationResponseFactory
12
{
13
    /**
14
     * Create Validation response object
15
     *
16
     * @param string $dataObjectName
17
     * @param InputFilterInterface $inputFilter
18
     * @param mixed $value
19
     * @return ResponseInterface
20
     */
21 26
    public function __invoke(
22
        string $dataObjectName,
23
        InputFilterInterface $inputFilter,
24
        $value = null
25
    ): ResponseInterface {
26 26
        $validation = [];
27 26
        foreach ($inputFilter->getInvalidInput() as $key => $input) {
28 26
            $messages = $input->getMessages();
29 26
            $validation[] = [
30 26
                'field' => $key,
31 26
                'msg' => reset($messages)
32
            ];
33
        }
34
35 26
        return new JsonResponse([
36
            'data' => [
37 26
                $dataObjectName => $value,
38 26
                'validation' => $validation,
39
            ],
40 26
            'msg' => new DangerMessage('There were validation errors.'),
41 26
        ], 400);
42
    }
43
}
44