Completed
Pull Request — master (#20)
by Woody
02:17 queued 12s
created

RequestDefinition   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 155
Duplicated Lines 0 %

Test Coverage

Coverage 60%

Importance

Changes 0
Metric Value
wmc 21
eloc 52
dl 0
loc 155
ccs 39
cts 65
cp 0.6
rs 10
c 0
b 0
f 0

18 Methods

Rating   Name   Duplication   Size   Complexity  
A getPathSchema() 0 3 1
A getMethod() 0 3 1
A getResponseDefinition() 0 16 3
A getBodySchema() 0 3 1
A getContentTypes() 0 3 1
A getOperationId() 0 3 1
A hasQueryParametersSchema() 0 3 1
A getHeadersSchema() 0 3 1
A hasBodySchema() 0 3 1
A hasHeadersSchema() 0 3 1
A unserialize() 0 9 1
A serialize() 0 9 1
A __construct() 0 15 2
A getRequestParameters() 0 3 1
A addResponseDefinition() 0 3 1
A getQueryParametersSchema() 0 3 1
A hasPathSchema() 0 3 1
A getPathTemplate() 0 3 1
1
<?php declare(strict_types=1);
2
3
namespace ElevenLabs\Api\Definition;
4
5
use stdClass;
6
7
class RequestDefinition implements \Serializable, MessageDefinition
8
{
9
    /** @var string */
10
    private $method;
11
12
    /** @var string */
13
    private $operationId;
14
15
    /** @var string */
16
    private $pathTemplate;
17
18
    /** @var Parameters */
19
    private $parameters;
20
21
    /** @var string[] */
22
    private $contentTypes;
23
24
    /** @var ResponseDefinition[] */
25
    private $responses;
26
27
    /**
28
     * @param string[] $contentTypes
29
     * @param ResponseDefinition[] $responses
30
     */
31 4
    public function __construct(
32
        string $method,
33
        string $operationId,
34
        string $pathTemplate,
35
        Parameters $parameters,
36
        array $contentTypes,
37
        array $responses
38
    ) {
39 4
        $this->method = $method;
40 4
        $this->operationId = $operationId;
41 4
        $this->pathTemplate = $pathTemplate;
42 4
        $this->parameters = $parameters;
43 4
        $this->contentTypes = $contentTypes;
44 4
        foreach ($responses as $response) {
45 2
            $this->addResponseDefinition($response);
46
        }
47 4
    }
48
49
    public function getMethod(): string
50
    {
51
        return $this->method;
52
    }
53
54
    public function getOperationId(): string
55
    {
56
        return $this->operationId;
57
    }
58
59
    public function getPathTemplate(): string
60
    {
61
        return $this->pathTemplate;
62
    }
63
64
    public function getRequestParameters(): Parameters
65
    {
66
        return $this->parameters;
67
    }
68
69
    public function getContentTypes(): array
70
    {
71
        return $this->contentTypes;
72
    }
73
74 3
    public function getResponseDefinition(int $statusCode): ResponseDefinition
75
    {
76 3
        if (isset($this->responses[$statusCode])) {
77 1
            return $this->responses[$statusCode];
78
        }
79
80 2
        if (isset($this->responses['default'])) {
81 1
            return $this->responses['default'];
82
        }
83
84 1
        throw new \InvalidArgumentException(
85 1
            sprintf(
86 1
                'No response definition for %s %s is available for status code %s',
87 1
                $this->method,
88 1
                $this->pathTemplate,
89 1
                $statusCode
90
            )
91
        );
92
    }
93
94
    public function hasBodySchema(): bool
95
    {
96
        return $this->parameters->hasBodySchema();
97
    }
98
99
    public function getBodySchema(): ?stdClass
100
    {
101
        return $this->parameters->getBodySchema();
102
    }
103
104
    public function hasHeadersSchema(): bool
105
    {
106
        return $this->parameters->hasHeadersSchema();
107
    }
108
109
    public function getHeadersSchema(): ?stdClass
110
    {
111
        return $this->parameters->getHeadersSchema();
112
    }
113
114
    public function hasPathSchema(): bool
115
    {
116
        return $this->parameters->hasPathSchema();
117
    }
118
119
    public function getPathSchema(): ?stdClass
120
    {
121
        return $this->parameters->getPathSchema();
122
    }
123
124
    public function hasQueryParametersSchema(): bool
125
    {
126
        return $this->parameters->hasQueryParametersSchema();
127
    }
128
129
    public function getQueryParametersSchema(): ?stdClass
130
    {
131
        return $this->parameters->getQueryParametersSchema();
132
    }
133
134
    // Serializable
135 1
    public function serialize()
136
    {
137 1
        return serialize([
138 1
            'method' => $this->method,
139 1
            'operationId' => $this->operationId,
140 1
            'pathTemplate' => $this->pathTemplate,
141 1
            'parameters' => $this->parameters,
142 1
            'contentTypes' => $this->contentTypes,
143 1
            'responses' => $this->responses
144
        ]);
145
    }
146
147
    // Serializable
148 1
    public function unserialize($serialized)
149
    {
150 1
        $data = unserialize($serialized);
151 1
        $this->method = $data['method'];
152 1
        $this->operationId = $data['operationId'];
153 1
        $this->pathTemplate = $data['pathTemplate'];
154 1
        $this->parameters = $data['parameters'];
155 1
        $this->contentTypes = $data['contentTypes'];
156 1
        $this->responses = $data['responses'];
157 1
    }
158
159 2
    private function addResponseDefinition(ResponseDefinition $response)
160
    {
161 2
        $this->responses[$response->getStatusCode()] = $response;
162 2
    }
163
}
164