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

Operation   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 129
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Importance

Changes 0
Metric Value
wmc 15
lcom 2
cbo 2
dl 0
loc 129
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A getPath() 0 4 1
A getId() 0 4 1
A getStatusCodes() 0 4 1
A getResponse() 0 15 3
A getResponses() 0 4 1
A getRequestSchema() 0 4 1
A hasParameters() 0 4 1
A getParameters() 0 4 2
A getParameter() 0 9 3
A getMethod() 0 4 1
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;
10
11
use KleijnWeb\ApiDescriptions\Description\Visitor\VisiteeMixin;
12
13
/**
14
 * @author John Kleijn <[email protected]>
15
 */
16
abstract class Operation implements Element
17
{
18
    use VisiteeMixin;
19
20
    /**
21
     * @var string
22
     */
23
    protected $path;
24
25
    /**
26
     * @var string
27
     */
28
    protected $method;
29
30
    /**
31
     * @var Parameter[]
32
     */
33
    protected $parameters;
34
35
    /**
36
     * @var Schema
37
     */
38
    protected $requestSchema;
39
40
    /**
41
     * @var Response[]
42
     */
43
    protected $responses;
44
45
    /**
46
     * @return string
47
     */
48
    public function getPath(): string
49
    {
50
        return $this->path;
51
    }
52
53
    /**
54
     * @return string
55
     */
56
    public function getId(): string
57
    {
58
        return "$this->path::$this->method";
59
    }
60
61
    /**
62
     * @return int[]
63
     */
64
    public function getStatusCodes(): array
65
    {
66
        return array_keys($this->responses);
67
    }
68
69
    /**
70
     * @param int $code
71
     *
72
     * @return Response
73
     */
74
    public function getResponse(int $code): Response
75
    {
76
        if (!isset($this->responses[$code])) {
77
            if (isset($this->responses[0])) {
78
                // Return default response
79
                return $this->responses[0];
80
            }
81
            throw new \InvalidArgumentException(
82
                "Operation '{$this->getId()}' does not have a definition for status '$code'" .
83
                " (has " . implode(', ', array_keys($this->responses)) . ')'
84
            );
85
        }
86
87
        return $this->responses[$code];
88
    }
89
90
    /**
91
     * @return Response[]
92
     */
93
    public function getResponses()
94
    {
95
        return array_values($this->responses);
96
    }
97
98
    /**
99
     * @return Schema
100
     */
101
    public function getRequestSchema(): Schema
102
    {
103
        return $this->requestSchema;
104
    }
105
106
    /**
107
     * @return bool
108
     */
109
    public function hasParameters(): bool
110
    {
111
        return (bool)count($this->parameters);
112
    }
113
114
    /**
115
     * @return  Parameter[]
116
     */
117
    public function getParameters(): array
118
    {
119
        return $this->hasParameters() ? $this->parameters : [];
120
    }
121
122
    /**
123
     * @param string $name
124
     *
125
     * @return Parameter
126
     */
127
    public function getParameter(string $name): Parameter
128
    {
129
        foreach ($this->getParameters() as $parameter) {
130
            if ($parameter->getName() === $name) {
131
                return $parameter;
132
            }
133
        }
134
        throw new \OutOfBoundsException("Parameter '$name' does not exist");
135
    }
136
137
    /**
138
     * @return string
139
     */
140
    public function getMethod(): string
141
    {
142
        return $this->method;
143
    }
144
}
145