Completed
Push — release/1.0.0 ( 7f2bd5...921032 )
by Yo
01:42
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.0534
c 0
b 0
f 0
1
<?php
2
namespace Yoanm\JsonRpcHttpServerSwaggerDoc\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
     * {@inheritdoc}
30
     */
31 4
    public function normalize(ErrorDoc $errorDoc)
32
    {
33 4
        $requiredDoc = ['required' => ['code']];
34
        $properties = [
35 4
            'code' => ['example' => $errorDoc->getCode()]
36
        ];
37 4
        if (null !== $errorDoc->getDataDoc()) {
38 2
            $properties['data'] = $this->typeDocNormalizer->normalize($errorDoc->getDataDoc());
39 2
            if (false !== $errorDoc->getDataDoc()->isRequired()) {
40 1
                $requiredDoc['required'][] = 'data';
41
            }
42
        }
43 4
        if (null !== $errorDoc->getMessage()) {
44 1
            $properties['message'] = ['example' => $errorDoc->getMessage()];
45
        }
46
47
        return [
48 4
            'title' => $errorDoc->getTitle(),
49
            'allOf' => [
50 4
                $this->shapeNormalizer->getErrorShapeDefinition(),
51 4
                (['type' => 'object'] + $requiredDoc + ['properties' => $properties]),
52
            ],
53
        ];
54
    }
55
}
56