BuzzClient::execute()   A
last analyzed

Complexity

Conditions 5
Paths 12

Size

Total Lines 37

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 27
CRAP Score 5.0011

Importance

Changes 0
Metric Value
dl 0
loc 37
ccs 27
cts 28
cp 0.9643
rs 9.0168
c 0
b 0
f 0
cc 5
nc 12
nop 1
crap 5.0011
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 20
    private static function convertResponse(Request $request, BuzzResponse $buzzResponse)
66
    {
67 20
        $statusCode = $buzzResponse->getStatusCode();
68 20
        $rawBody = (string) $buzzResponse->getContent();
69
70 20
        $rawHeaders = $buzzResponse->getHeaders();
71 20
        $headers = array();
72 20
        foreach ($rawHeaders as $header) {
73 20
            if (stristr($header, 'HTTP/1.')) {
74 20
                continue;
75
            }
76
77 20
            $parts = explode(': ', $header);
78
79 20
            if (count($parts) !== 2) {
80
                $headers[$parts[0]] = '';
81
                continue;
82
            }
83
84 20
            list ($key, $value) = $parts;
85 20
            $headers[$key] = $value;
86 10
        }
87
88 20
        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 20
    public function execute(Request $request)
99
    {
100 20
        $method = $request->getMethod();
101 20
        $endpoint = $request->getEndpoint();
102 20
        $params = $request->getParams();
103 20
        $headers = $request->getHeaders();
104
105
        try {
106 20
            if ($method === 'GET') {
107 10
                $buzzResponse = $this->client->call(
108 10
                    $endpoint.'?'.http_build_query($params),
109 10
                    $method,
110 10
                    $headers,
111 10
                    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 5
                );
113 5
            } else {
114 10
                $buzzRequest = new FormRequest();
115 10
                $buzzRequest->fromUrl($endpoint);
116 10
                $buzzRequest->setMethod($method);
117 10
                $buzzRequest->setHeaders($headers);
118 10
                foreach ($params as $key => $value) {
119 10
                    if ($value instanceof Image) {
120 2
                        $value = new FormUpload($value->getData());
121 1
                    }
122
123 10
                    $buzzRequest->setField($key, $value);
124 5
                }
125
126 10
                $buzzResponse = new BuzzResponse();
127 15
                $this->client->send($buzzRequest, $buzzResponse);
128
            }
129 10
        } catch (RequestException $e) {
130
            throw new Exception($e->getMessage());
131
        }
132
133 20
        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