Passed
Push — release/1.0.0 ( ec2067...0e1864 )
by Yo
01:35
created

DocNormalizer   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 132
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 132
ccs 47
cts 47
cp 1
rs 10
c 0
b 0
f 0
wmc 9

8 Methods

Rating   Name   Duplication   Size   Complexity  
A pathsArray() 0 19 2
A infoArray() 0 7 1
A serverArray() 0 8 1
A normalize() 0 10 1
A __construct() 0 6 1
A convertToTagDoc() 0 7 1
A externalSchemaListArray() 0 5 1
A tagsArray() 0 11 1
1
<?php
2
namespace Yoanm\JsonRpcHttpServerSwaggerDoc\Infra\Normalizer;
3
4
use Yoanm\JsonRpcHttpServerSwaggerDoc\App\Helper\ArrayAppendHelperTrait;
5
use Yoanm\JsonRpcHttpServerSwaggerDoc\App\Normalizer\Component\ExternalSchemaListDocNormalizer;
6
use Yoanm\JsonRpcHttpServerSwaggerDoc\App\Normalizer\Component\OperationDocNormalizer;
7
use Yoanm\JsonRpcServerDoc\Domain\Model\HttpServerDoc;
8
use Yoanm\JsonRpcServerDoc\Domain\Model\TagDoc;
9
10
/**
11
 * Class DocNormalizer
12
 */
13
class DocNormalizer
14
{
15
    use ArrayAppendHelperTrait;
16
17
    /** @var ExternalSchemaListDocNormalizer */
18
    private $externalSchemaListDocNormalizer;
19
    /** @var OperationDocNormalizer */
20
    private $operationDocNormalizer;
21
22
    /**
23
     * @param ExternalSchemaListDocNormalizer $externalSchemaListDocNormalizer
24
     * @param OperationDocNormalizer          $operationDocNormalizer
25
     */
26 10
    public function __construct(
27
        ExternalSchemaListDocNormalizer $externalSchemaListDocNormalizer,
28
        OperationDocNormalizer $operationDocNormalizer
29
    ) {
30 10
        $this->externalSchemaListDocNormalizer = $externalSchemaListDocNormalizer;
31 10
        $this->operationDocNormalizer = $operationDocNormalizer;
32 10
    }
33
34
    /**
35
     * @param HttpServerDoc $doc
36
     *
37
     * @return array
38
     */
39 10
    public function normalize(HttpServerDoc $doc)
40
    {
41
        return [
42
                'swagger' => '2.0',
43
            ]
44 10
            + $this->infoArray($doc)
45 10
            + $this->serverArray($doc)
46 10
            + $this->tagsArray($doc)
47 10
            + $this->pathsArray($doc)
48 10
            + $this->externalSchemaListArray($doc)
49
        ;
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55 10
    protected function infoArray(HttpServerDoc $doc)
56
    {
57 10
        $infoArray = [];
58 10
        $infoArray = $this->appendIfValueNotNull('title', $doc->getName(), $infoArray);
59 10
        $infoArray = $this->appendIfValueNotNull('version', $doc->getVersion(), $infoArray);
60
61 10
        return $this->appendIfValueHaveSiblings('info', $infoArray);
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67 10
    protected function serverArray(HttpServerDoc $doc)
68
    {
69 10
        $docArray = [];
70 10
        $docArray = $this->appendIfValueNotNull('host', $doc->getHost(), $docArray);
71 10
        $docArray = $this->appendIfValueNotNull('basePath', $doc->getBasePath(), $docArray);
72 10
        $docArray = $this->appendIfValueHaveSiblings('schemes', $doc->getSchemeList(), $docArray);
73
74 10
        return $docArray;
75
    }
76
77
    /**
78
     * {@inheritdoc}
79
     */
80 10
    protected function tagsArray(HttpServerDoc $doc)
81
    {
82 10
        $self = $this;
83
84 10
        return $this->appendIfValueHaveSiblings(
85 10
            'tags',
86 10
            array_map(
87
                function (TagDoc $tagDoc) use ($self) {
88 2
                    return $self->convertToTagDoc($tagDoc);
89 10
                },
90 10
                $doc->getTagList()
91
            )
92
        );
93
    }
94
95
    /**
96
     * {@inheritdoc}
97
     */
98 10
    protected function pathsArray(HttpServerDoc $doc)
99
    {
100 10
        $paths = [];
101 10
        foreach ($doc->getMethodList() as $method) {
102 3
            $operationDoc = $this->operationDocNormalizer->normalize($method);
103
104
            // As JSON-RPC use only one endpoint
105
            // and openApi does not handle multiple request definition for the same endpoint
106
            // => create a fake (but usable) endpoint by using method id and '/../'
107 3
            $openApiHttpEndpoint = sprintf(
108 3
                '/%s/..%s',
109 3
                str_replace('/', '-', $method->getIdentifier()),
110 3
                $doc->getEndpoint() ?? ''
111
            );
112
113 3
            $paths[$openApiHttpEndpoint] = ['post' => $operationDoc];
114
        }
115
116 10
        return $this->appendIfValueHaveSiblings('paths', $paths);
117
    }
118
119
    /**
120
     * @param HttpServerDoc $doc
121
     *
122
     * @return array
123
     */
124 10
    protected function externalSchemaListArray(HttpServerDoc $doc)
125
    {
126 10
        return $this->appendIfValueHaveSiblings(
127 10
            'definitions',
128 10
            $this->externalSchemaListDocNormalizer->normalize($doc)
129
        );
130
    }
131
132
133
    /**
134
     * @param TagDoc $tag
135
     *
136
     * @return array
137
     */
138 2
    private function convertToTagDoc(TagDoc $tag)
139
    {
140 2
        $tagArray = ['name' => $tag->getName()];
141
142 2
        $tagArray = $this->appendIfValueNotNull('description', $tag->getDescription(), $tagArray);
143
144 2
        return $tagArray;
145
    }
146
}
147