Completed
Push — master ( fcd315...e54b6a )
by John
02:20
created

OpenApiOperation::__construct()   C

Complexity

Conditions 11
Paths 24

Size

Total Lines 46
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 46
rs 5.2653
c 1
b 0
f 0
cc 11
eloc 30
nc 24
nop 4

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php declare(strict_types = 1);
2
/*
3
 * This file is part of the KleijnWeb\ApiDescriptions package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
9
namespace KleijnWeb\ApiDescriptions\Description\OpenApi;
10
11
use KleijnWeb\ApiDescriptions\Description\Operation;
12
use KleijnWeb\ApiDescriptions\Description\Schema;
13
14
/**
15
 * @author John Kleijn <[email protected]>
16
 */
17
class OpenApiOperation extends Operation
18
{
19
    /**
20
     * Operation constructor.
21
     *
22
     * @param \stdClass $definition
23
     * @param string    $path
24
     * @param string    $method
25
     * @param array     $pathParameters
26
     */
27
    public function __construct(\stdClass $definition, string $path, string $method, array $pathParameters = [])
28
    {
29
        $this->path       = $path;
30
        $this->method     = $method;
31
        $this->parameters = $pathParameters;
32
33
        if (isset($definition->parameters)) {
34
            foreach ($definition->parameters as $parameterDefinition) {
35
                $this->parameters[] = new OpenApiParameter($parameterDefinition);
36
            }
37
        }
38
39
        if (isset($definition->responses)) {
40
            $hasOkResponse = false;
41
            foreach ($definition->responses as $code => $responseDefinition) {
42
                $code = (string)$code;
43
                if ($code === 'default' || substr((string)$code, 1) === '1') {
44
                    $hasOkResponse = true;
45
                }
46
                $code                   = (int)$code;
47
                $this->responses[$code] = new OpenApiResponse($code, $responseDefinition);
48
            }
49
            if (!$hasOkResponse) {
50
                $this->responses[200] = new OpenApiResponse(200, (object)[]);
51
            }
52
        }
53
54
        $schemaDefinition = (object)[];
55
        if (!isset($definition->parameters)) {
56
            $schemaDefinition->type = 'null';
57
            $this->requestSchema    = Schema::get($schemaDefinition);
58
        } else {
59
            $schemaDefinition->type       = 'object';
60
            $schemaDefinition->required   = [];
61
            $schemaDefinition->properties = (object)[];
62
63
            foreach ($this->parameters as $parameter) {
64
                if ($parameter->isRequired()) {
65
                    $schemaDefinition->required[] = $parameter->getName();
66
                }
67
                $schemaDefinition->properties->{$parameter->getName()} = $parameter->getSchema()->getDefinition();
68
            }
69
70
            $this->requestSchema = Schema::get($schemaDefinition);
71
        }
72
    }
73
}
74