Passed
Pull Request — master (#26)
by Yo
04:47 queued 01:57
created

DocNormalizer   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 143
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A normalize() 0 10 1
A pathsArray() 0 19 2
A infoArray() 0 7 1
A serverArray() 0 8 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
     * @throws \ReflectionException
40
     */
41 10
    public function normalize(HttpServerDoc $doc) : array
42
    {
43
        return [
44
                'swagger' => '2.0',
45
            ]
46 10
            + $this->infoArray($doc)
47 10
            + $this->serverArray($doc)
48 10
            + $this->tagsArray($doc)
49 10
            + $this->pathsArray($doc)
50 10
            + $this->externalSchemaListArray($doc)
51
        ;
52
    }
53
54
    /**
55
     * @param HttpServerDoc $doc
56
     *
57
     * @return array
58
     */
59 10
    protected function infoArray(HttpServerDoc $doc) : array
60
    {
61 10
        $infoArray = [];
62 10
        $infoArray = $this->appendIfValueNotNull('title', $doc->getName(), $infoArray);
63 10
        $infoArray = $this->appendIfValueNotNull('version', $doc->getVersion(), $infoArray);
64
65 10
        return $this->appendIfValueHaveSiblings('info', $infoArray);
66
    }
67
68
    /**
69
     * @param HttpServerDoc $doc
70
     *
71
     * @return array
72
     */
73 10
    protected function serverArray(HttpServerDoc $doc) : array
74
    {
75 10
        $docArray = [];
76 10
        $docArray = $this->appendIfValueNotNull('host', $doc->getHost(), $docArray);
77 10
        $docArray = $this->appendIfValueNotNull('basePath', $doc->getBasePath(), $docArray);
78 10
        $docArray = $this->appendIfValueHaveSiblings('schemes', $doc->getSchemeList(), $docArray);
79
80 10
        return $docArray;
81
    }
82
83
    /**
84
     * @param HttpServerDoc $doc
85
     *
86
     * @return array
87
     */
88 10
    protected function tagsArray(HttpServerDoc $doc) : array
89
    {
90 10
        $self = $this;
91
92 10
        return $this->appendIfValueHaveSiblings(
93 10
            'tags',
94 10
            array_map(
95
                function (TagDoc $tagDoc) use ($self) {
96 2
                    return $self->convertToTagDoc($tagDoc);
97 10
                },
98 10
                $doc->getTagList()
99
            )
100
        );
101
    }
102
103
    /**
104
     * @param HttpServerDoc $doc
105
     *
106
     * @return array
107
     */
108 10
    protected function pathsArray(HttpServerDoc $doc) : array
109
    {
110 10
        $paths = [];
111 10
        foreach ($doc->getMethodList() as $method) {
112 3
            $operationDoc = $this->operationDocNormalizer->normalize($method);
113
114
            // As JSON-RPC use only one endpoint
115
            // and openApi does not handle multiple request definition for the same endpoint
116
            // => create a fake (but usable) endpoint by using method id and '/../'
117 3
            $openApiHttpEndpoint = sprintf(
118 3
                '/%s/..%s',
119 3
                str_replace('/', '-', $method->getIdentifier()),
120 3
                $doc->getEndpoint() ?? ''
121
            );
122
123 3
            $paths[$openApiHttpEndpoint] = ['post' => $operationDoc];
124
        }
125
126 10
        return $this->appendIfValueHaveSiblings('paths', $paths);
127
    }
128
129
    /**
130
     * @param HttpServerDoc $doc
131
     *
132
     * @return array
133
     *
134
     * @throws \ReflectionException
135
     */
136 10
    protected function externalSchemaListArray(HttpServerDoc $doc) : array
137
    {
138 10
        return $this->appendIfValueHaveSiblings(
139 10
            'definitions',
140 10
            $this->externalSchemaListDocNormalizer->normalize($doc)
141
        );
142
    }
143
144
    /**
145
     * @param TagDoc $tag
146
     *
147
     * @return array
148
     */
149 2
    private function convertToTagDoc(TagDoc $tag) : array
150
    {
151 2
        $tagArray = ['name' => $tag->getName()];
152
153 2
        $tagArray = $this->appendIfValueNotNull('description', $tag->getDescription(), $tagArray);
154
155 2
        return $tagArray;
156
    }
157
}
158