Completed
Pull Request — master (#81)
by John
03:37
created

Specification::getOperations()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 12
rs 9.4285
cc 3
eloc 6
nc 3
nop 0
1
<?php declare(strict_types = 1);
2
/*
3
 * This file is part of the KleijnWeb\SwaggerBundle 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\SwaggerBundle\Document;
10
11
use KleijnWeb\SwaggerBundle\Document\Specification\Operation;
12
13
/**
14
 * @author John Kleijn <[email protected]>
15
 */
16
class Specification
17
{
18
    /**
19
     * @var object
20
     */
21
    private $definition;
22
23
    /**
24
     * @var Operation[]
25
     */
26
    private $operations;
27
28
    /**
29
     * @param \stdClass $definition
30
     */
31
    public function __construct(\stdClass $definition)
32
    {
33
        $this->definition = $definition;
34
    }
35
36
    /**
37
     * @param callable $f
38
     *
39
     * @return void
40
     */
41
    public function apply(callable  $f)
42
    {
43
        $recurse = function (&$item) use ($f, &$recurse) {
44
45
            foreach ($item as $attribute => &$value) {
46
                if (false === $f($value, $attribute)) {
47
                    return false;
48
                }
49
                if ($value === null) {
50
                    return true;
51
                }
52
                if (!is_scalar($value)) {
53
                    if (false === $recurse($value)) {
54
                        return false;
55
                    }
56
                }
57
            }
58
59
            return true;
60
        };
61
        $recurse($this->definition);
62
    }
63
64
    /**
65
     * @return \stdClass
66
     */
67
    public function getDefinition(): \stdClass
68
    {
69
        return $this->definition;
70
    }
71
72
    /**
73
     * @return \stdClass
74
     */
75
    public function getPaths(): \stdClass
76
    {
77
        return $this->definition->paths;
78
    }
79
80
    /**
81
     * @param string $path
82
     * @param string $method
83
     *
84
     * @return Operation
85
     */
86
    public function getOperation(string $path, string $method): Operation
87
    {
88
        $operation = new Operation($this, $path, $method);
89
90
        if (isset($this->operations[$operation->getId()])) {
91
            return $this->operations[$operation->getId()];
92
        }
93
94
        return $this->operations[$operation->getId()] = $operation;
95
    }
96
97
    /**
98
     * @return Operation[]
99
     */
100
    public function getOperations(): array
101
    {
102
        $operations = [];
103
104
        foreach ($this->getPaths() as $path => $pathItem) {
0 ignored issues
show
Bug introduced by
The expression $this->getPaths() of type object<stdClass> is not traversable.
Loading history...
105
            foreach (array_keys((array)$pathItem) as $method) {
106
                $operations[] = $this->getOperation($path, $method);
107
            }
108
        }
109
110
        return $operations;
111
    }
112
113
    /**
114
     * @deprecated
115
     *
116
     * @param string $path
117
     * @param string $method
118
     *
119
     * @return \stdClass
120
     */
121
    public function getOperationDefinition(string $path, string $method): \stdClass
122
    {
123
        return $this->getOperation($path, $method)->getDefinition();
124
    }
125
}
126