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
Pull Request — master (#68)
by Marc
08:42
created

HttpGuzzlClient::get()   B

Complexity

Conditions 7
Paths 13

Size

Total Lines 58
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 14.5328

Importance

Changes 0
Metric Value
dl 0
loc 58
ccs 13
cts 28
cp 0.4643
rs 7.6045
c 0
b 0
f 0
cc 7
eloc 35
nc 13
nop 1
crap 14.5328

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * @package: chapi
4
 *
5
 * @author:  msiebeneicher
6
 * @since:   2015-07-28
7
 *
8
 */
9
10
namespace Chapi\Component\Http;
11
12
use Chapi\Entity\Http\AuthEntity;
13
use Chapi\Exception\HttpConnectionException;
14
use GuzzleHttp\ClientInterface;
15
use GuzzleHttp\Exception\ClientException;
16
use GuzzleHttp\Exception\ConnectException;
17
use GuzzleHttp\Exception\RequestException;
18
use GuzzleHttp\Exception\ServerException;
19
use GuzzleHttp\Exception\TooManyRedirectsException;
20
21
class HttpGuzzlClient implements HttpClientInterface
22
{
23
    const DEFAULT_CONNECTION_TIMEOUT = 5;
24
    const DEFAULT_TIMEOUT = 30;
25
26
    /**
27
     * @var ClientInterface
28
     */
29
    private $oGuzzelClient;
30
31
    /**
32
     * @var AuthEntity
33
     */
34
    private $oAuthEntity;
35
36
    /**
37
     * @param ClientInterface $oGuzzelClient
38
     * @param AuthEntity $oAuthEntity
39
     */
40 7
    public function __construct(
41
        ClientInterface $oGuzzelClient,
42
        AuthEntity $oAuthEntity
43
    )
44
    {
45 7
        $this->oGuzzelClient = $oGuzzelClient;
46 7
        $this->oAuthEntity = $oAuthEntity;
47 7
    }
48
49
    /**
50
     * @param string $sUrl
51
     * @return HttpClientResponseInterface
52
     * @throws HttpConnectionException
53
     */
54 3
    public function get($sUrl)
55
    {
56 3
        $_aRequestOptions = $this->getDefaultRequestOptions();
57
58
        try
59
        {
60 3
            $_oResponse = $this->oGuzzelClient->request('GET', $sUrl, $_aRequestOptions);
61 2
            return new HttpGuzzlResponse($_oResponse);
62
        }
63 1
        catch (ClientException $oException) // 400 level errors
64
        {
65
            throw new HttpConnectionException(
66
                sprintf('Client error: Calling %s returned %d', $this->oGuzzelClient->getConfig('base_uri') . $sUrl, $oException->getCode()),
67
                $oException->getCode(),
68
                $oException
69
            );
70
        }
71 1
        catch (ServerException $oException) // 500 level errors
72
        {
73
            throw new HttpConnectionException(
74
                sprintf('Server error: Calling %s returned %d', $this->oGuzzelClient->getConfig('base_uri') . $sUrl, $oException->getCode()),
75
                $oException->getCode(),
76
                $oException
77
            );
78
        }
79 1
        catch (TooManyRedirectsException $oException) // too many redirects to follow
80
        {
81
            throw new HttpConnectionException(
82
                sprintf('Request to %s failed due to too many redirects', $this->oGuzzelClient->getConfig('base_uri') . $sUrl),
83
                HttpConnectionException::ERROR_CODE_TOO_MANY_REDIRECT_EXCEPTION,
84
                $oException
85
            );
86
        }
87 1
        catch (ConnectException $oException) // networking error
88
        {
89
            throw new HttpConnectionException(
90
                sprintf('Cannot connect to %s due to some networking error', $this->oGuzzelClient->getConfig('base_uri') . $sUrl),
91
                HttpConnectionException::ERROR_CODE_CONNECT_EXCEPTION,
92
                $oException
93
            );
94
        }
95 1
        catch (RequestException $oException) // networking error (connection timeout, DNS errors, etc.)
96
        {
97
            throw new HttpConnectionException(
98
                sprintf('Cannot connect to %s due to networking error', $this->oGuzzelClient->getConfig('base_uri') . $sUrl),
99
                HttpConnectionException::ERROR_CODE_REQUEST_EXCEPTION,
100
                $oException
101
            );
102
        }
103 1
        catch (\Exception $oException)
104
        {
105 1
            throw new HttpConnectionException(
106 1
                sprintf('Can\'t get response from "%s"', $this->oGuzzelClient->getConfig('base_uri') . $sUrl),
107 1
                HttpConnectionException::ERROR_CODE_UNKNOWN,
108
                $oException
109
            );
110
        }
111
    }
112
113
    /**
114
     * @param string $sUrl
115
     * @param mixed $mPostData
116
     * @return HttpGuzzlResponse
117
     */
118 2
    public function postJsonData($sUrl, $mPostData)
119
    {
120 2
        return $this->sendJsonDataWithMethod('POST', $sUrl, $mPostData);
121
    }
122
123
    /**
124
     * @param string $sUrl
125
     * @param mixed $mPutData
126
     * @return HttpGuzzlResponse
127
     */
128
    public function putJsonData($sUrl, $mPutData)
129
    {
130
        return $this->sendJsonDataWithMethod('PUT', $sUrl, $mPutData);
131
    }
132
133
134
    /**
135
     * @param string $sUrl
136
     * @return HttpGuzzlResponse
137
     */
138 2
    public function delete($sUrl)
139
    {
140 2
        $_aRequestOptions = $this->getDefaultRequestOptions();
141 2
        $_oResponse = $this->oGuzzelClient->request('DELETE', $sUrl, $_aRequestOptions);
142 2
        return new HttpGuzzlResponse($_oResponse);
143
    }
144
145
    /**
146
     * @param $sMethod
147
     * @param $sUrl
148
     * @param $mData
149
     * @return HttpGuzzlResponse
150
     */
151 2
    private function sendJsonDataWithMethod($sMethod, $sUrl, $mData)
152
    {
153 2
        $_aRequestOptions = $this->getDefaultRequestOptions();
154 2
        $_aRequestOptions['json'] = $mData;
155
156 2
        $_oResponse = $this->oGuzzelClient->request($sMethod, $sUrl, $_aRequestOptions);
157
158 2
        return new HttpGuzzlResponse($_oResponse);
159
    }
160
161
    /**
162
     * Returns default options for the HTTP request.
163
     * If an username and password is provided, auth
164
     * header will be applied as well.
165
     *
166
     * @return array<string,integer|string>
0 ignored issues
show
Documentation introduced by
Should the return type not be array<string,integer|string[]>?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
167
     */
168 7
    private function getDefaultRequestOptions()
169
    {
170
        $_aRequestOptions = [
171 7
            'connect_timeout' => self::DEFAULT_CONNECTION_TIMEOUT,
172 7
            'timeout' => self::DEFAULT_TIMEOUT
173
        ];
174
175 7
        if (!empty($this->oAuthEntity->username)
176 7
            && !empty($this->oAuthEntity->password)
177
        )
178
        {
179 3
            $_aRequestOptions['auth'] = [
180 3
                $this->oAuthEntity->username,
181 3
                $this->oAuthEntity->password
182
            ];
183
        }
184
185 7
        return $_aRequestOptions;
186
    }
187
188
}