HttpClient::put()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 1
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 31 and the first side effect is on line 19.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
/*
4
 * This file is part of the XabbuhPandaClient package.
5
 *
6
 * (c) Christian Flothmann <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Xabbuh\PandaClient\Api;
13
14
use Guzzle\Http\Client;
15
use Xabbuh\PandaClient\Exception\ApiException;
16
use Xabbuh\PandaClient\Exception\HttpException;
17
use Xabbuh\PandaClient\Signer\PandaSigner;
18
19 1
@trigger_error('Xabbuh\PandaClient\Api\HttpClient is deprecated since 1.3.0 and will be removed in 2.0. Use Xabbuh\PandaClient\Api\HttplugClient instead.', E_USER_DEPRECATED);
20
21
/**
22
 * Panda REST client implementation using the PHP cURL extension.
23
 *
24
 * Send signed requests (GET, POST, PUT or DELETE) to the Panda encoding
25
 * webservice.
26
 *
27
 * @author Christian Flothmann <[email protected]>
28
 *
29
 * @deprecated since 1.3.0, will be removed in 2.0. Use HttplugClient instead.
30
 */
31
class HttpClient implements HttpClientInterface
32
{
33
    /**
34
     * @var Client
35
     */
36
    private $guzzleClient;
37
38
    /**
39
     * Panda cloud id
40
     *
41
     * @var string
42
     */
43
    private $cloudId;
44
45
    /**
46
     * Panda Account
47
     *
48
     * @var Account
49
     */
50
    private $account;
51
52
    /**
53
     * {@inheritDoc}
54
     */
55 5
    public function setCloudId($cloudId)
56
    {
57 5
        $this->cloudId = $cloudId;
58 5
    }
59
60
    /**
61
     * {@inheritDoc}
62
     */
63
    public function getCloudId()
64
    {
65
        return $this->cloudId;
66
    }
67
68
    /**
69
     * {@inheritDoc}
70
     */
71 5
    public function setAccount(Account $account)
72
    {
73 5
        $this->account = $account;
74 5
    }
75
76
    /**
77
     * {@inheritDoc}
78
     */
79
    public function getAccount()
80
    {
81
        return $this->account;
82
    }
83
84 5
    public function setGuzzleClient(Client $guzzleClient)
85
    {
86 5
        $this->guzzleClient = $guzzleClient;
87 5
    }
88
89
    public function getGuzzleClient()
90
    {
91
        return $this->guzzleClient;
92
    }
93
94
    /**
95
     * {@inheritDoc}
96
     */
97 2
    public function get($path, array $params = array())
98
    {
99 2
        return $this->request('GET', $path, $params);
100
    }
101
102
    /**
103
     * {@inheritDoc}
104
     */
105 1
    public function post($path, array $params = array())
106
    {
107 1
        return $this->request('POST', $path, $params);
108
    }
109
110
    /**
111
     * {@inheritDoc}
112
     */
113 1
    public function put($path, array $params = array())
114
    {
115 1
        return $this->request('PUT', $path, $params);
116
    }
117
118
    /**
119
     * {@inheritDoc}
120
     */
121 1
    public function delete($path, array $params = array())
122
    {
123 1
        return $this->request('DELETE', $path, $params);
124
    }
125
126
    /**
127
     * Send signed HTTP requests to the API server.
128
     *
129
     * @param string $method HTTP method (GET, POST, PUT or DELETE)
130
     * @param string $path   Request path
131
     * @param array  $params Additional request parameters
132
     *
133
     * @return string The API server's response
134
     *
135
     * @throws ApiException  if an API error occurs
136
     * @throws HttpException if the request fails
137
     */
138 5
    private function request($method, $path, array $params)
139
    {
140
        // sign the request parameters
141 5
        $signer = PandaSigner::getInstance($this->cloudId, $this->account);
142 5
        $params = $signer->signParams($method, $path, $params);
143
144
        // ensure to use relative paths
145 5
        if (0 === strpos($path, '/')) {
146 5
            $path = substr($path, 1);
147
        }
148
149
        // append request parameters to the URL
150 5
        if ('GET' === $method || 'DELETE' === $method) {
151 3
            $path .= '?'.http_build_query($params, '', '&');
152
        }
153
154
        // prepare the request
155 5
        $request = null;
156
157
        switch ($method) {
158 5
            case 'GET':
159 2
                $request = $this->guzzleClient->get($path);
160 2
                break;
161 3
            case 'DELETE':
162 1
                $request = $this->guzzleClient->delete($path);
163 1
                break;
164 2
            case 'PUT':
165 1
                $request = $this->guzzleClient->put($path, null, $params);
0 ignored issues
show
Documentation introduced by
$params is of type array<integer,string>, but the function expects a string|resource|object<G...tityBodyInterface>|null.

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...
166 1
                break;
167 1
            case 'POST':
168 1
                $request = $this->guzzleClient->post($path, null, $params);
169 1
                break;
170
        }
171
172
        // and execute it
173
        try {
174 5
            $response = $request->send();
175
        } catch (\Exception $e) {
176
            // throw an exception if the http request failed
177
            throw new HttpException($e->getMessage(), $e->getCode());
178
        }
179
180
        // throw an API exception if the API response is not valid
181 5
        if ($response->getStatusCode() < 200 || $response->getStatusCode() > 207) {
182 1
            $decodedResponse = json_decode($response->getBody(true));
183 1
            $message = $decodedResponse->error.': '.$decodedResponse->message;
184
185 1
            throw new ApiException($message, $response->getStatusCode());
186
        }
187
188 4
        return $response->getBody(true);
189
    }
190
}
191