Client   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 21
c 3
b 0
f 0
dl 0
loc 99
rs 10
wmc 8

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getHandlerStack() 0 11 2
A getHttpClient() 0 7 1
A __construct() 0 3 2
A containsAccessToken() 0 7 2
A get() 0 6 1
1
<?php
2
namespace Shobi\Weatherapp\Http;
3
4
5
use GuzzleHttp\Psr7\Uri;
6
use GuzzleHttp\Middleware;
7
use GuzzleHttp\HandlerStack;
8
use GuzzleHttp\Handler\CurlHandler;
9
use GuzzleHttp\Client as GuzzleClient;
10
use Psr\Http\Message\RequestInterface;
11
use Shobi\Weatherapp\Contracts\Client as ClientContract;
12
13
class Client implements ClientContract
14
{
15
    /**
16
     * The API's base URI
17
     */
18
    const BASE_URL = 'api.openweathermap.org/data/';
19
20
    /**
21
     * The API's version string
22
     */
23
    const VERSION = '2.5';
24
25
    /**
26
     * The HTTP client
27
     *
28
     * @var \GuzzleHttp\Client
29
     */
30
    protected $http;
31
32
    /**
33
     * Construct the HTTP client and passing the
34
     * token for authorization
35
     *
36
     * @param string $token
37
     */
38
    public function __construct(string $token)
39
    {
40
        $this->http = $this->http ? : $this->getHttpClient($token);
41
    }
42
43
    /**
44
     * Sends a GET Request
45
     *
46
     * @param string $uri
47
     * @param array $query
48
     * @param array $options
49
     *
50
     * @return string JSON
51
     */
52
    public function get(string $uri, array $query = [], array $options = []) : string
53
    {
54
        $uri      = $uri .'?'. http_build_query($query);
55
        $response = $this->http->request('GET', $uri, $options);
56
57
        return (string) $response->getBody()->getContents();
58
    }
59
60
    /**
61
     * Constructs the HTTP client
62
     *
63
     * @param string $token
64
     *
65
     * @return \GuzzleHttp\Client
66
     */
67
    private function getHttpClient(string $token)
68
    {
69
        $stack = $this->getHandlerStack($token);
70
71
        return new GuzzleClient([
72
            'base_uri' => self::BASE_URL . self::VERSION . '/',
73
            'handler'  => $stack
74
        ]);
75
    }
76
77
    /**
78
     * Creates Guzzle Handler stack
79
     * Also appends the APP ID to every request
80
     *
81
     * @param string $token
82
     *
83
     * @return \GuzzleHttp\HandlerStack $stack
84
     */
85
    private function getHandlerStack(string $token)
86
    {
87
        $stack = HandlerStack::create(new CurlHandler());
88
89
        $stack->push(Middleware::mapRequest(function (RequestInterface $request) use($token) {
90
            return ! $this->containsAccessToken($request)
91
                ? $request->withUri(Uri::withQueryValue($request->getUri(), 'APPID', $token))
92
                : $request;
93
        }));
94
95
        return $stack;
96
    }
97
98
    /**
99
     * Checks if the request already contains an oauth_token
100
     *
101
     * @param \Psr\Http\Message\RequestInterface $request
102
     *
103
     * @return bool
104
     */
105
    private function containsAccessToken(RequestInterface $request)
106
    {
107
        if (strpos($request->getUri()->getQuery(), 'APPID') !== false) {
108
            return true;
109
        }
110
111
        return false;
112
    }
113
}