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

OpenApiPath::__construct()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.2
c 0
b 0
f 0
cc 4
eloc 10
nc 4
nop 2
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\Path;
13
14
/**
15
 * @author John Kleijn <[email protected]>
16
 */
17
class OpenApiPath extends Path
18
{
19
    /**
20
     * Path constructor.
21
     *
22
     * @param string    $path
23
     * @param \stdClass $definition
24
     */
25
    public function __construct(string $path, \stdClass $definition)
26
    {
27
        $this->path = $path;
28
        $definition = clone $definition;
29
        if (isset($definition->parameters)) {
30
            foreach ($definition->parameters as $parameterDefinition) {
31
                $this->pathParameters[] = new OpenApiParameter($parameterDefinition);
32
            }
33
            unset($definition->parameters);
34
        }
35
36
        foreach ($definition as $method => $operationDefinition) {
0 ignored issues
show
Bug introduced by
The expression $definition of type object<stdClass> is not traversable.
Loading history...
37
            $method                    = strtolower($method);
38
            $this->operations[$method] = $this->createOperation($method, $operationDefinition);
39
        }
40
    }
41
42
    /**
43
     * @param string    $method
44
     * @param \stdClass $operationDefinition
45
     *
46
     * @return Operation
47
     */
48
    protected function createOperation(string $method, \stdClass $operationDefinition): Operation
49
    {
50
        return new OpenApiOperation(
51
            $operationDefinition,
52
            $this->getPath(),
53
            $method,
54
            $this->pathParameters
55
        );
56
    }
57
}
58