Passed
Pull Request — master (#6)
by Mattia
32:50 queued 25:28
created

HttpClient   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 176
Duplicated Lines 0 %

Test Coverage

Coverage 95.35%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 36
c 2
b 0
f 0
dl 0
loc 176
ccs 41
cts 43
cp 0.9535
rs 10
wmc 16

13 Methods

Rating   Name   Duplication   Size   Complexity  
A setHttpAuthPass() 0 3 1
A getHttpRequestConfig() 0 3 1
A setHttpClient() 0 3 1
A setConnectTimeout() 0 3 1
A setHttpProxy() 0 3 1
A setRequestTimeout() 0 3 1
A setCustomHttpRequestConfig() 0 3 1
A getHttpClient() 0 3 1
A setHttpVerifySsl() 0 3 1
A httpPost() 0 6 1
A setHttpAuthUser() 0 3 1
A generateHttpClient() 0 3 1
A baseHttpRequestConfig() 0 23 4
1
<?php
2
3
namespace PagOnline\Traits;
4
5
use GuzzleHttp\Client;
6
7
/**
8
 * Trait HttpClient.
9
 */
10
trait HttpClient
11
{
12
    /**
13
     * @var int
14
     */
15
    protected $connectTimeout = 5;
16
17
    /**
18
     * @var int
19
     */
20
    protected $requestTimeout = 30;
21
22
    /**
23
     * @var \GuzzleHttp\Client
24
     */
25
    protected $httpClient;
26
27
    /**
28
     * @var array
29
     */
30
    protected $httpCustomConfiguration = [];
31
32
    /**
33
     * @var string
34
     */
35
    protected $httpAuthUser = '';
36
37
    /**
38
     * @var string
39
     */
40
    protected $httpAuthPass = '';
41
42
    /**
43
     * @var string
44
     */
45
    protected $httpProxy = '';
46
47
    /**
48
     * @var bool
49
     */
50
    protected $httpVerifySsl = true;
51
52
    /**
53
     * @param array $configuration
54
     */
55 1
    public function setCustomHttpRequestConfig(array $configuration): void
56
    {
57 1
        $this->httpCustomConfiguration = $configuration;
58
    }
59
60
    /**
61
     * @param string $httpProxy
62
     */
63 2
    public function setHttpProxy(string $httpProxy): void
64
    {
65 2
        $this->httpProxy = $httpProxy;
66
    }
67
68
    /**
69
     * @param string $httpAuthUser
70
     */
71 2
    public function setHttpAuthUser(string $httpAuthUser): void
72
    {
73 2
        $this->httpAuthUser = $httpAuthUser;
74
    }
75
76
    /**
77
     * @param string $httpAuthPass
78
     */
79 1
    public function setHttpAuthPass(string $httpAuthPass): void
80
    {
81 1
        $this->httpAuthPass = $httpAuthPass;
82
    }
83
84
    /**
85
     * @param int $connectTimeout
86
     */
87 1
    public function setConnectTimeout(int $connectTimeout): void
88
    {
89 1
        $this->connectTimeout = $connectTimeout;
90
    }
91
92
    /**
93
     * @param int $requestTimeout
94
     */
95 1
    public function setRequestTimeout(int $requestTimeout): void
96
    {
97 1
        $this->requestTimeout = $requestTimeout;
98
    }
99
100
    /**
101
     * @return Client
102
     */
103
    public function getHttpClient(): Client
104
    {
105
        return $this->httpClient;
106
    }
107
108
    /**
109
     * @param Client $httpClient
110
     */
111 255
    public function setHttpClient(Client $httpClient): void
112
    {
113 255
        $this->httpClient = $httpClient;
114
    }
115
116
    /**
117
     * Generate Http Client.
118
     */
119 255
    public function generateHttpClient()
120
    {
121 255
        $this->setHttpClient(new Client());
122
    }
123
124
    /**
125
     * @param bool $httpVerifySsl
126
     */
127 1
    public function setHttpVerifySsl(bool $httpVerifySsl): void
128
    {
129 1
        $this->httpVerifySsl = $httpVerifySsl;
130
    }
131
132
    /**
133
     * Create configuration array for Guzzle Client.
134
     *
135
     * @return array
136
     */
137 7
    protected function baseHttpRequestConfig()
138
    {
139 7
        $configuration = [
140 7
            \GuzzleHttp\RequestOptions::TIMEOUT => $this->requestTimeout,
141 7
            \GuzzleHttp\RequestOptions::CONNECT_TIMEOUT => $this->connectTimeout,
142 7
            \GuzzleHttp\RequestOptions::VERIFY => $this->httpVerifySsl,
143 7
            \GuzzleHttp\RequestOptions::HEADERS => [
144 7
                'Content-Type' => 'text/xml; charset="utf-8"',
145 7
            ],
146 7
        ];
147
148 7
        if (!empty($this->httpProxy)) {
149 1
            $configuration[\GuzzleHttp\RequestOptions::PROXY] = $this->httpProxy;
150
        }
151
152 7
        if ($this->httpAuthUser !== null && $this->httpAuthPass !== null) {
153 7
            $configuration[\GuzzleHttp\RequestOptions::AUTH] = [
154 7
                $this->httpAuthUser,
155 7
                $this->httpAuthPass,
156 7
            ];
157
        }
158
159 7
        return $configuration;
160
    }
161
162
    /**
163
     * Get Http Request config.
164
     *
165
     * @return array
166
     */
167 7
    protected function getHttpRequestConfig()
168
    {
169 7
        return \array_merge($this->baseHttpRequestConfig(), $this->httpCustomConfiguration);
170
    }
171
172
    /**
173
     * Make HTTP Post request.
174
     *
175
     * @param string $url
176
     * @param string $request
177
     *
178
     * @return \Psr\Http\Message\ResponseInterface
179
     */
180 7
    protected function httpPost($url, $request)
181
    {
182 7
        $configuration = $this->getHttpRequestConfig();
183 7
        $configuration[\GuzzleHttp\RequestOptions::BODY] = $request;
184
185 7
        return $this->httpClient->post($url, $configuration);
186
    }
187
}
188