ApiRouteSpec   A
last analyzed

Complexity

Total Complexity 34

Size/Duplication

Total Lines 259
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Importance

Changes 0
Metric Value
wmc 34
lcom 2
cbo 1
dl 0
loc 259
rs 9.2
c 0
b 0
f 0

23 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 14 3
A setDescription() 0 4 1
A getDescription() 0 4 1
A setPath() 0 8 2
A getPath() 0 4 1
A setMethod() 0 4 1
A getMethod() 0 4 1
C setParameters() 0 26 7
A getParameters() 0 4 1
A setPriority() 0 4 1
A getPriority() 0 4 1
A setFormat() 0 4 1
A getFormat() 0 4 1
A setExample() 0 4 1
A getExample() 0 4 1
A setSection() 0 4 1
A getSection() 0 4 1
A setTags() 0 4 1
A getTags() 0 17 3
A setResponseCodes() 0 4 1
A getResponseCodes() 0 4 1
A setDisable() 0 4 1
A getDisable() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * @copyright   Copyright (c) 2016 ublaboo <[email protected]>
7
 * @author      Pavel Janda <[email protected]>
8
 * @package     Ublaboo
9
 */
10
11
namespace Ublaboo\ApiRouter;
12
13
use Doctrine\Common\Annotations\Annotation\Enum;
14
use Ublaboo\ApiRouter\Exception\ApiRouteWrongPropertyException;
15
16
abstract class ApiRouteSpec
17
{
18
19
	/**
20
	 * @var string|null
21
	 */
22
	protected $description;
23
24
	/**
25
	 * @var string
26
	 */
27
	protected $path = '/';
28
29
	/**
30
	 * @Enum({"CREATE", "READ", "UPDATE", "DELETE", "OPTIONS"})
31
	 * @var string
32
	 */
33
	protected $method;
34
35
	/**
36
	 * @var array
37
	 */
38
	protected $parameters = [];
39
40
	/**
41
	 * @var array
42
	 */
43
	protected $parameters_infos = ['requirement', 'type', 'description', 'default'];
44
45
	/**
46
	 * @var int
47
	 */
48
	protected $priority = 0;
49
50
	/**
51
	 * @Enum({"json", "xml"})
52
	 * @var string
53
	 */
54
	protected $format = 'json';
55
56
	/**
57
	 * @var array|null
58
	 */
59
	protected $example;
60
61
	/**
62
	 * @var string|null
63
	 */
64
	protected $section;
65
66
	/**
67
	 * @var array
68
	 */
69
	protected $tags = [];
70
71
	/**
72
	 * @var array
73
	 */
74
	protected $response_codes = [];
75
76
	/**
77
	 * @Enum({true, false})
78
	 * @var bool
79
	 */
80
	protected $disable = false;
81
82
83
	/**
84
	 * @param array $data
85
	 */
86
	public function __construct(array $data)
87
	{
88
		foreach ($data as $key => $value) {
89
			$method = 'set' . str_replace('_', '', ucwords($key, '_'));
90
91
			if (!method_exists($this, $method)) {
92
				throw new ApiRouteWrongPropertyException(
93
					sprintf('Unknown property "%s" on annotation "%s"', $key, get_class($this))
94
				);
95
			}
96
97
			$this->$method($value);
98
		}
99
	}
100
101
102
	public function setDescription(?string $description): void
103
	{
104
		$this->description = $description;
105
	}
106
107
108
	public function getDescription(): ?string
109
	{
110
		return $this->description;
111
	}
112
113
114
	protected function setPath(string $path): void
115
	{
116
		if (!$path) {
117
			throw new ApiRouteWrongPropertyException('ApiRoute path can not be empty');
118
		}
119
120
		$this->path = (string) $path;
121
	}
122
123
124
	public function getPath(): string
125
	{
126
		return $this->path;
127
	}
128
129
130
	protected function setMethod(string $method): void
131
	{
132
		$this->method = strtoupper($method);
133
	}
134
135
136
	public function getMethod(): string
137
	{
138
		return $this->method;
139
	}
140
141
142
	/**
143
	 * @throws ApiRouteWrongPropertyException
144
	 */
145
	protected function setParameters(array $parameters): void
146
	{
147
		foreach ($parameters as $key => $info) {
148
			if (strpos($this->getPath(), "<{$key}>") === false) {
149
				throw new ApiRouteWrongPropertyException("Parameter <$key> is not present in the url mask");
150
			}
151
152
			foreach ($info as $info_key => $value) {
153
				if (!in_array($info_key, $this->parameters_infos, true)) {
154
					throw new ApiRouteWrongPropertyException(sprintf(
155
						'You cat set only these description informations: [%s] - "%s" given',
156
						implode(', ', $this->parameters_infos),
157
						$info_key
158
					));
159
				}
160
161
				if (!is_scalar($value) && $value !== null) {
162
					throw new ApiRouteWrongPropertyException(
163
						"You cat set only scalar parameters informations (key [{$info_key}])"
164
					);
165
				}
166
			}
167
		}
168
169
		$this->parameters = $parameters;
170
	}
171
172
173
	public function getParameters(): array
174
	{
175
		return $this->parameters;
176
	}
177
178
179
	public function setPriority(int $priority): void
180
	{
181
		$this->priority = $priority;
182
	}
183
184
185
	public function getPriority(): int
186
	{
187
		return $this->priority;
188
	}
189
190
191
	public function setFormat(string $format): void
192
	{
193
		$this->format = $format;
194
	}
195
196
197
	public function getFormat(): string
198
	{
199
		return $this->format;
200
	}
201
202
203
	public function setExample(?array $example): void
204
	{
205
		$this->example = $example;
206
	}
207
208
209
	public function getExample(): ?array
210
	{
211
		return $this->example;
212
	}
213
214
215
	public function setSection(?string $section): void
216
	{
217
		$this->section = $section;
218
	}
219
220
221
	public function getSection(): ?string
222
	{
223
		return $this->section;
224
	}
225
226
227
	public function setTags(array $tags): void
228
	{
229
		$this->tags = $tags;
230
	}
231
232
233
	public function getTags(): array
234
	{
235
		$return = [];
236
237
		/**
238
		 * Tag may be saves aither with color: [tagName => color] or without: [tagName]
239
		 */
240
		foreach ($this->tags as $tag => $color) {
241
			if (is_numeric($tag)) {
242
				$return[$color] = '#9b59b6';
243
			} else {
244
				$return[$tag] = $color;
245
			}
246
		}
247
248
		return $return;
249
	}
250
251
252
	public function setResponseCodes(array $response_codes): void
253
	{
254
		$this->response_codes = $response_codes;
255
	}
256
257
258
	public function getResponseCodes(): array
259
	{
260
		return $this->response_codes;
261
	}
262
263
264
	public function setDisable(bool $disable): void
265
	{
266
		$this->disable = (bool) $disable;
267
	}
268
269
270
	public function getDisable(): bool
271
	{
272
		return $this->disable;
273
	}
274
}
275