GuzzleConnection   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 3
Bugs 1 Features 2
Metric Value
wmc 7
c 3
b 1
f 2
lcom 1
cbo 4
dl 0
loc 92
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A makeConnection() 0 8 1
A setConnection() 0 10 2
A getResult() 0 6 1
A getHttpStatus() 0 6 1
A isNotEmptyResponse() 0 6 2
1
<?php
2
3
namespace ComicVine\Api\Connection;
4
5
use GuzzleHttp\Client;
6
use ComicVine\Exceptions\InvalidUrl;
7
use ComicVine\Exceptions\EmptyResponse;
8
use Psr\Http\Message\ResponseInterface;
9
10
/**
11
 * Extending Connection contract. Make connection
12
 * to ComicVine using GuzzleHttp package.
13
 *
14
 * Class GuzzleConnection
15
 *
16
 * @package ComicVine\Api\Connection
17
 * @author  Grzegorz Gajda <[email protected]>
18
 */
19
class GuzzleConnection implements Connection
20
{
21
22
    /**
23
     * GuzzleHttp Client instance.
24
     *
25
     * @var \GuzzleHttp\Client
26
     */
27
    protected $guzzle;
28
29
    /**
30
     * Promise for GET request.
31
     *
32
     * @var \Psr\Http\Message\ResponseInterface
33
     */
34
    protected $response;
35
36
    /**
37
     * Make instance for new Guzzle Client.
38
     *
39
     * @param int    $timeout   Timeout for request.
40
     * @param string $userAgent Header for user agent.
41
     *
42
     * @return $this
43
     */
44
    public function makeConnection($timeout = 30, $userAgent = "")
45
    {
46
        $this->guzzle = new Client([
47
            'timeout' => $timeout,
48
        ]);
49
50
        return $this;
51
    }
52
53
    /**
54
     * Make GET request.
55
     *
56
     * @param string $url Request URL.
57
     *
58
     * @return $this
59
     * @throws \ComicVine\Exceptions\InvalidUrl
60
     */
61
    public function setConnection($url)
62
    {
63
        if (filter_var($url, FILTER_VALIDATE_URL) === false) {
64
            throw new InvalidUrl("Passed wrong URL");
65
        }
66
67
        $this->response = $this->guzzle->request('GET', $url);
68
69
        return $this;
70
    }
71
72
    /**
73
     * Return response from request.
74
     *
75
     * @return mixed
76
     * @throws \ComicVine\Exceptions\EmptyResponse
77
     */
78
    public function getResult()
79
    {
80
        $this->isNotEmptyResponse();
81
82
        return $this->response->getBody();
83
    }
84
85
    /**
86
     * Return status code of request.
87
     *
88
     * @return mixed
89
     * @throws \ComicVine\Exceptions\EmptyResponse
90
     */
91
    public function getHttpStatus()
92
    {
93
        $this->isNotEmptyResponse();
94
95
        return $this->response->getStatusCode();
96
    }
97
98
    /**
99
     * Check is response attribute is not empty.
100
     *
101
     * @throws \ComicVine\Exceptions\EmptyResponse
102
     */
103
    protected function isNotEmptyResponse()
104
    {
105
        if (empty($this->response) === true) {
106
            throw new EmptyResponse("Request has not been executed.");
107
        }
108
    }
109
110
}