Passed
Push — master ( 02b4c6...8eef8d )
by Yo
46s
created

ErrorDocNormalizer::normalize()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 14
nc 6
nop 1
dl 0
loc 21
ccs 12
cts 12
cp 1
crap 4
rs 9.7998
c 0
b 0
f 0
1
<?php
2
namespace Yoanm\JsonRpcHttpServerOpenAPIDoc\App\Normalizer\Component;
3
4
use Yoanm\JsonRpcServerDoc\Domain\Model\ErrorDoc;
5
6
/**
7
 * Class ErrorDocNormalizer
8
 */
9
class ErrorDocNormalizer
10
{
11
    /** @var TypeDocNormalizer */
12
    private $typeDocNormalizer;
13
    /** @var ShapeNormalizer */
14
    private $shapeNormalizer;
15
16
    /**
17
     * @param TypeDocNormalizer $typeDocNormalizer
18
     * @param ShapeNormalizer   $shapeNormalizer
19
     */
20 4
    public function __construct(
21
        TypeDocNormalizer $typeDocNormalizer,
22
        ShapeNormalizer $shapeNormalizer
23
    ) {
24 4
        $this->typeDocNormalizer = $typeDocNormalizer;
25 4
        $this->shapeNormalizer = $shapeNormalizer;
26 4
    }
27
28
    /**
29
     * @param ErrorDoc $errorDoc
30
     *
31
     * @return array
32
     *
33
     * @throws \ReflectionException
34
     */
35 4
    public function normalize(ErrorDoc $errorDoc) : array
36
    {
37 4
        $requiredDoc = ['required' => ['code']];
38
        $properties = [
39 4
            'code' => ['example' => $errorDoc->getCode()]
40
        ];
41 4
        if (null !== $errorDoc->getDataDoc()) {
42 2
            $properties['data'] = $this->typeDocNormalizer->normalize($errorDoc->getDataDoc());
43 2
            if (false !== $errorDoc->getDataDoc()->isRequired()) {
44 1
                $requiredDoc['required'][] = 'data';
45
            }
46
        }
47 4
        if (null !== $errorDoc->getMessage()) {
48 1
            $properties['message'] = ['example' => $errorDoc->getMessage()];
49
        }
50
51
        return [
52 4
            'title' => $errorDoc->getTitle(),
53
            'allOf' => [
54 4
                $this->shapeNormalizer->getErrorShapeDefinition(),
55 4
                (['type' => 'object'] + $requiredDoc + ['properties' => $properties]),
56
            ],
57
        ];
58
    }
59
}
60