Passed
Push — release/1.0.0 ( 02b4c6...f79e53 )
by Yo
01:58 queued 34s
created

DocNormalizer::pathsArray()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 9
nc 2
nop 1
dl 0
loc 19
ccs 10
cts 10
cp 1
crap 2
rs 9.9666
c 0
b 0
f 0
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 10
    public function normalize(HttpServerDoc $doc)
40
    {
41
        return [
42
                'openapi' => '3.0.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
     * {@inheritdoc}
53
     */
54 10
    protected function infoArray(HttpServerDoc $doc)
55
    {
56 10
        $infoArray = [];
57 10
        $infoArray = $this->appendIfValueNotNull('title', $doc->getName(), $infoArray);
58 10
        $infoArray = $this->appendIfValueNotNull('version', $doc->getVersion(), $infoArray);
59
60 10
        return $this->appendIfValueHaveSiblings('info', $infoArray);
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66 10
    protected function serverArray(HttpServerDoc $doc)
67
    {
68 10
        $serverList = [];
69 10
        if (null !== $doc->getHost()) {
70 4
            $host = $doc->getHost();
71 4
            if (null !== $host) {
0 ignored issues
show
introduced by
The condition null !== $host is always true.
Loading history...
72 4
                if (null !== $doc->getBasePath()) {
73 2
                    $host .= $doc->getBasePath();
74
                }
75 4
                $schemeList = $doc->getSchemeList();
76 4
                if (0 === count($schemeList)) {
77 3
                    $schemeList[] = 'http';
78
                }
79 4
                foreach ($schemeList as $scheme) {
80 4
                    $serverList[] = ['url' => sprintf('%s://%s', $scheme, $host)];
81
                }
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 10
    protected function externalSchemaListArray(HttpServerDoc $doc)
136
    {
137
        return [
138
            'components' => [
139 10
                'schemas' => $this->externalSchemaListDocNormalizer->normalize($doc),
140
            ],
141
        ];
142
    }
143
144
    /**
145
     * @param TagDoc $tag
146
     *
147
     * @return array
148
     */
149 2
    private function convertToTagDoc(TagDoc $tag)
150
    {
151
        $tagDoc = [
152 2
            'name' => $tag->getName(),
153
        ];
154 2
        if (null !== $tag->getDescription()) {
155 2
            $tagDoc['description'] = $tag->getDescription();
156
        }
157
158 2
        return $tagDoc;
159
    }
160
}
161