Passed
Pull Request — master (#12)
by Yo
02:57 queued 01:20
created

DocNormalizer   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 148
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 14
eloc 52
dl 0
loc 148
ccs 52
cts 52
cp 1
rs 10
c 0
b 0
f 0

8 Methods

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