createFromGuzzleException()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 11
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 4
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 11
rs 10
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