GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( e0a7f1...ec95b5 )
by Florian
05:01
created

CommunicationAdapter::decodeUrlEncodedResponse()   D

Complexity

Conditions 9
Paths 16

Size

Total Lines 41
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 6
Bugs 1 Features 3
Metric Value
c 6
b 1
f 3
dl 0
loc 41
rs 4.9091
cc 9
eloc 23
nc 16
nop 4
1
<?php
2
namespace Checkdomain\Comodo;
3
4
use Checkdomain\Comodo\Model\Account;
5
use GuzzleHttp\Client;
6
7
/**
8
 * Class CommunicationAdapter
9
 *
10
 * Manages the communication with comodo
11
 */
12
class CommunicationAdapter
13
{
14
    const RESPONSE_NEW_LINE    = 0;
15
    const RESPONSE_URL_ENCODED = 1;
16
17
    /**
18
     * @var Client
19
     */
20
    protected $client;
21
22
    /**
23
     * @var Account
24
     */
25
    protected $account;
26
27
    /**
28
     * @param \Checkdomain\Comodo\Model\Account $account
29
     *
30
     * @return CommunicationAdapter
31
     */
32
    public function setAccount($account)
33
    {
34
        $this->account = $account;
35
36
        return $this;
37
    }
38
39
    /**
40
     * @return \Checkdomain\Comodo\Model\Account
41
     */
42
    public function getAccount()
43
    {
44
        return $this->account;
45
    }
46
47
    /**
48
     * Constructs a communication adapter with an account
49
     *
50
     * @param Account $account
51
     */
52
    public function __construct(Account $account = null)
53
    {
54
        $this->account = $account;
55
    }
56
57
    /**
58
     * @param \GuzzleHttp\Client $client
59
     *
60
     * @return CommunicationAdapter
61
     */
62
    public function setClient($client)
63
    {
64
        $this->client = $client;
65
66
        return $this;
67
    }
68
69
    /**
70
     * @return \GuzzleHttp\Client
71
     */
72
    public function getClient()
73
    {
74
        if ($this->client === null) {
75
            $this->client = new Client();
76
        }
77
78
        return $this->client;
79
    }
80
81
    /**
82
     * Sends a query to the provided url and return the response body.
83
     *
84
     * @param string $url
85
     * @param array  $params
86
     *
87
     * @return string
88
     */
89
    public function sendToWebsite($url, array $params)
90
    {
91
        // Sending request
92
        $client  = $this->getClient();
93
        $response = $client->request('POST', $url, [
94
            'query' => http_build_query($params, '', '&')
95
        ]);
96
97
        // Getting response body
98
        $responseString = $response->getBody()->getContents();
99
        $responseString = trim($responseString);
100
101
        return $responseString;
102
    }
103
104
    /**
105
     * Send a request to the comodo API, and decodes the response as given
106
     *
107
     * @param string     $url
108
     * @param array      $params
109
     * @param int        $responseType
110
     * @param array|null $notDecode
111
     * @param array      $forceArray
112
     *
113
     * @return array|bool
114
     */
115
    public function sendToApi($url, array $params, $responseType = self::RESPONSE_NEW_LINE, array $notDecode = null, $forceArray = array())
116
    {
117
        $this->preSendToApiCheck();
118
119
        // Merging post-data
120
        $fields                  = array();
121
        $fields["loginName"]     = $this->getAccount()->getLoginName();
122
        $fields["loginPassword"] = $this->getAccount()->getLoginPassword();
123
        $fields                  = array_merge($fields, $params);
124
125
        // Sending request
126
        $client   = $this->getClient();
127
        $response = $client->request('POST', $url, [
128
            'form_params' => $fields
129
        ]);
130
        $query    = http_build_query($params);
131
132
        // Getting response body
133
        $responseString = $response->getBody()->getContents();
134
        $responseString = trim($responseString);
135
136
        // Decoding and returning response
137
        if ($responseType == self::RESPONSE_NEW_LINE) {
138
            return $this->decodeNewLineEncodedResponse($responseString, $query, $forceArray);
139
        } else {
140
            return $this->decodeUrlEncodedResponse($responseString, $query, $notDecode, $forceArray);
141
        }
142
    }
143
144
    /**
145
     * Checks, if a valid account has been provided
146
     *
147
     * @return bool
148
     * @throws \Exception
149
     */
150
    protected function preSendToApiCheck()
151
    {
152
        if ($this->getAccount() === null) {
153
            throw new \Exception("Please provided an account");
154
        }
155
156
        if (!$this->getAccount()->isValid()) {
157
            throw new \Exception("Please provided valid account");
158
        }
159
160
        return true;
161
    }
162
163
    /**
164
     * Decodes a responseString, separated by new lines and returns an response array
165
     *
166
     * @param string $responseString
167
     * @param string $requestQuery
168
     * @param array  $forceArray
169
     *
170
     * @return array
171
     */
172
    protected function decodeNewLineEncodedResponse($responseString, $requestQuery, array $forceArray = array())
173
    {
174
        // Splitting response body
175
        $parts = explode("\n", $responseString);
176
177
        // Getting the status
178
        $status = trim($parts[0]);
179
180
        $responseArray = array();
181
182
183
        // Valid answer?
184
        if (is_numeric($status)) {
185
            // Successful?
186
            if ($status != "0") {
187
                $responseArray["errorCode"]    = $status;
188
                $responseArray["errorMessage"] = trim($parts[1]);
189
            } else {
190
                $responseArray["errorCode"]    = $status;
191
192
                $partCount = count($parts);
193
                for ($i = 1; $i < $partCount; $i++) {
194
                    $tmp = preg_split('/[\s\t]+/', $parts[$i], 2);
195
196
                    $key   = trim($tmp[0]);
197
                    $value = trim($tmp[1]);
198
199
                    // if key already exists, open new array dimension
200
                    if (isset($responseArray[$key])) {
201
                        if (!is_array($responseArray[$key])) {
202
                            $tmpValue              = $responseArray[$key];
203
                            $responseArray[$key]   = array();
204
                            $responseArray[$key][] = $tmpValue;
205
                            $responseArray[$key][] = $value;
206
                        } else {
207
                            $responseArray[$key][] = $value;
208
                        }
209
                    } else {
210
                        // Just save
211
                        $responseArray[$key] = $value;
212
                    }
213
                }
214
            }
215
        } else {
216
            $responseArray["errorCode"]    = "";
217
            $responseArray["errorMessage"] = trim($responseString);
218
        }
219
220
        foreach ($forceArray as $value) {
221
            if (isset($responseArray[$value]) && !is_array($responseArray[$value])) {
222
                $responseArray[$value] = array($responseArray[$value]);
223
            }
224
        }
225
226
        $responseArray["responseString"] = $responseString;
227
        $responseArray["requestQuery"]   = $requestQuery;
228
229
        return $responseArray;
230
    }
231
232
    /**
233
     *  Decodes a responseString, encoded in query-string-format and returns an response array
234
     *
235
     * @param string   $responseString
236
     * @param string   $requestQuery
237
     * @param array    $notDecode
238
     * @param string[] $forceArray
239
     *
240
     * @return mixed
241
     */
242
    protected function decodeUrlEncodedResponse(
243
        $responseString,
244
        $requestQuery,
245
        array $notDecode = null,
246
        $forceArray = array()
247
    )
248
    {
249
        if (empty($notDecode)) {
250
            $responseString = urldecode($responseString);
251
        }
252
253
        if (!empty($forceArray)) {
254
            foreach ($forceArray as $param) {
255
                $responseString = str_replace($param . '=', $param .'[]=', $responseString);
256
            }
257
        }
258
259
        // Splitting response body
260
        $responseArray = [];
261
        parse_str($responseString, $responseArray);
262
263
        if (!empty($notDecode) && is_array($responseArray)) {
264
            foreach ($responseArray as $index => $value) {
265
                if (!in_array($index, $notDecode)) {
266
                    $value = urldecode($value);
267
                }
268
269
                $responseArray[$index] = $value;
270
            }
271
        }
272
273
        $responseArray["responseString"] = $responseString;
274
        $responseArray["requestQuery"]   = $requestQuery;
275
276
        if (!isset($responseArray['errorCode'])) {
277
            $responseArray['errorCode'] = 99;
278
            $responseArray['errorMessage'] = $responseString;
279
        }
280
281
        return $responseArray;
282
    }
283
}
284