Completed
Push — master ( 9cbcb0...b6322f )
by Xu
09:02 queued 02:42
created

HasHttpRequest::request()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 3
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @link http://www.tintsoft.com/
4
 * @copyright Copyright (c) 2012 TintSoft Technology Co. Ltd.
5
 * @license http://www.tintsoft.com/license/
6
 */
7
8
namespace yuncms\payment\traits;
9
10
use GuzzleHttp\Client;
11
use Psr\Http\Message\ResponseInterface;
12
13
/**
14
 * Trait HasHttpRequest
15
 */
16
trait HasHttpRequest
17
{
18
    /**
19
     * Make a get request.
20
     *
21
     * @param string $endpoint
22
     * @param array $query
23
     * @param array $headers
24
     *
25
     * @return array
26
     */
27
    protected function get($endpoint, $query = [], $headers = [])
28
    {
29
        return $this->request('get', $endpoint, [
30
            'headers' => $headers,
31
            'query' => $query,
32
        ]);
33
    }
34
35
    /**
36
     * Make a post request.
37
     *
38
     * @param string $endpoint
39
     * @param array $params
40
     * @param array $headers
41
     *
42
     * @return array
43
     */
44
    protected function post($endpoint, $params = [], $headers = [])
45
    {
46
        return $this->request('post', $endpoint, [
47
            'headers' => $headers,
48
            'form_params' => $params,
49
        ]);
50
    }
51
52
    /**
53
     * Make a http request.
54
     *
55
     * @param string $method
56
     * @param string $endpoint
57
     * @param array $options http://docs.guzzlephp.org/en/latest/request-options.html
58
     *
59
     * @return array
60
     */
61
    protected function request($method, $endpoint, $options = [])
62
    {
63
        return $this->unwrapResponse($this->getHttpClient($this->getBaseOptions())->{$method}($endpoint, $options));
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->unwrapResp...d($endpoint, $options)) also could return the type string which is incompatible with the documented return type array.
Loading history...
64
    }
65
66
    /**
67
     * Return base Guzzle options.
68
     *
69
     * @return array
70
     */
71
    protected function getBaseOptions()
72
    {
73
        $options = [
74
            'base_uri' => method_exists($this, 'getBaseUri') ? $this->getBaseUri() : '',
75
            'timeout' => property_exists($this, 'timeout') ? $this->timeout : 5.0,
76
        ];
77
78
        return $options;
79
    }
80
81
    /**
82
     * Return http client.
83
     *
84
     * @param array $options
85
     *
86
     * @return \GuzzleHttp\Client
87
     *
88
     * @codeCoverageIgnore
89
     */
90
    protected function getHttpClient(array $options = [])
91
    {
92
        return new Client($options);
93
    }
94
95
    /**
96
     * Convert response contents to json.
97
     *
98
     * @param \Psr\Http\Message\ResponseInterface $response
99
     *
100
     * @return array|mixed
101
     */
102
    protected function unwrapResponse(ResponseInterface $response)
103
    {
104
        $contentType = $response->getHeaderLine('Content-Type');
105
        $contents = $response->getBody()->getContents();
106
107
        if (false !== stripos($contentType, 'json') || stripos($contentType, 'javascript')) {
108
            return json_decode($contents, true);
109
        } elseif (false !== stripos($contentType, 'xml')) {
110
            return json_decode(json_encode(simplexml_load_string($contents)), true);
111
        }
112
113
        return $contents;
114
    }
115
}