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.
Test Setup Failed
Pull Request — master (#106)
by
unknown
04:27
created

HasHttpRequest::unwrapResponse()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 0
cts 0
cp 0
rs 9.2
c 0
b 0
f 0
cc 4
eloc 8
nc 3
nop 1
crap 20
1
<?php
2
3
/*
4
 * This file is part of the overtrue/easy-sms.
5
 *
6
 * (c) overtrue <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace Overtrue\EasySms\Traits;
13
14
use GuzzleHttp\Client;
15
use Psr\Http\Message\ResponseInterface;
16
17
/**
18
 * Trait HasHttpRequest.
19
 */
20
trait HasHttpRequest
21
{
22
    /**
23
     * Make a get request.
24
     *
25
     * @param string $endpoint
26
     * @param array  $query
27
     * @param array  $headers
28
     *
29
     * @return array
30
     */
31 1
    protected function get($endpoint, $query = [], $headers = [])
32
    {
33 1
        return $this->request('get', $endpoint, [
34 1
            'headers' => $headers,
35 1
            'query' => $query,
36 1
        ]);
37
    }
38
39
    /**
40
     * Make a post request.
41
     *
42
     * @param string $endpoint
43
     * @param array  $params
44
     * @param array  $headers
45
     *
46
     * @return array
47
     */
48 1
    protected function post($endpoint, $params = [], $headers = [])
49
    {
50 1
        return $this->request('post', $endpoint, [
51 1
            'headers' => $headers,
52 1
            'form_params' => $params,
53 1
        ]);
54
    }
55
    
56
    /**
57
     * Make a postJson request.
58
     *
59
     * @param string $endpoint
60
     * @param array  $params
61
     * @param array  $headers
62
     *
63
     * @return array
64
     */
65 1
    protected function postJson($endpoint, $params = [], $headers = [])
66
    {
67 1
        return $this->request('post', $endpoint, [
68
            'headers' => $headers,
69
            'json' => $params,
70
        ]);
71
    }
72
    
73
    /**
74
     * Make a http request.
75 1
     *
76
     * @param string $method
77
     * @param string $endpoint
78 1
     * @param array  $options  http://docs.guzzlephp.org/en/latest/request-options.html
79 1
     *
80 1
     * @return array
81
     */
82 1
    protected function request($method, $endpoint, $options = [])
83
    {
84
        return $this->unwrapResponse($this->getHttpClient($this->getBaseOptions())->{$method}($endpoint, $options));
85
    }
86
87
    /**
88
     * Return base Guzzle options.
89
     *
90
     * @return array
91
     */
92
    protected function getBaseOptions()
93
    {
94
        $options = [
95
            'base_uri' => method_exists($this, 'getBaseUri') ? $this->getBaseUri() : '',
96
            'timeout' => property_exists($this, 'timeout') ? $this->timeout : 5.0,
97
        ];
98
99
        return $options;
100
    }
101
102
    /**
103
     * Return http client.
104
     *
105
     * @param array $options
106 3
     *
107
     * @return \GuzzleHttp\Client
108 3
     *
109 3
     * @codeCoverageIgnore
110
     */
111 3
    protected function getHttpClient(array $options = [])
112 1
    {
113 2
        return new Client($options);
114 1
    }
115
116
    /**
117 1
     * Convert response contents to json.
118
     *
119
     * @param \Psr\Http\Message\ResponseInterface $response
120
     *
121
     * @return array
122
     */
123
    protected function unwrapResponse(ResponseInterface $response)
124
    {
125
        $contentType = $response->getHeaderLine('Content-Type');
126
        $contents = $response->getBody()->getContents();
127
128
        if (false !== stripos($contentType, 'json') || stripos($contentType, 'javascript')) {
129
            return json_decode($contents, true);
130
        } elseif (false !== stripos($contentType, 'xml')) {
131
            return json_decode(json_encode(simplexml_load_string($contents)), true);
132
        }
133
134
        return $contents;
135
    }
136
}
137