Completed
Push — master ( 5c06ae...c9967d )
by Elf
02:12
created

HttpClient::getResponse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 4
ccs 0
cts 4
cp 0
crap 2
rs 10
1
<?php
2
3
namespace ElfSundae\Laravel\Helper;
4
5
use Exception;
6
use GuzzleHttp\Client;
7
use Illuminate\Support\Arr;
8
9
class HttpClient
10
{
11
    /**
12
     * The Guzzle client.
13
     *
14
     * @var \GuzzleHttp\Client
15
     */
16
    protected $client;
17
18
    /**
19
     * The Guzzle response.
20
     *
21
     * @var \GuzzleHttp\Psr7\Response
22
     */
23
    protected $response;
24
25
    /**
26
     * Create a http client instance.
27
     *
28
     * @param  array  $config
29
     */
30
    public function __construct($config = [])
31
    {
32
        $config = array_merge([
33
            'connect_timeout' => 5,
34
            'timeout' => 25,
35
        ], $config);
36
37
        $this->client = new Client($config);
38
    }
39
40
    /**
41
     * Get the Guzzle client instance.
42
     *
43
     * @return \GuzzleHttp\Client
44
     */
45
    public function getClient()
46
    {
47
        return $this->client;
48
    }
49
50
    /**
51
     * Get the Guzzle response instance.
52
     *
53
     * @return \GuzzleHttp\Psr7\Response|null
54
     */
55
    public function getResponse()
56
    {
57
        return $this->response;
58
    }
59
60
    /**
61
     * Get response body.
62
     *
63
     * @return \GuzzleHttp\Psr7\Stream|null
64
     */
65
    public function getBody()
66
    {
67
        if ($this->response) {
68
            return $this->response->getBody();
69
        }
70
    }
71
72
    /**
73
     * Get response content.
74
     *
75
     * @return string|null
76
     */
77
    public function getContent()
78
    {
79
        if ($body = $this->getBody()) {
80
            return (string) $body;
81
        }
82
    }
83
84
    /**
85
     * Get JSON decoded response content.
86
     *
87
     * @param  bool  $assoc
88
     * @return mixed
89
     */
90
    public function getJson($assoc = true)
91
    {
92
        if ($content = $this->getContent()) {
93
            return json_decode($content, $assoc);
94
        }
95
    }
96
97
    /**
98
     * Make request to an URL.
99
     *
100
     * @param  string  $url
101
     * @param  string  $method
102
     * @param  array  $options
103
     * @return $this
104
     */
105
    public function request($url, $method = 'GET', $options = [])
106
    {
107
        try {
108
            $this->response = $this->client->request($method, $url, $options);
109
        } catch (Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
110
        }
111
112
        return $this;
113
    }
114
115
    /**
116
     * Make request to an URL, expecting JSON content.
117
     *
118
     * @param  string  $url
119
     * @param  string  $method
120
     * @param  array  $options
121
     * @return $this
122
     */
123
    public function requestJson($url, $method = 'GET', $options = [])
124
    {
125
        Arr::set($options, 'headers.Accept', 'application/json');
126
127
        return $this->request($url, $method, $options);
128
    }
129
130
    /**
131
     * Request the URL and return the response content.
132
     *
133
     * @param  string  $url
134
     * @param  string  $method
135
     * @param  array  $options
136
     * @return string|null
137
     */
138
    public function fetchContent($url, $method = 'GET', $options = [])
139
    {
140
        return $this->request($url, $method, $options)->getContent();
141
    }
142
143
    /**
144
     * Request the URL and return the JSON decoded response content.
145
     *
146
     * @param  string  $url
147
     * @param  string  $method
148
     * @param  array  $options
149
     * @param  bool  $assoc
150
     * @return mixed
151
     */
152
    public function fetchJson($url, $method = 'GET', $options = [], $assoc = true)
153
    {
154
        return $this->requestJson($url, $method, $options)->getJson($assoc);
155
    }
156
}
157