Completed
Push — master ( 777296...5c380a )
by Hans
04:32 queued 04:30
created

BuzzClient::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2.0932

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 5
cts 7
cp 0.7143
rs 9.9332
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2.0932
1
<?php
2
3
/*
4
 * This file is part of the Pinterest PHP library.
5
 *
6
 * (c) Hans Ott <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.md.
10
 *
11
 * Source: https://github.com/hansott/pinterest-php
12
 */
13
14
namespace Pinterest\Http;
15
16
use Exception;
17
use Buzz\Browser;
18
use Pinterest\Image;
19
use Buzz\Client\Curl;
20
use Buzz\Message\Form\FormUpload;
21
use Buzz\Message\Form\FormRequest;
22
use Buzz\Exception\RequestException;
23
use Buzz\Message\Response as BuzzResponse;
24
25
/**
26
 * The implemented http client class (uses Buzz).
27
 *
28
 * @link https://github.com/kriswallsmith/Buzz
29
 *
30
 * @author Toon Daelman <[email protected]>
31
 */
32
class BuzzClient implements ClientInterface
33
{
34
    /**
35
     * Buzz browser.
36
     *
37
     * @var Browser
38
     */
39
    private $client;
40
41
    /**
42
     * Creates a new buzz client.
43
     *
44
     * @param \Buzz\Client\ClientInterface|null $client
45
     */
46 60
    public function __construct(\Buzz\Client\ClientInterface $client = null)
47
    {
48 60
        if ($client instanceof ClientInterface) {
49
            $this->client = $client;
50
        } else {
51
            // Backwards compatible
52 60
            $curl = new Curl();
53 60
            $this->client = new Browser($curl);
54
        }
55 60
    }
56
57
    /**
58
     * Converts a buzz response to a pinterest response.
59
     *
60
     * @param Request      $request      The request.
61
     * @param BuzzResponse $buzzResponse The buzz response.
62
     *
63
     * @return Response The response.
64
     */
65 10
    private static function convertResponse(Request $request, BuzzResponse $buzzResponse)
66
    {
67 10
        $statusCode = $buzzResponse->getStatusCode();
68 10
        $rawBody = (string) $buzzResponse->getContent();
69
70 10
        $rawHeaders = $buzzResponse->getHeaders();
71 10
        $headers = array();
72 10
        foreach ($rawHeaders as $header) {
73 10
            if (stristr($header, 'HTTP/1.')) {
74 10
                continue;
75
            }
76
77 10
            $parts = explode(': ', $header);
78
79 10
            if (count($parts) !== 2) {
80
                $headers[$parts[0]] = '';
81
                continue;
82
            }
83
84 10
            list ($key, $value) = $parts;
85 10
            $headers[$key] = $value;
86 5
        }
87
88 10
        return new Response($request, $statusCode, $rawBody, $headers);
89
    }
90
91
    /**
92
     * Executes a http request.
93
     *
94
     * @param Request $request The http request.
95
     *
96
     * @return Response The http response.
97
     */
98 10
    public function execute(Request $request)
99
    {
100 10
        $method = $request->getMethod();
101 10
        $endpoint = $request->getEndpoint();
102 10
        $params = $request->getParams();
103 10
        $headers = $request->getHeaders();
104
105
        try {
106 10
            if ($method === 'GET') {
107 8
                $buzzResponse = $this->client->call(
108 8
                    $endpoint.'?'.http_build_query($params),
109 8
                    $method,
110 8
                    $headers,
111 8
                    array()
0 ignored issues
show
Documentation introduced by
array() is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
112 4
                );
113 4
            } else {
114 2
                $buzzRequest = new FormRequest();
115 2
                $buzzRequest->fromUrl($endpoint);
116 2
                $buzzRequest->setMethod($method);
117 2
                $buzzRequest->setHeaders($headers);
118 2
                foreach ($params as $key => $value) {
119 2
                    if ($value instanceof Image) {
120
                        $value = new FormUpload($value->getData());
121
                    }
122
123 2
                    $buzzRequest->setField($key, $value);
124 1
                }
125
126 2
                $buzzResponse = new BuzzResponse();
127 6
                $this->client->send($buzzRequest, $buzzResponse);
128
            }
129 5
        } catch (RequestException $e) {
130
            throw new Exception($e->getMessage());
131
        }
132
133 10
        return static::convertResponse($request, $buzzResponse);
0 ignored issues
show
Bug introduced by
Since convertResponse() is declared private, calling it with static will lead to errors in possible sub-classes. You can either use self, or increase the visibility of convertResponse() to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
}

public static function getSomeVariable()
{
    return static::getTemperature();
}

}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass {
      private static function getTemperature() {
        return "-182 °C";
    }
}

print YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
    }

    public static function getSomeVariable()
    {
        return self::getTemperature();
    }
}
Loading history...
Compatibility introduced by
$buzzResponse of type object<Buzz\Message\MessageInterface> is not a sub-type of object<Buzz\Message\Response>. It seems like you assume a concrete implementation of the interface Buzz\Message\MessageInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
134
    }
135
}
136