Completed
Pull Request — master (#19)
by
unknown
05:55
created

BuzzClient   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 9.09%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 9
c 2
b 0
f 0
lcom 1
cbo 8
dl 0
loc 91
ccs 4
cts 44
cp 0.0909
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A convertResponse() 0 19 3
B execute() 0 37 5
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 Buzz\Browser
38
     */
39
    private $client;
40
41
    /**
42
     * Creates a new buzz client.
43
     */
44 42
    public function __construct()
45
    {
46 42
        $curl = new Curl();
47 42
        $this->client = new Browser($curl);
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Buzz\Browser($curl) of type object<Buzz\Browser> is incompatible with the declared type object<Pinterest\Http\Buzz\Browser> of property $client.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
48 42
    }
49
50
    /**
51
     * Converts a buzz response to a pinterest response.
52
     *
53
     * @param Request      $request      The request.
54
     * @param BuzzResponse $buzzResponse The buzz response.
55
     *
56
     * @return Response The response.
57
     */
58
    private static function convertResponse(Request $request, BuzzResponse $buzzResponse)
59
    {
60
        $statusCode = $buzzResponse->getStatusCode();
61
        $rawBody = (string) $buzzResponse->getContent();
62
63
        $rawHeaders = $buzzResponse->getHeaders();
64
        $headers = array();
65
        foreach ($rawHeaders as $header) {
66
            if (stristr($header, 'HTTP/1.')) {
67
                continue;
68
            }
69
70
            list($key, $value) = explode(': ', $header);
71
72
            $headers[$key] = $value;
73
        }
74
75
        return new Response($request, $statusCode, $rawBody, $headers);
76
    }
77
78
    /**
79
     * Executes a http request.
80
     *
81
     * @param Request $request The http request.
82
     *
83
     * @return Response The http response.
84
     */
85
    public function execute(Request $request)
86
    {
87
        $method = $request->getMethod();
88
        $endpoint = $request->getEndpoint();
89
        $params = $request->getParams();
90
        $headers = $request->getHeaders();
91
92
        try {
93
            if ($method === 'GET') {
94
                $buzzResponse = $this->client->call(
95
                    $endpoint.'?'.http_build_query($params),
96
                    $method,
97
                    $headers,
98
                    array()
99
                );
100
            } else {
101
                $buzzRequest = new FormRequest();
102
                $buzzRequest->fromUrl($endpoint);
103
                $buzzRequest->setMethod($method);
104
                $buzzRequest->setHeaders($headers);
105
                foreach ($params as $key => $value) {
106
                    if ($value instanceof Image) {
107
                        $value = new FormUpload($value->getData());
108
                    }
109
110
                    $buzzRequest->setField($key, $value);
111
                }
112
113
                $buzzResponse = new BuzzResponse();
114
                $this->client->send($buzzRequest, $buzzResponse);
115
            }
116
        } catch (RequestException $e) {
117
            throw new Exception($e->getMessage());
118
        }
119
120
        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...
121
    }
122
}
123