InstagramStreamHttpClient::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Maztech\HttpClients;
4
5
use Maztech\Http\GraphRawResponse;
6
use Maztech\Exceptions\InstagramSDKException;
7
8
class InstagramStreamHttpClient implements InstagramHttpClientInterface
9
{
10
    /**
11
     * @var InstagramStream Procedural stream wrapper as object.
12
     */
13
    protected $InstagramStream;
14
15
    /**
16
     * @param InstagramStream|null Procedural stream wrapper as object.
0 ignored issues
show
Bug introduced by
The type Maztech\HttpClients\Procedural 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...
17
     */
18
    public function __construct(InstagramStream $InstagramStream = null)
19
    {
20
        $this->InstagramStream = $InstagramStream ?: new InstagramStream();
21
    }
22
23
    /**
24
     * @inheritdoc
25
     */
26
    public function send($url, $method, $body, array $headers, $timeOut)
27
    {
28
        $options = [
29
            'http' => [
30
                'method' => $method,
31
                'header' => $this->compileHeader($headers),
32
                'content' => $body,
33
                'timeout' => $timeOut,
34
                'ignore_errors' => true
35
            ],
36
            'ssl' => [
37
                'verify_peer' => true,
38
                'verify_peer_name' => true,
39
                'allow_self_signed' => true, // All root certificates are self-signed
40
                'cafile' => __DIR__ . '/certs/DigiCertHighAssuranceEVRootCA.pem',
41
            ],
42
        ];
43
44
        $this->InstagramStream->streamContextCreate($options);
45
        $rawBody = $this->InstagramStream->fileGetContents($url);
46
        $rawHeaders = $this->InstagramStream->getResponseHeaders();
47
48
        if ($rawBody === false || empty($rawHeaders)) {
49
            throw new InstagramSDKException('Stream returned an empty response', 660);
50
        }
51
52
        $rawHeaders = implode("\r\n", $rawHeaders);
53
54
        return new GraphRawResponse($rawHeaders, $rawBody);
0 ignored issues
show
Bug Best Practice introduced by
The expression return new Maztech\Http\...($rawHeaders, $rawBody) 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...
55
    }
56
57
    /**
58
     * Formats the headers for use in the stream wrapper.
59
     *
60
     * @param array $headers The request headers.
61
     *
62
     * @return string
63
     */
64
    public function compileHeader(array $headers)
65
    {
66
        $header = [];
67
        foreach ($headers as $k => $v) {
68
            $header[] = $k . ': ' . $v;
69
        }
70
71
        return implode("\r\n", $header);
72
    }
73
}
74