SwaggerGenerator   A
last analyzed

Complexity

Total Complexity 27

Size/Duplication

Total Lines 253
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 27
eloc 63
c 0
b 0
f 0
dl 0
loc 253
rs 10

22 Methods

Rating   Name   Duplication   Size   Complexity  
A setIndent() 0 3 1
A setOperationsGenerator() 0 3 1
A configure() 0 25 1
A setNamespace() 0 3 1
A setDefinitions() 0 3 1
A generateClasses() 0 19 4
A getSwaggerUri() 0 3 1
A generate() 0 3 1
A processDefinitions() 0 10 2
A setModelGenerator() 0 3 1
A setOverride() 0 3 1
A getDefinitions() 0 3 1
A getOperationsGenerator() 0 3 1
A setFolder() 0 3 1
A processPaths() 0 10 2
A __construct() 0 7 1
A isOverride() 0 3 1
A getIndent() 0 3 1
A getFolder() 0 3 1
A getModelGenerator() 0 3 1
A getNamespace() 0 3 1
A setSwaggerUri() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Prometee\SwaggerClientGenerator\Swagger;
6
7
class SwaggerGenerator implements SwaggerGeneratorInterface
8
{
9
    /** @var SwaggerModelGeneratorInterface */
10
    protected $modelGenerator;
11
    /** @var SwaggerOperationsGeneratorInterface */
12
    protected $operationsGenerator;
13
14
    /** @var string */
15
    protected $swaggerUri = '';
16
    /** @var string */
17
    protected $folder = '';
18
    /** @var string */
19
    protected $namespace = '';
20
    /** @var array */
21
    protected $definitions = [];
22
    /** @var string */
23
    protected $indent = '    ';
24
    /** @var bool */
25
    protected $override = false;
26
27
    /**
28
     * @param SwaggerModelGeneratorInterface $modelGenerator
29
     * @param SwaggerOperationsGeneratorInterface $operationsGenerator
30
     */
31
    public function __construct(
32
        SwaggerModelGeneratorInterface $modelGenerator,
33
        SwaggerOperationsGeneratorInterface $operationsGenerator
34
    )
35
    {
36
        $this->modelGenerator = $modelGenerator;
37
        $this->operationsGenerator = $operationsGenerator;
38
    }
39
40
    /**
41
     * {@inheritDoc}
42
     */
43
    public function configure(
44
        string $swaggerUri,
45
        string $folder,
46
        string $namespace,
47
        string $indent = '    ',
48
        bool $override = false
49
    ): void
50
    {
51
        $this->swaggerUri = $swaggerUri;
52
        $this->folder = $folder;
53
        $this->namespace = $namespace;
54
        $this->indent = $indent;
55
        $this->override = $override;
56
57
        $this->modelGenerator->configure(
58
            $folder . '/' . static::TYPE_MODEL,
59
            $namespace . '\\' . static::TYPE_MODEL,
60
            $indent
61
        );
62
63
        $this->operationsGenerator->configure(
64
            $folder . '/' . static::TYPE_OPERATIONS,
65
            $namespace . '\\' . static::TYPE_OPERATIONS,
66
            $namespace . '\\' . static::TYPE_MODEL,
67
            $indent
68
        );
69
70
    }
71
72
    /**
73
     * {@inheritDoc}
74
     */
75
    public function generateClasses(): bool
76
    {
77
        $content = file_get_contents($this->swaggerUri);
78
        if ($content === false) {
79
            return false;
80
        }
81
        if (empty($content)) {
82
            return false;
83
        }
84
        $json = json_decode($content, true);
85
        if ($json === null) {
86
            return false;
87
        }
88
89
        $this->processDefinitions($json);
90
91
        $this->processPaths($json);
92
93
        return true;
94
    }
95
96
    /**
97
     * {@inheritDoc}
98
     */
99
    public function generate(string $indent = null): ?string
0 ignored issues
show
Unused Code introduced by
The parameter $indent is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

99
    public function generate(/** @scrutinizer ignore-unused */ string $indent = null): ?string

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
100
    {
101
        return null;
102
    }
103
104
    /**
105
     * {@inheritDoc}
106
     */
107
    public function processDefinitions(array $json): bool
108
    {
109
        if (!isset($json['definitions'])) {
110
            return false;
111
        }
112
113
        $this->modelGenerator->setDefinitions($json['definitions']);
114
        $this->modelGenerator->setOverwrite($this->override);
115
116
        return $this->modelGenerator->generate();
117
    }
118
119
    /**
120
     * {@inheritDoc}
121
     */
122
    public function processPaths(array $json, bool $overwrite = false): bool
123
    {
124
        if (!isset($json['paths'])) {
125
            return false;
126
        }
127
128
        $this->operationsGenerator->setPaths($json['paths']);
129
        $this->operationsGenerator->setOverwrite($this->override);
130
131
        return $this->operationsGenerator->generate();
132
    }
133
134
    /**
135
     * {@inheritDoc}
136
     */
137
    public function getSwaggerUri(): string
138
    {
139
        return $this->swaggerUri;
140
    }
141
142
    /**
143
     * {@inheritDoc}
144
     */
145
    public function setSwaggerUri(string $swaggerUri): void
146
    {
147
        $this->swaggerUri = $swaggerUri;
148
    }
149
150
    /**
151
     * {@inheritDoc}
152
     */
153
    public function getFolder(): string
154
    {
155
        return $this->folder;
156
    }
157
158
    /**
159
     * {@inheritDoc}
160
     */
161
    public function setFolder(string $folder): void
162
    {
163
        $this->folder = $folder;
164
    }
165
166
    /**
167
     * {@inheritDoc}
168
     */
169
    public function getNamespace(): string
170
    {
171
        return $this->namespace;
172
    }
173
174
    /**
175
     * {@inheritDoc}
176
     */
177
    public function setNamespace(string $namespace): void
178
    {
179
        $this->namespace = $namespace;
180
    }
181
182
    /**
183
     * {@inheritDoc}
184
     */
185
    public function getDefinitions(): array
186
    {
187
        return $this->definitions;
188
    }
189
190
    /**
191
     * {@inheritDoc}
192
     */
193
    public function setDefinitions(array $definitions): void
194
    {
195
        $this->definitions = $definitions;
196
    }
197
198
    /**
199
     * {@inheritDoc}
200
     */
201
    public function getIndent(): string
202
    {
203
        return $this->indent;
204
    }
205
206
    /**
207
     * {@inheritDoc}
208
     */
209
    public function setIndent(string $indent): void
210
    {
211
        $this->indent = $indent;
212
    }
213
214
    /**
215
     * {@inheritDoc}
216
     */
217
    public function getModelGenerator(): SwaggerModelGeneratorInterface
218
    {
219
        return $this->modelGenerator;
220
    }
221
222
    /**
223
     * {@inheritDoc}
224
     */
225
    public function setModelGenerator(SwaggerModelGeneratorInterface $modelGenerator): void
226
    {
227
        $this->modelGenerator = $modelGenerator;
228
    }
229
230
    /**
231
     * {@inheritDoc}
232
     */
233
    public function getOperationsGenerator(): SwaggerOperationsGeneratorInterface
234
    {
235
        return $this->operationsGenerator;
236
    }
237
238
    /**
239
     * {@inheritDoc}
240
     */
241
    public function setOperationsGenerator(SwaggerOperationsGeneratorInterface $operationsGenerator): void
242
    {
243
        $this->operationsGenerator = $operationsGenerator;
244
    }
245
246
    /**
247
     * {@inheritDoc}
248
     */
249
    public function isOverride(): bool
250
    {
251
        return $this->override;
252
    }
253
254
    /**
255
     * {@inheritDoc}
256
     */
257
    public function setOverride(bool $override): void
258
    {
259
        $this->override = $override;
260
    }
261
}
262