| Total Complexity | 54 |
| Total Lines | 397 |
| Duplicated Lines | 0 % |
| Changes | 20 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Client often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Client, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 26 | class Client implements ClientInterface |
||
| 27 | { |
||
| 28 | |||
| 29 | /** @var string The API access token */ |
||
| 30 | private static $token = null; |
||
| 31 | |||
| 32 | /** @var string The instance token, settable once per new instance */ |
||
| 33 | private $instanceToken; |
||
| 34 | |||
| 35 | private $apiBaseUri; |
||
| 36 | |||
| 37 | private $apiQuery; |
||
| 38 | |||
| 39 | private $apiResource; |
||
| 40 | |||
| 41 | private $apiVersion; |
||
| 42 | |||
| 43 | private $validator; |
||
| 44 | |||
| 45 | private $guzzleClient; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @param string|null $token The API access token, as obtained on diffbot.com/dev |
||
| 49 | * @throws DiffbotException When no token is provided |
||
| 50 | */ |
||
| 51 | public function __construct($token = null) |
||
| 76 | } |
||
| 77 | } |
||
| 78 | } |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @todo debug flag |
||
| 82 | */ |
||
| 83 | protected function request(): ResponseInterface |
||
| 104 | } |
||
| 105 | } |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @todo |
||
| 109 | */ |
||
| 110 | public function getAttachment($attachmentId) |
||
| 125 | } |
||
| 126 | } |
||
| 127 | |||
| 128 | /** |
||
| 129 | * |
||
| 130 | * {@inheritDoc} |
||
| 131 | * @see \InShore\BookWhen\Interfaces\ClientInterface::getAttachments() |
||
| 132 | * @todo validate params. |
||
| 133 | */ |
||
| 134 | public function getAttachments($title = null, $fileName = null, $fileType = null): array |
||
| 135 | { |
||
| 136 | |||
| 137 | $this->apiResource = $this->apiVersion . '/attachments'; |
||
| 138 | |||
| 139 | try { |
||
| 140 | $return = []; |
||
| 141 | $Response = $this->request(); |
||
| 142 | $body = json_decode($Response->getBody()->getContents()); |
||
| 143 | |||
| 144 | foreach ($body->data as $attachment) { |
||
| 145 | array_push($return, $attachment); |
||
| 146 | } |
||
| 147 | |||
| 148 | return $return; |
||
| 149 | } catch (Exception $e) { |
||
| 150 | throw new RestException($e->getMessage()); |
||
| 151 | } |
||
| 152 | } |
||
| 153 | |||
| 154 | /** |
||
| 155 | * |
||
| 156 | * {@inheritDoc} |
||
| 157 | * @see \InShore\BookWhen\Interfaces\ClientInterface::getClassPass() |
||
| 158 | */ |
||
| 159 | public function getClassPass($classPassId) |
||
| 160 | { |
||
| 161 | $this->apiResource = $this->apiVersion . '/class_passes'; |
||
| 162 | |||
| 163 | if (!empty($classPassId && !$this->validator->validId($classPassId, 'classPass'))) { |
||
| 164 | throw ValidationException::class; |
||
| 165 | } |
||
| 166 | |||
| 167 | try { |
||
| 168 | $Response = $this->request(); |
||
| 169 | $body = json_decode($Response->getBody()->getContents()); |
||
| 170 | $classPass = $body->data[0]; |
||
| 171 | $return = $classPass; |
||
| 172 | return $return; |
||
| 173 | } catch (Exception $e) { |
||
| 174 | throw new RestException($e->getMessage());; |
||
| 175 | } |
||
| 176 | } |
||
| 177 | |||
| 178 | /** |
||
| 179 | * |
||
| 180 | * {@inheritDoc} |
||
| 181 | * @see \InShore\BookWhen\Interfaces\ClientInterface::getClassPasses() |
||
| 182 | */ |
||
| 183 | public function getClassPasses($title = null, $detail = null, $usageType, $cost = null, $usageAllowance = null, $useRestrictedForDays = null): array |
||
| 184 | { |
||
| 185 | $this->apiResource = $this->apiVersion . '/???'; |
||
| 186 | |||
| 187 | // @todo prepocess response onto nice model objects. |
||
| 188 | $Response = $this->request(); |
||
| 189 | return json_decode($Response->getBody()->getContents()); |
||
| 190 | } |
||
| 191 | |||
| 192 | /** |
||
| 193 | * |
||
| 194 | * {@inheritDoc} |
||
| 195 | * @see \InShore\BookWhen\Interfaces\ClientInterface::getEvent() |
||
| 196 | */ |
||
| 197 | public function getEvent($eventId) |
||
| 198 | { |
||
| 199 | if (!empty($eventId && !$this->validator->validId($eventId, 'event'))) { |
||
| 200 | throw new ValidationException(); |
||
| 201 | } |
||
| 202 | $this->apiResource = $this->apiVersion . '/events' . '/' . $eventId; |
||
| 203 | |||
| 204 | try { |
||
| 205 | $Response = $this->request(); |
||
| 206 | $body = json_decode($Response->getBody()->getContents()); |
||
| 207 | $event = $body->data; |
||
| 208 | $event->soldOut = (bool) ($event->attributes->attendee_count >= $event->attributes->attendee_limit); |
||
| 209 | $return = $event; |
||
| 210 | return $return; |
||
| 211 | } catch (Exception $e) { |
||
| 212 | throw new RestException($e->getMessage()); |
||
| 213 | } |
||
| 214 | } |
||
| 215 | |||
| 216 | /** |
||
| 217 | * |
||
| 218 | * {@inheritDoc} |
||
| 219 | * @see \InShore\BookWhen\Interfaces\ClientInterface::getEvents() |
||
| 220 | */ |
||
| 221 | public function getEvents( |
||
| 222 | $calendar = false, |
||
| 223 | $entry = false, |
||
| 224 | $location = [], |
||
| 225 | $tags = [], |
||
| 226 | $title = [], |
||
| 227 | $detail = [], |
||
| 228 | $from = null, |
||
| 229 | $to = null, |
||
| 230 | $includeLocation = false, |
||
| 231 | $includeAttachments = false, |
||
| 232 | $includeTickets = false, |
||
| 233 | $includeTicketsEvents = false, |
||
| 234 | $includeTicketsClassPasses = false): array |
||
| 235 | { |
||
| 236 | // Validate $tags. |
||
| 237 | if (!empty($tags)) { |
||
| 238 | if (!is_array($tags)) { |
||
| 239 | throw new ValidationException(); |
||
| 240 | } else { |
||
| 241 | $tags = array_unique($tags); |
||
| 242 | foreach ($tags as $tag) { |
||
| 243 | if (!empty($tag) && !$this->validator->validTag($tag)) { |
||
| 244 | throw new ValidationException(); |
||
| 245 | } |
||
| 246 | } |
||
| 247 | } |
||
| 248 | $this->apiQuery['filter[tag]'] = implode(',', $tags); |
||
| 249 | } |
||
| 250 | |||
| 251 | // Validate $from; |
||
| 252 | if (!empty($from)) { |
||
| 253 | if (!$this->validator->validFrom($from, $to)) { |
||
| 254 | throw new ValidationException(); |
||
| 255 | } else { |
||
| 256 | $this->apiQuery['filter[from]'] = $from; |
||
| 257 | } |
||
| 258 | } |
||
| 259 | |||
| 260 | // Validate $to; |
||
| 261 | if (!empty($to)) { |
||
| 262 | if (!$this->validator->validTo($to, $from)) { |
||
| 263 | throw ValidationException::class; |
||
| 264 | } else { |
||
| 265 | $this->apiQuery['filter[to]'] = $to; |
||
| 266 | } |
||
| 267 | } |
||
| 268 | |||
| 269 | // API resource. |
||
| 270 | $this->apiResource = $this->apiVersion . '/events'; |
||
| 271 | |||
| 272 | |||
| 273 | |||
| 274 | // Validate $includeLocation; |
||
| 275 | |||
| 276 | // Validate $includeTickets; |
||
| 277 | |||
| 278 | try { |
||
| 279 | $Response = $this->request(); |
||
| 280 | |||
| 281 | $body = json_decode($Response->getBody()->getContents()); |
||
| 282 | |||
| 283 | // Prepocess response onto nice model objects. |
||
| 284 | // @todo abstract. |
||
| 285 | $return = []; |
||
| 286 | |||
| 287 | foreach ($body->data as $event) { |
||
| 288 | // Add additional properties here. |
||
| 289 | $event->soldOut = (bool) ($event->attributes->attendee_count >= $event->attributes->attendee_limit); |
||
| 290 | array_push($return, $event); |
||
| 291 | } |
||
| 292 | |||
| 293 | return $return; |
||
| 294 | } catch (Exception $e) { |
||
| 295 | throw new RestException($e->getMessage()); |
||
| 296 | } |
||
| 297 | } |
||
| 298 | |||
| 299 | /** |
||
| 300 | * |
||
| 301 | * {@inheritDoc} |
||
| 302 | * @see \InShore\BookWhen\Interfaces\ClientInterface::getLocation() |
||
| 303 | */ |
||
| 304 | public function getLocation($locationId) |
||
| 319 | } |
||
| 320 | |||
| 321 | } |
||
| 322 | |||
| 323 | /** |
||
| 324 | * |
||
| 325 | * {@inheritDoc} |
||
| 326 | * @see \InShore\BookWhen\Interfaces\ClientInterface::getLocations() |
||
| 327 | * @todo validate params. |
||
| 328 | */ |
||
| 329 | public function getLocations($addressText = null, $additionalInfo = null): array |
||
| 330 | { |
||
| 331 | $this->apiResource = $this->apiVersion . '/locations'; |
||
| 332 | |||
| 333 | $return = []; |
||
| 334 | |||
| 335 | try { |
||
| 336 | $Response = $this->request(); |
||
| 337 | $body = json_decode($Response->getBody()->getContents()); |
||
| 338 | |||
| 339 | foreach ($body->data as $location) { |
||
| 340 | array_push($return, $location); |
||
| 341 | } |
||
| 342 | |||
| 343 | return $return; |
||
| 344 | } catch (Exception $e) { |
||
| 345 | throw new RestException($e->getMessage()); |
||
| 346 | } |
||
| 347 | } |
||
| 348 | |||
| 349 | /** |
||
| 350 | * |
||
| 351 | * {@inheritDoc} |
||
| 352 | * @see \InShore\BookWhen\Interfaces\ClientInterface::getTicket() |
||
| 353 | */ |
||
| 354 | public function getTicket($ticketId) |
||
| 355 | { |
||
| 356 | if (!empty($ticketId && !$this->validator->validId($ticketId, 'ticket'))) { |
||
| 357 | // throw new ValidationException(); |
||
| 358 | } |
||
| 359 | |||
| 360 | $this->apiResource = $this->apiVersion . '/tickets'; |
||
| 361 | |||
| 362 | |||
| 363 | try { |
||
| 364 | $Response = $this->request(); |
||
| 365 | $body = json_decode($Response->getBody()->getContents()); |
||
| 366 | $ticket = $body->data[0]; |
||
| 367 | $return = $ticket; |
||
| 368 | return $return; |
||
| 369 | } catch (Exception $e) { |
||
| 370 | throw new RestException($e->getMessage()); |
||
| 371 | } |
||
| 372 | } |
||
| 373 | |||
| 374 | /** |
||
| 375 | * |
||
| 376 | * {@inheritDoc} |
||
| 377 | * @see \InShore\BookWhen\Interfaces\ClientInterface::getTickets() |
||
| 378 | */ |
||
| 379 | public function getTickets($eventId): array |
||
| 380 | { |
||
| 381 | if (!$this->validator->validId($eventId, 'event')) { |
||
| 382 | throw new ValidationException(); |
||
| 383 | } |
||
| 384 | |||
| 385 | $this->apiQuery = ['event' => $eventId]; |
||
| 386 | |||
| 387 | $this->apiResource = $this->apiVersion . '/tickets'; |
||
| 388 | |||
| 389 | try { |
||
| 390 | $return = []; |
||
| 391 | |||
| 392 | $Response = $this->request(); |
||
| 393 | $body = json_decode($Response->getBody()->getContents()); |
||
| 394 | |||
| 395 | foreach ($body->data as $ticket) { |
||
| 396 | array_push($return, $ticket); |
||
| 397 | } |
||
| 398 | |||
| 399 | return $return; |
||
| 400 | } catch (Exception $e) { |
||
| 401 | throw new RestException($e->getMessage()); |
||
| 402 | } |
||
| 403 | } |
||
| 404 | |||
| 405 | /** |
||
| 406 | * Set Guzzle Client |
||
| 407 | */ |
||
| 408 | public function setGuzzleClient($guzzleClient) |
||
| 411 | } |
||
| 412 | |||
| 413 | /** |
||
| 414 | * Sets the token for all future new instances |
||
| 415 | * @param $token string The API access token, as obtained on diffbot.com/dev. |
||
| 416 | */ |
||
| 417 | public static function setToken($token) |
||
| 423 | } |
||
| 424 | } |
||
| 425 | |||
| 426 | // EOF! |
||
| 427 |