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 Bidesh
02:54
created

HttpGuzzlClient::putJsonData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
crap 2
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
16
class HttpGuzzlClient implements HttpClientInterface
17
{
18
    const DEFAULT_CONNECTION_TIMEOUT = 5;
19
    const DEFAULT_TIMEOUT = 30;
20
21
    /**
22
     * @var ClientInterface
23
     */
24
    private $oGuzzelClient;
25
26
    /**
27
     * @var AuthEntity
28
     */
29
    private $oAuthEntity;
30
31
    /**
32
     * @param ClientInterface $oGuzzelClient
33
     * @param AuthEntity $oAuthEntity
34
     */
35 7
    public function __construct(
36
        ClientInterface $oGuzzelClient,
37
        AuthEntity $oAuthEntity
38
    )
39
    {
40 7
        $this->oGuzzelClient = $oGuzzelClient;
41 7
        $this->oAuthEntity = $oAuthEntity;
42 7
    }
43
44
    /**
45
     * @param string $sUrl
46
     * @return HttpClientResponseInterface
47
     * @throws HttpConnectionException
48
     */
49 3
    public function get($sUrl)
50
    {
51 3
        $_aRequestOptions = $this->getDefaultRequestOptions();
52
53
        try
54
        {
55 3
            $_oResponse = $this->oGuzzelClient->request('GET', $sUrl, $_aRequestOptions);
56 2
            return new HttpGuzzlResponse($_oResponse);
57
        }
58 1
        catch (\Exception $oException)
59
        {
60 1
            throw new HttpConnectionException(
61 1
                sprintf('Can\'t get response from "%s"', $this->oGuzzelClient->getConfig('base_uri') . $sUrl),
62 1
                0,
63
                $oException
64 1
            );
65
        }
66
    }
67
68
    /**
69
     * @param string $sUrl
70
     * @param mixed $mPostData
71
     * @return HttpGuzzlResponse
72
     */
73 2
    public function postJsonData($sUrl, $mPostData)
74
    {
75 2
        return $this->sendJsonDataWithMethod('POST', $sUrl, $mPostData);
76
    }
77
78
    /**
79
     * @param string $sUrl
80
     * @param mixed $mPutData
81
     * @return HttpGuzzlResponse
82
     */
83
    public function putJsonData($sUrl, $mPutData)
84
    {
85
        return $this->sendJsonDataWithMethod('PUT', $sUrl, $mPutData);
86
    }
87
88
89
    /**
90
     * @param string $sUrl
91
     * @return HttpGuzzlResponse
92
     */
93 2
    public function delete($sUrl)
94
    {
95 2
        $_aRequestOptions = $this->getDefaultRequestOptions();
96 2
        $_oResponse = $this->oGuzzelClient->request('DELETE', $sUrl, $_aRequestOptions);
97 2
        return new HttpGuzzlResponse($_oResponse);
98
    }
99
100
    /**
101
     * @param $sMethod
102
     * @param $sUrl
103
     * @param $mData
104
     * @return HttpGuzzlResponse
105
     */
106 2
    private function sendJsonDataWithMethod($sMethod, $sUrl, $mData) {
107 2
        $_aRequestOptions = $this->getDefaultRequestOptions();
108 2
        $_aRequestOptions['json'] = $mData;
109
110 2
        $_oResponse = $this->oGuzzelClient->request($sMethod, $sUrl, $_aRequestOptions);
111
112 2
        return new HttpGuzzlResponse($_oResponse);
113
    }
114
115
    /**
116
     * Returns default options for the HTTP request.
117
     * If an username and password is provided, auth
118
     * header will be applied as well.
119
     *
120
     * @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...
121
     */
122 7
    private function getDefaultRequestOptions() {
123
        $_aRequestOptions = [
124 7
            'connect_timeout' => self::DEFAULT_CONNECTION_TIMEOUT,
125
            'timeout' => self::DEFAULT_TIMEOUT
126 7
        ];
127
128 7
        if (!empty($this->oAuthEntity->username)
129 7
            && !empty($this->oAuthEntity->password)
130 3
        )
131 7
        {
132 3
            $_aRequestOptions['auth'] = [
133 3
                $this->oAuthEntity->username,
134 3
                $this->oAuthEntity->password
135 3
            ];
136 3
        }
137
138 7
        return $_aRequestOptions;
139
    }
140
}