|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Copyright (C) 2016 SURFnet. |
|
4
|
|
|
* |
|
5
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
6
|
|
|
* it under the terms of the GNU Affero General Public License as |
|
7
|
|
|
* published by the Free Software Foundation, either version 3 of the |
|
8
|
|
|
* License, or (at your option) any later version. |
|
9
|
|
|
* |
|
10
|
|
|
* This program is distributed in the hope that it will be useful, |
|
11
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13
|
|
|
* GNU Affero General Public License for more details. |
|
14
|
|
|
* |
|
15
|
|
|
* You should have received a copy of the GNU Affero General Public License |
|
16
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
17
|
|
|
*/ |
|
18
|
|
|
|
|
19
|
|
|
namespace SURFnet\VPN\Common\HttpClient; |
|
20
|
|
|
|
|
21
|
|
|
use RuntimeException; |
|
22
|
|
|
|
|
23
|
|
|
class CurlHttpClient implements HttpClientInterface |
|
24
|
|
|
{ |
|
25
|
|
|
/** @var resource */ |
|
26
|
|
|
private $curlChannel; |
|
27
|
|
|
|
|
28
|
|
|
/** @var array */ |
|
29
|
|
|
private $authInfo; |
|
30
|
|
|
|
|
31
|
|
|
public function __construct(array $authInfo) |
|
32
|
|
|
{ |
|
33
|
|
|
if (false === $this->curlChannel = curl_init()) { |
|
34
|
|
|
throw new RuntimeException('unable to create cURL channel'); |
|
35
|
|
|
} |
|
36
|
|
|
$this->authInfo = $authInfo; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function __destruct() |
|
40
|
|
|
{ |
|
41
|
|
|
curl_close($this->curlChannel); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
View Code Duplication |
public function get($requestUri) |
|
|
|
|
|
|
45
|
|
|
{ |
|
46
|
|
|
$curlOptions = [ |
|
47
|
|
|
CURLOPT_USERPWD => sprintf('%s:%s', $this->authInfo[0], $this->authInfo[1]), |
|
48
|
|
|
CURLOPT_URL => $requestUri, |
|
49
|
|
|
CURLOPT_HEADER => 0, |
|
50
|
|
|
CURLOPT_RETURNTRANSFER => 1, |
|
51
|
|
|
CURLOPT_FOLLOWLOCATION => 0, |
|
52
|
|
|
CURLOPT_PROTOCOLS => CURLPROTO_HTTP | CURLPROTO_HTTPS, |
|
53
|
|
|
]; |
|
54
|
|
|
|
|
55
|
|
|
if (false === curl_setopt_array($this->curlChannel, $curlOptions)) { |
|
56
|
|
|
throw new RuntimeException('unable to set cURL options'); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
if (false === $responseData = curl_exec($this->curlChannel)) { |
|
60
|
|
|
throw new RuntimeException('failure performing the HTTP request'); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
$responseData = json_decode($responseData, true); |
|
64
|
|
|
// XXX check for errors! |
|
65
|
|
|
|
|
66
|
|
|
return [ |
|
67
|
|
|
curl_getinfo($this->curlChannel, CURLINFO_HTTP_CODE), |
|
68
|
|
|
$responseData, |
|
69
|
|
|
]; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
View Code Duplication |
public function post($requestUri, array $postData = []) |
|
|
|
|
|
|
73
|
|
|
{ |
|
74
|
|
|
$curlOptions = [ |
|
75
|
|
|
CURLOPT_USERPWD => sprintf('%s:%s', $this->authInfo[0], $this->authInfo[1]), |
|
76
|
|
|
CURLOPT_URL => $requestUri, |
|
77
|
|
|
CURLOPT_HEADER => 0, |
|
78
|
|
|
CURLOPT_RETURNTRANSFER => 1, |
|
79
|
|
|
CURLOPT_FOLLOWLOCATION => 0, |
|
80
|
|
|
CURLOPT_PROTOCOLS => CURLPROTO_HTTP | CURLPROTO_HTTPS, |
|
81
|
|
|
CURLOPT_POSTFIELDS => http_build_query($postData), |
|
82
|
|
|
]; |
|
83
|
|
|
|
|
84
|
|
|
if (false === curl_setopt_array($this->curlChannel, $curlOptions)) { |
|
85
|
|
|
throw new RuntimeException('unable to set cURL options'); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
if (false === $responseData = curl_exec($this->curlChannel)) { |
|
89
|
|
|
throw new RuntimeException('failure performing the HTTP request'); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
$responseData = json_decode($responseData, true); |
|
93
|
|
|
// XXX check for errors! |
|
94
|
|
|
|
|
95
|
|
|
return [ |
|
96
|
|
|
curl_getinfo($this->curlChannel, CURLINFO_HTTP_CODE), |
|
97
|
|
|
$responseData, |
|
98
|
|
|
]; |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.