ClubCollectApiException   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 42
rs 10
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A createFromResponse() 0 6 1
A createFromGuzzleException() 0 11 4
1
<?php
2
declare(strict_types=1);
3
4
namespace SetBased\ClubCollect\Exception;
5
6
use GuzzleHttp\Exception\GuzzleException;
7
use Psr\Http\Message\ResponseInterface;
8
use SetBased\Exception\FormattedException;
9
10
/**
11
 *
12
 */
13
class ClubCollectApiException extends \Exception
14
{
15
  //--------------------------------------------------------------------------------------------------------------------
16
  use FormattedException;
17
18
  //--------------------------------------------------------------------------------------------------------------------
19
  /**
20
   * Creates an exception given a Guzzle exception.
21
   *
22
   * @param GuzzleException $exception The Guzzle exception.
23
   *
24
   * @return static
25
   *
26
   * @throws static
27
   */
28
  public static function createFromGuzzleException(GuzzleException $exception): self
29
  {
30
    if (method_exists($exception, 'hasResponse') && method_exists($exception, 'getResponse'))
31
    {
32
      if ($exception->hasResponse())
33
      {
34
        return static::createFromResponse($exception->getResponse());
35
      }
36
    }
37
38
    return new static([$exception->getCode()], $exception->getMessage());
39
  }
40
41
  //--------------------------------------------------------------------------------------------------------------------
42
  /**
43
   * Creates an exception given the response from the http client.
44
   *
45
   * @param ResponseInterface $response The response from the http client.
46
   *
47
   * @return static
48
   */
49
  public static function createFromResponse(ResponseInterface $response): self
50
  {
51
    return new static([$response->getStatusCode()],
52
                      "Error executing API call, status %d: %s",
53
                      $response->getStatusCode(),
54
                      $response->getBody());
55
  }
56
57
  //--------------------------------------------------------------------------------------------------------------------
58
}
59
60
//----------------------------------------------------------------------------------------------------------------------
61