Completed
Push — release/1.0.0 ( 7f2bd5...921032 )
by Yo
01:42
created

ErrorDocNormalizer   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 43
ccs 16
cts 16
cp 1
rs 10
c 0
b 0
f 0
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A normalize() 0 21 4
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