php-guard /
curl
| 1 | <?php |
||||
| 2 | /** |
||||
| 3 | * php-guard/curl <https://github.com/php-guard/curl> |
||||
| 4 | * Copyright (C) ${YEAR} by Alexandre Le Borgne <[email protected]>. |
||||
| 5 | * |
||||
| 6 | * This program is free software: you can redistribute it and/or modify |
||||
| 7 | * it under the terms of the GNU General Public License as published by |
||||
| 8 | * the Free Software Foundation, either version 3 of the License, or |
||||
| 9 | * (at your option) any later version. |
||||
| 10 | * |
||||
| 11 | * This program is distributed in the hope that it will be useful, |
||||
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||
| 14 | * GNU General Public License for more details. |
||||
| 15 | * |
||||
| 16 | * You should have received a copy of the GNU General Public License |
||||
| 17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
||||
| 18 | */ |
||||
| 19 | |||||
| 20 | namespace PhpGuard\Curl; |
||||
| 21 | |||||
| 22 | use PhpGuard\Curl\RequestModifier\DataRequestModifier; |
||||
| 23 | use PhpGuard\Curl\RequestModifier\FileRequestModifier; |
||||
| 24 | use PhpGuard\Curl\RequestModifier\PlainTextRequestModifier; |
||||
| 25 | |||||
| 26 | class Curl |
||||
| 27 | { |
||||
| 28 | private $requestModifierPipeline; |
||||
| 29 | private $curlResponseFactory; |
||||
| 30 | private $curlRequestFactory; |
||||
| 31 | |||||
| 32 | public function __construct(?string $host = null) |
||||
| 33 | { |
||||
| 34 | $this->requestModifierPipeline = new RequestModifierPipeline(); |
||||
| 35 | $this->curlResponseFactory = new CurlResponseFactory(); |
||||
| 36 | $this->curlRequestFactory = new CurlRequestFactory($this, $host); |
||||
| 37 | |||||
| 38 | $this->requestModifierPipeline |
||||
| 39 | ->pipe(new FileRequestModifier()) |
||||
| 40 | ->pipe(new DataRequestModifier()) |
||||
| 41 | ->pipe(new PlainTextRequestModifier()); |
||||
| 42 | } |
||||
| 43 | |||||
| 44 | /** |
||||
| 45 | * @param string $url |
||||
| 46 | * @param mixed $query |
||||
| 47 | * @param array $headers |
||||
| 48 | * @param bool $throwExceptionOnHttpError |
||||
| 49 | * |
||||
| 50 | * @return CurlResponse |
||||
| 51 | * |
||||
| 52 | * @throws CurlError |
||||
| 53 | */ |
||||
| 54 | 15 | public function get(string $url, $query = null, array $headers = [], bool $throwExceptionOnHttpError = false): CurlResponse |
|||
| 55 | { |
||||
| 56 | 15 | return $this->curlRequestFactory->create('GET', $url, null, $query, $headers)->execute($throwExceptionOnHttpError); |
|||
| 57 | } |
||||
| 58 | |||||
| 59 | /** |
||||
| 60 | * @param string $url |
||||
| 61 | * @param mixed $data |
||||
| 62 | * @param mixed $query |
||||
| 63 | * @param array $headers |
||||
| 64 | * @param bool $throwExceptionOnHttpError |
||||
| 65 | * |
||||
| 66 | * @return CurlResponse |
||||
| 67 | * |
||||
| 68 | * @throws CurlError |
||||
| 69 | */ |
||||
| 70 | 18 | public function post(string $url, $data = null, $query = null, array $headers = [], bool $throwExceptionOnHttpError = false): CurlResponse |
|||
| 71 | { |
||||
| 72 | 18 | return $this->curlRequestFactory->create('POST', $url, $data, $query, $headers)->execute($throwExceptionOnHttpError); |
|||
| 73 | } |
||||
| 74 | |||||
| 75 | /** |
||||
| 76 | * @param string $url |
||||
| 77 | * @param mixed $data |
||||
| 78 | * @param mixed $query |
||||
| 79 | * @param array $headers |
||||
| 80 | * @param bool $throwExceptionOnHttpError |
||||
| 81 | * |
||||
| 82 | * @return CurlResponse |
||||
| 83 | * |
||||
| 84 | * @throws CurlError |
||||
| 85 | */ |
||||
| 86 | 3 | public function put(string $url, $data = null, $query = null, array $headers = [], bool $throwExceptionOnHttpError = false): CurlResponse |
|||
| 87 | { |
||||
| 88 | 3 | return $this->curlRequestFactory->create('PUT', $url, $data, $query, $headers)->execute($throwExceptionOnHttpError); |
|||
| 89 | } |
||||
| 90 | |||||
| 91 | /** |
||||
| 92 | * @param string $url |
||||
| 93 | * @param mixed $data |
||||
| 94 | * @param mixed $query |
||||
| 95 | * @param array $headers |
||||
| 96 | * @param bool $throwExceptionOnHttpError |
||||
| 97 | * |
||||
| 98 | * @return CurlResponse |
||||
| 99 | * |
||||
| 100 | * @throws CurlError |
||||
| 101 | */ |
||||
| 102 | 3 | public function patch(string $url, $data = null, $query = null, array $headers = [], bool $throwExceptionOnHttpError = false): CurlResponse |
|||
| 103 | { |
||||
| 104 | 3 | return $this->curlRequestFactory->create('PATCH', $url, $data, $query, $headers)->execute($throwExceptionOnHttpError); |
|||
| 105 | } |
||||
| 106 | |||||
| 107 | /** |
||||
| 108 | * @param string $url |
||||
| 109 | * @param mixed $data |
||||
| 110 | * @param mixed $query |
||||
| 111 | * @param array $headers |
||||
| 112 | * @param bool $throwExceptionOnHttpError |
||||
| 113 | * |
||||
| 114 | * @return CurlResponse |
||||
| 115 | * |
||||
| 116 | * @throws CurlError |
||||
| 117 | */ |
||||
| 118 | 3 | public function delete(string $url, $data = null, $query = null, array $headers = [], bool $throwExceptionOnHttpError = false): CurlResponse |
|||
| 119 | { |
||||
| 120 | 3 | return $this->curlRequestFactory->create('DELETE', $url, $data, $query, $headers)->execute($throwExceptionOnHttpError); |
|||
| 121 | } |
||||
| 122 | |||||
| 123 | /** |
||||
| 124 | * @param array $options Définit les options pour le gestionnaire multiple cURL |
||||
| 125 | * |
||||
| 126 | * @return MultiCurl |
||||
| 127 | */ |
||||
| 128 | 3 | public function multi(array $options = []) |
|||
| 129 | { |
||||
| 130 | 3 | return new MultiCurl($this, $options); |
|||
| 131 | } |
||||
| 132 | |||||
| 133 | /** |
||||
| 134 | * @param CurlRequest $request |
||||
| 135 | * |
||||
| 136 | * @return CurlResponse |
||||
| 137 | * |
||||
| 138 | * @throws CurlError |
||||
| 139 | */ |
||||
| 140 | 42 | public function execute(CurlRequest $request): CurlResponse |
|||
| 141 | { |
||||
| 142 | 42 | $ch = $this->requestModifierPipeline->process($request)->resource(); |
|||
| 143 | |||||
| 144 | try { |
||||
| 145 | 42 | return $this->curlResponseFactory->create($ch, curl_exec($ch)); |
|||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||
| 146 | } finally { |
||||
| 147 | 42 | curl_close($ch); |
|||
|
0 ignored issues
–
show
It seems like
$ch can also be of type false; however, parameter $ch of curl_close() does only seem to accept resource, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 148 | } |
||||
| 149 | } |
||||
| 150 | |||||
| 151 | /** |
||||
| 152 | * @return RequestModifierPipeline |
||||
| 153 | */ |
||||
| 154 | 3 | public function getRequestModifierPipeline(): RequestModifierPipeline |
|||
| 155 | { |
||||
| 156 | 3 | return $this->requestModifierPipeline; |
|||
| 157 | } |
||||
| 158 | |||||
| 159 | /** |
||||
| 160 | * @return CurlRequestFactory |
||||
| 161 | */ |
||||
| 162 | 6 | public function getCurlRequestFactory(): CurlRequestFactory |
|||
| 163 | { |
||||
| 164 | 6 | return $this->curlRequestFactory; |
|||
| 165 | } |
||||
| 166 | |||||
| 167 | /** |
||||
| 168 | * @return CurlResponseFactory |
||||
| 169 | */ |
||||
| 170 | 3 | public function getCurlResponseFactory(): CurlResponseFactory |
|||
| 171 | { |
||||
| 172 | 3 | return $this->curlResponseFactory; |
|||
| 173 | } |
||||
| 174 | } |
||||
| 175 |