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 (#46)
by Andy
04:01
created

HttpGuzzlClient::addAuthOption()   B

Complexity

Conditions 5
Paths 2

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 5

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 15
ccs 13
cts 13
cp 1
rs 8.8571
cc 5
eloc 9
nc 2
nop 1
crap 5
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\Exception\HttpConnectionException;
13
use GuzzleHttp\ClientInterface;
14
15
class HttpGuzzlClient implements HttpClientInterface
16
{
17
    const DEFAULT_CONNECTION_TIMEOUT = 5;
18
    const DEFAULT_TIMEOUT = 30;
19
20
    /**
21
     * @var ClientInterface
22
     */
23
    private $oGuzzelClient;
24
25
    /**
26
     * @var array
27
     */
28
    private $aGuzzelClientConfig;
29
30
    /**
31
     * @param ClientInterface $oGuzzelClient
32
     * @param array $aGuzzelClientConfig
33
     */
34 7
    public function __construct(
35
        ClientInterface $oGuzzelClient,
36
        array $aGuzzelClientConfig = []
37
    )
38
    {
39 7
        $this->oGuzzelClient = $oGuzzelClient;
40 7
        $this->aGuzzelClientConfig = $aGuzzelClientConfig;
41 7
    }
42
43
    /**
44
     * @param string $sUrl
45
     * @return HttpClientResponseInterface
46
     * @throws HttpConnectionException
47
     */
48 3
    public function get($sUrl)
49
    {
50
        $_aRequestOptions = [
51 3
            'connect_timeout' => self::DEFAULT_CONNECTION_TIMEOUT,
52
            'timeout' => self::DEFAULT_TIMEOUT
53 3
        ];
54 3
        $_aRequestOptions = $this->addAuthOption($_aRequestOptions);
55
56
        try
57
        {
58 3
            $_oResponse = $this->oGuzzelClient->get($sUrl, $_aRequestOptions);
59 2
            return new HttpGuzzlResponse($_oResponse);
60
        }
61 1
        catch (\Exception $oException)
62
        {
63 1
            throw new HttpConnectionException(
64 1
                sprintf('Can\'t get response from "%s"', $this->oGuzzelClient->getBaseUrl() . $sUrl),
65 1
                0,
66
                $oException
67 1
            );
68
        }
69
    }
70
71
    /**
72
     * @param string $sUrl
73
     * @param mixed $mPostData
74
     * @return HttpGuzzlResponse
75
     */
76 2
    public function postJsonData($sUrl, $mPostData)
77
    {
78 2
        $_aRequestOptions = ['json' => $mPostData];
79 2
        $_aRequestOptions = $this->addAuthOption($_aRequestOptions);
80
81 2
        $_oRequest = $this->oGuzzelClient->createRequest('post', $sUrl, $_aRequestOptions);
82 2
        $_oResponse = $this->oGuzzelClient->send($_oRequest);
83
84 2
        return new HttpGuzzlResponse($_oResponse);
85
    }
86
87
    /**
88
     * @param string $sUrl
89
     * @return HttpGuzzlResponse
90
     */
91 2
    public function delete($sUrl)
92
    {
93 2
        $_aRequestOptions = [];
94 2
        $_aRequestOptions = $this->addAuthOption($_aRequestOptions);
95
96 2
        $_oResponse = $this->oGuzzelClient->delete($sUrl, $_aRequestOptions);
97 2
        return new HttpGuzzlResponse($_oResponse);
98
    }
99
100
    /**
101
     * Adds authentication headers according the Guzzle http options.
102
     *
103
     * @param array $aOptions
104
     * @return array
105
     */
106 7
    private function addAuthOption(array $aOptions) {
107 7
        if (array_key_exists('username', $this->aGuzzelClientConfig)
108 7
            && $this->aGuzzelClientConfig['username']
109 7
            && array_key_exists('password', $this->aGuzzelClientConfig)
110 7
            && $this->aGuzzelClientConfig['password']
111 3
        )
112 7
        {
113 3
            $aOptions['auth'] = [
114 3
                $this->aGuzzelClientConfig['username'],
115 3
                $this->aGuzzelClientConfig['password']
116 3
            ];
117 3
        }
118
119 7
        return $aOptions;
120
    }
121
}