GuzzleClient::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 1
1
<?php
2
 
3
namespace PatricPoba\MtnMomo\Http;
4
5
use GuzzleHttp\Client; 
6
use GuzzleHttp\ClientInterface; 
7
use PatricPoba\MtnMomo\Exceptions\MtnMomoException;
8
9
class GuzzleClient implements HttpClientInterface
10
{
11
    /**
12
     * @var ClientInterface
13
     */
14
    private $client;
15
16
    /**
17
     * Instance of this HttpCLient (Singleton Pattern)
18
     *
19
     * @var [type]
20
     */
21
    private static $instance;
22
23
24
    public function __construct(ClientInterface $client = null)
25
    { 
26
        // $this->client = $client ?? new Client();  
27
        $this->client = $client ?? new Client(['http_errors' => false]);
28
    }
29
 
30
    /**
31
     * Make an http request
32
     *
33
     * @param [type] $method
0 ignored issues
show
Documentation introduced by
The doc-type [type] could not be parsed: Unknown type name "" at position 0. [(view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
34
     * @param [type] $url
0 ignored issues
show
Documentation introduced by
The doc-type [type] could not be parsed: Unknown type name "" at position 0. [(view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
35
     * @param array $params
36
     * @param array $headers
37
     * @return \PatricPoba\MtnMomo\Http\Response
38
     */
39
    public function request($method, $url, $params = [], $headers = [])
40
    {
41
        try {
42
            // $response = $this->client->send(
43
            //     new \GuzzleHttp\Psr7\Request\Request($method, $url, $headers), ['json' => $params]
44
            // );
45
46
            $response = $this->client->request($method, $url, [
47
                'headers' => $headers,
48
                'json' => $params
49
            ]);
50
 
51
        } catch (\Exception $exception) {
52
            throw new MtnMomoException("HTTP request failed: {$url} " . $exception->getMessage(), null, $exception); 
53
        }
54
55
        // Casting the body (stream) to a string performs a rewind, ensuring we return the entire response.
56
        // See https://stackoverflow.com/a/30549372/86696
57
        return new ApiResponse($response->getStatusCode(), (string) $response->getBody(), $response->getHeaders());
58
    }
59
 
60
}
61