TheSportsDbClient   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 71.43%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 3
dl 0
loc 55
ccs 10
cts 14
cp 0.7143
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getBaseUrl() 0 3 1
A doRequest() 0 12 3
1
<?php
2
/**
3
 * @file
4
 * Contains \TheSportsDb\Http\TheSportsDbClient.
5
 */
6
namespace TheSportsDb\Http;
7
8
use GuzzleHttp\ClientInterface;
9
use GuzzleHttp\Exception\ClientException;
10
11
/**
12
 * Http client for thesportsdb.
13
 *
14
 * @author Jelle Sebreghts
15
 */
16
class TheSportsDbClient implements TheSportsDbClientInterface {
17
18
  /**
19
   * The API key.
20
   *
21
   * @var string
22
   */
23
  protected $apiKey;
24
25
  /**
26
   * HTTP Client to fetch data.
27
   *
28
   * @var \GuzzleHttp\ClientInterface
29
   */
30
  protected $httpClient;
31
32
  /**
33
   * Creates a \TheSportsDb\Http\TheSportsDbClient object.
34
   *
35
   * @param string $apiKey
36
   *   The api key to connect to the sports db service.
37
   * @param ClientInterface $httpClient
38
   *   The HTTP client that will make the requests.
39
   */
40
  public function __construct($apiKey, ClientInterface $httpClient) {
41
    $this->apiKey = $apiKey;
42
    $this->httpClient = $httpClient;
43
  }
44
45
  /**
46
   * Get the base url for requests.
47
   *
48
   * @return string
49
   */
50 1
  protected function getBaseUrl() {
51 1
    return 'https://thesportsdb.com/api/v1/json/' . $this->apiKey . '/';
52
  }
53
54
  /**
55
   * {@inheritdoc}
56
   */
57 1
  public function doRequest($endpoint, array $parameters = array()) {
58 1
    $url = $this->getBaseUrl() . $endpoint;
59
    try {
60 1
      $response = $this->httpClient->request('GET', $url, array('query' => array_filter($parameters)));
61 1
      if ($response->getStatusCode() == 200) {
62 1
        return json_decode($response->getBody()->getContents());
63
      }
64 1
    } catch (ClientException $e) {
65 1
      $response = $e->getResponse();
66
    }
67 1
    throw new \Exception('Request to ' . $url . ' failed: ' . $response->getStatusCode() . ' ' . $response->getReasonPhrase() . '.');
68
  }
69
70
}
71