Completed
Push — master ( 3fbfb5...43cfb4 )
by Kirill
116:33 queued 110:37
created

HttpAdapter   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 4
dl 0
loc 97
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A injectToken() 0 9 1
A request() 0 21 2
A prepareRequestOptions() 0 10 3
A parseResponse() 0 10 2
1
<?php declare(strict_types=1);
2
/**
3
 * This file is part of GitterApi package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
namespace Gitter\Adapters;
9
10
use Gitter\Client;
11
use Gitter\Route;
12
use GuzzleHttp\Client as Guzzle;
13
use Psr\Http\Message\ResponseInterface;
14
15
/**
16
 * Class HttpAdapter
17
 * @package Gitter\Adapters
18
 */
19
class HttpAdapter extends AbstractClient implements SyncAdapterInterface
20
{
21
    /**
22
     * @var Guzzle
23
     */
24
    private $guzzle;
25
26
    /**
27
     * @var Client
28
     */
29
    private $client;
30
31
    /**
32
     * HttpAdapter constructor.
33
     * @param Client $client
34
     */
35
    public function __construct(Client $client)
36
    {
37
        $this->client  = $client;
38
        $this->options = $this->injectToken($client, []);
39
        $this->guzzle  = new Guzzle($this->options);
40
    }
41
42
    /**
43
     * @param Client $client
44
     * @param array $options
45
     * @return array
46
     */
47
    private function injectToken(Client $client, array $options)
48
    {
49
        $options['headers'] = array_merge(
50
            $options['headers'] ?? [],
51
            $this->buildHeaders($client)
52
        );
53
54
        return $options;
55
    }
56
57
    /**
58
     * @param Route $route
59
     * @return array
60
     * @throws \RuntimeException
61
     * @throws \InvalidArgumentException
62
     */
63
    public function request(Route $route): array
64
    {
65
        list($method, $uri) = [$route->method(), $route->build()];
66
        $options = $this->prepareRequestOptions($route);
67
68
        // Log request
69
        $this->debugLog($this->client, ' -> ' . $method . ' ' . $uri);
70
        if ($options['body'] ?? false) {
71
            $this->debugLog($this->client, '    -> body ' . $options['body']);
72
        }
73
74
        // End log request
75
        $response = $this->guzzle->request($method, $uri, $options);
76
77
        // Log response
78
        $this->debugLog($this->client, ' <- ' . $response->getStatusCode() . ' ' . $response->getReasonPhrase());
79
        $this->debugLog($this->client, '   <- ' . (string)$response->getBody());
80
        // End log response
81
82
        return $this->parseResponse($response);
83
    }
84
85
    /**
86
     * @param Route $route
87
     * @return array
88
     */
89
    private function prepareRequestOptions(Route $route): array
90
    {
91
        $options = [];
92
93
        if ($route->method() !== 'GET' && $route->getBody() !== null) {
94
            $options['body'] = $route->getBody();
95
        }
96
97
        return array_merge($this->options, $options);
98
    }
99
100
    /**
101
     * @param ResponseInterface $response
102
     * @return array
103
     * @throws \RuntimeException
104
     */
105
    private function parseResponse(ResponseInterface $response): array
106
    {
107
        $data = json_decode((string)$response->getBody(), true);
108
109
        if (json_last_error() !== JSON_ERROR_NONE) {
110
            throw new \RuntimeException(json_last_error_msg());
111
        }
112
113
        return $data;
114
    }
115
}
116