InstagramGuzzleHttpClient   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 25
dl 0
loc 62
rs 10
c 0
b 0
f 0
wmc 8

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 2
A send() 0 26 4
A getHeadersAsString() 0 9 2
1
<?php
2
3
namespace Maztech\HttpClients;
4
5
use Maztech\Http\GraphRawResponse;
6
use Maztech\Exceptions\InstagramSDKException;
7
8
use GuzzleHttp\Client;
9
use GuzzleHttp\Message\ResponseInterface;
10
use GuzzleHttp\Ring\Exception\RingException;
11
use GuzzleHttp\Exception\RequestException;
12
13
class InstagramGuzzleHttpClient implements InstagramHttpClientInterface
14
{
15
    /**
16
     * @var \GuzzleHttp\Client The Guzzle client.
17
     */
18
    protected $guzzleClient;
19
20
    /**
21
     * @param \GuzzleHttp\Client|null The Guzzle client.
0 ignored issues
show
Bug introduced by
The type Maztech\HttpClients\The was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
22
     */
23
    public function __construct(Client $guzzleClient = null)
24
    {
25
        $this->guzzleClient = $guzzleClient ?: new Client();
26
    }
27
28
    /**
29
     * @inheritdoc
30
     */
31
    public function send($url, $method, $body, array $headers, $timeOut)
32
    {
33
        $options = [
34
            'headers' => $headers,
35
            'body' => $body,
36
            'timeout' => $timeOut,
37
            'connect_timeout' => 10,
38
            'verify' => __DIR__ . '/certs/DigiCertHighAssuranceEVRootCA.pem',
39
        ];
40
        $request = $this->guzzleClient->createRequest($method, $url, $options);
41
42
        try {
43
            $rawResponse = $this->guzzleClient->send($request);
44
        } catch (RequestException $e) {
45
            $rawResponse = $e->getResponse();
46
47
            if ($e->getPrevious() instanceof RingException || !$rawResponse instanceof ResponseInterface) {
48
                throw new InstagramSDKException($e->getMessage(), $e->getCode());
49
            }
50
        }
51
52
        $rawHeaders = $this->getHeadersAsString($rawResponse);
53
        $rawBody = $rawResponse->getBody();
54
        $httpStatusCode = $rawResponse->getStatusCode();
55
56
        return new GraphRawResponse($rawHeaders, $rawBody, $httpStatusCode);
0 ignored issues
show
Bug Best Practice introduced by
The expression return new Maztech\Http\...wBody, $httpStatusCode) returns the type Maztech\Http\GraphRawResponse which is incompatible with the return type mandated by Maztech\HttpClients\Inst...ClientInterface::send() of Instagram\Http\GraphRawResponse.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
57
    }
58
59
    /**
60
     * Returns the Guzzle array of headers as a string.
61
     *
62
     * @param ResponseInterface $response The Guzzle response.
63
     *
64
     * @return string
65
     */
66
    public function getHeadersAsString(ResponseInterface $response)
67
    {
68
        $headers = $response->getHeaders();
69
        $rawHeaders = [];
70
        foreach ($headers as $name => $values) {
71
            $rawHeaders[] = $name . ": " . implode(", ", $values);
72
        }
73
74
        return implode("\r\n", $rawHeaders);
75
    }
76
}
77