Completed
Push — master ( e54b6a...c762b0 )
by John
03:11
created

RamlOperation::__construct()   C

Complexity

Conditions 7
Paths 12

Size

Total Lines 37
Code Lines 24

Duplication

Lines 17
Ratio 45.95 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 17
loc 37
rs 6.7272
c 1
b 0
f 0
cc 7
eloc 24
nc 12
nop 4
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\Standard\Raml;
10
11
use KleijnWeb\ApiDescriptions\Description\Operation;
12
use KleijnWeb\ApiDescriptions\Description\Parameter;
13
use KleijnWeb\ApiDescriptions\Description\Schema;
14
15
/**
16
 * @author John Kleijn <[email protected]>
17
 */
18
class RamlOperation extends Operation
19
{
20
    /**
21
     * Operation constructor.
22
     *
23
     * @param \stdClass $definition
24
     * @param string    $path
25
     * @param string    $method
26
     * @param array     $pathParameters
27
     */
28
    public function __construct(\stdClass $definition, string $path, string $method, array $pathParameters = [])
29
    {
30
        $this->path       = $path;
31
        $this->method     = $method;
32
        $this->parameters = array_merge($pathParameters, self::extractParameters($definition));
0 ignored issues
show
Documentation Bug introduced by
It seems like array_merge($pathParamet...arameters($definition)) of type array is incompatible with the declared type array<integer,object<Kle...Description\Parameter>> of property $parameters.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
33
34
35
        if (isset($definition->responses)) {
36
            $hasOkResponse = false;
37
            foreach ($definition->responses as $code => $responseDefinition) {
38
                $code                   = (int)$code;
39
                $this->responses[$code] = new RamlResponse($code, $responseDefinition);
40
            }
41
            if (!$hasOkResponse) {
42
                $this->responses[200] = new RamlResponse(200, (object)[]);
43
            }
44
        }
45
46
        $schemaDefinition = (object)[];
47 View Code Duplication
        if (!count($this->parameters)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
48
            $schemaDefinition->type = 'null';
49
            $this->requestSchema    = Schema::get($schemaDefinition);
50
        } else {
51
            $schemaDefinition->type       = 'object';
52
            $schemaDefinition->required   = [];
53
            $schemaDefinition->properties = (object)[];
54
55
            foreach ($this->parameters as $parameter) {
56
                if ($parameter->isRequired()) {
57
                    $schemaDefinition->required[] = $parameter->getName();
58
                }
59
                $schemaDefinition->properties->{$parameter->getName()} = $parameter->getSchema()->getDefinition();
60
            }
61
62
            $this->requestSchema = Schema::get($schemaDefinition);
63
        }
64
    }
65
66
    /**
67
     * @param \stdClass $definition
68
     *
69
     * @return array
70
     */
71
    public static function extractParameters(\stdClass $definition)
72
    {
73
        $parameters = [];
74
75 View Code Duplication
        if (isset($definition->queryParameters)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
76
            foreach ($definition->queryParameters as $name => $parameterDefinition) {
77
                $parameters[] = new RamlParameter($name, Parameter::IN_QUERY, $parameterDefinition);
78
            }
79
        }
80 View Code Duplication
        if (isset($definition->uriParameters)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
81
            foreach ($definition->uriParameters as $name => $parameterDefinition) {
82
                $parameters[] = new RamlParameter($name, Parameter::IN_PATH, $parameterDefinition);
83
            }
84
        }
85
86
        return $parameters;
87
    }
88
}
89