|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
/** |
|
4
|
|
|
* |
|
5
|
|
|
* Copyright (C) 2018 Ross Mitchell |
|
6
|
|
|
* |
|
7
|
|
|
* This file is part of RossMitchell/UpdateCloudFlare. |
|
8
|
|
|
* |
|
9
|
|
|
* RossMitchell/UpdateCloudFlare is free software: you can redistribute |
|
10
|
|
|
* it and/or modify it under the terms of the GNU General Public License |
|
11
|
|
|
* as published by the Free Software Foundation, either version 3 of the |
|
12
|
|
|
* License, or (at your option) any later version. |
|
13
|
|
|
* |
|
14
|
|
|
* This program is distributed in the hope that it will be useful, |
|
15
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
16
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
17
|
|
|
* GNU General Public License for more details. |
|
18
|
|
|
* |
|
19
|
|
|
* You should have received a copy of the GNU General Public License |
|
20
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
21
|
|
|
*/ |
|
22
|
|
|
|
|
23
|
|
|
namespace RossMitchell\UpdateCloudFlare\Model; |
|
24
|
|
|
|
|
25
|
|
|
|
|
26
|
|
|
use RossMitchell\UpdateCloudFlare\Interfaces\CurlInterface; |
|
27
|
|
|
use RossMitchell\UpdateCloudFlare\Interfaces\RequestInterface; |
|
28
|
|
|
use Symfony\Component\Console\Exception\LogicException; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Class Curl |
|
32
|
|
|
* @package RossMitchell\UpdateCloudFlare\Model |
|
33
|
|
|
*/ |
|
34
|
|
|
class Curl implements CurlInterface |
|
35
|
|
|
{ |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @param RequestInterface $request |
|
39
|
|
|
* |
|
40
|
|
|
* @return mixed |
|
41
|
|
|
* @throws LogicException |
|
42
|
|
|
* @throws \RuntimeException |
|
43
|
|
|
*/ |
|
44
|
|
|
public function makeRequest(RequestInterface $request) |
|
45
|
|
|
{ |
|
46
|
|
|
$curl = \curl_init(); |
|
47
|
|
|
$this->setRequiredCurlOptions($request, $curl); |
|
48
|
|
|
$this->setOptionalCurlOptions($request, $curl); |
|
49
|
|
|
|
|
50
|
|
|
return $this->sendRequest($curl); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @param RequestInterface $request |
|
55
|
|
|
* @param $curl |
|
56
|
|
|
*/ |
|
57
|
|
|
private function setRequiredCurlOptions(RequestInterface $request, $curl): void |
|
58
|
|
|
{ |
|
59
|
|
|
\curl_setopt($curl, \CURLOPT_URL, $request->getUrl()); |
|
60
|
|
|
\curl_setopt($curl, \CURLOPT_USERAGENT, 'curl'); |
|
61
|
|
|
\curl_setopt($curl, \CURLOPT_CUSTOMREQUEST, $request->getRequestType()); |
|
62
|
|
|
\curl_setopt($curl, \CURLOPT_RETURNTRANSFER, true); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @param RequestInterface $request |
|
67
|
|
|
* @param $curl |
|
68
|
|
|
* |
|
69
|
|
|
* @throws LogicException |
|
70
|
|
|
*/ |
|
71
|
|
|
private function setOptionalCurlOptions(RequestInterface $request, $curl): void |
|
72
|
|
|
{ |
|
73
|
|
|
$fields = $request->getFields(); |
|
74
|
|
|
$headers = $request->getHeaders(); |
|
75
|
|
|
$hasFields = \count($fields) > 0; |
|
76
|
|
|
$hasHeaders = \count($headers) > 0; |
|
77
|
|
|
if ($hasFields === true) { |
|
78
|
|
|
\curl_setopt($curl, \CURLOPT_POSTFIELDS, $this->getFieldsString($fields)); |
|
79
|
|
|
} |
|
80
|
|
|
if ($hasHeaders === true) { |
|
81
|
|
|
\curl_setopt($curl, \CURLOPT_HTTPHEADER, $headers); |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @param $curl |
|
87
|
|
|
* |
|
88
|
|
|
* @return mixed |
|
89
|
|
|
* @throws \RuntimeException |
|
90
|
|
|
*/ |
|
91
|
|
|
private function sendRequest($curl) |
|
92
|
|
|
{ |
|
93
|
|
|
$result = \curl_exec($curl); |
|
94
|
|
|
|
|
95
|
|
|
if (\curl_error($curl)) { |
|
96
|
|
|
throw new \RuntimeException(\curl_error($curl)); |
|
97
|
|
|
} |
|
98
|
|
|
\curl_close($curl); |
|
99
|
|
|
|
|
100
|
|
|
return $result; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Converts an array of fields into a string that can be sent through |
|
105
|
|
|
* |
|
106
|
|
|
* @param array $fields |
|
107
|
|
|
* |
|
108
|
|
|
* @return string |
|
109
|
|
|
* @throws LogicException |
|
110
|
|
|
*/ |
|
111
|
|
|
private function getFieldsString(array $fields): string |
|
112
|
|
|
{ |
|
113
|
|
|
$fieldsString = \json_encode($fields); |
|
114
|
|
|
if ($fieldsString === false) { |
|
115
|
|
|
throw new LogicException('Unable to encode the fields'); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
return $fieldsString; |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|