• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

webeweb / core-library / 16553382373

27 Jul 2025 04:49PM UTC coverage: 99.806% (-0.03%) from 99.832%
16553382373

push

github

webeweb
Update unit tests

87525 of 87695 relevant lines covered (99.81%)

18.48 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

87.8
/lib/curl/Helper/CurlHelper.php
1
<?php
2

3
/*
4
 * This file is part of the core-library package.
5
 *
6
 * (c) 2019 WEBEWEB
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11

12
declare(strict_types = 1);
13

14
namespace WBW\Library\Curl\Helper;
15

16
use DateTime;
17
use Throwable;
18
use WBW\Library\Curl\Configuration\Configuration;
19
use WBW\Library\Curl\Request\RequestInterface;
20
use WBW\Library\Curl\Response\ResponseInterface;
21

22
/**
23
 * cURL helper.
24
 *
25
 * @author webeweb <https://github.com/webeweb>
26
 * @package WBW\Library\Curl\Helper
27
 */
28
class CurlHelper {
29

30
    /**
31
     * Enumerate the codes.
32
     *
33
     * @return int[] Returns the codes enumeration.
34
     */
35
    public static function enumCodes(): array {
36

37
        return [
7✔
38
            ResponseInterface::CODE_CONTINUE,
9✔
39
            ResponseInterface::CODE_SWITCHING_PROTOCOLS,
9✔
40
            ResponseInterface::CODE_PROCESSING,
9✔
41
            ResponseInterface::CODE_OK,
9✔
42
            ResponseInterface::CODE_CREATED,
9✔
43
            ResponseInterface::CODE_ACCEPTED,
9✔
44
            ResponseInterface::CODE_NON_AUTHORITATIVE_INFORMATION,
9✔
45
            ResponseInterface::CODE_NO_CONTENT,
9✔
46
            ResponseInterface::CODE_RESET_CONTENT,
9✔
47
            ResponseInterface::CODE_PARTIAL_CONTENT,
9✔
48
            ResponseInterface::CODE_MULTI_STATUS,
9✔
49
            ResponseInterface::CODE_ALREADY_REPORTED,
9✔
50
            ResponseInterface::CODE_IM_USED,
9✔
51
            ResponseInterface::CODE_MULTIPLE_CHOICES,
9✔
52
            ResponseInterface::CODE_MOVED_PERMANENTLY,
9✔
53
            ResponseInterface::CODE_MOVED_TEMPORARILY,
9✔
54
            ResponseInterface::CODE_SEE_OTHER,
9✔
55
            ResponseInterface::CODE_NOT_MODIFIED,
9✔
56
            ResponseInterface::CODE_USE_PROXY,
9✔
57
            ResponseInterface::CODE_TEMPORARY_REDIRECT,
9✔
58
            ResponseInterface::CODE_PERMANENT_REDIRECT,
9✔
59
            ResponseInterface::CODE_BAD_REQUEST,
9✔
60
            ResponseInterface::CODE_UNAUTHORIZED,
9✔
61
            ResponseInterface::CODE_PAYMENT_REQUIRED,
9✔
62
            ResponseInterface::CODE_FORBIDDEN,
9✔
63
            ResponseInterface::CODE_NOT_FOUND,
9✔
64
            ResponseInterface::CODE_METHOD_NOT_ALLOWED,
9✔
65
            ResponseInterface::CODE_NOT_ACCEPTABLE,
9✔
66
            ResponseInterface::CODE_PROXY_AUTHENTICATION_REQUIRED,
9✔
67
            ResponseInterface::CODE_REQUEST_TIME_OUT,
9✔
68
            ResponseInterface::CODE_CONFLICT,
9✔
69
            ResponseInterface::CODE_GONE,
9✔
70
            ResponseInterface::CODE_LENGTH_REQUIRED,
9✔
71
            ResponseInterface::CODE_PRECONDITION_FAILED,
9✔
72
            ResponseInterface::CODE_REQUEST_ENTITY_TOO_LARGE,
9✔
73
            ResponseInterface::CODE_REQUEST_URI_TOO_LONG,
9✔
74
            ResponseInterface::CODE_UNSUPPORTED_MEDIA_TYPE,
9✔
75
            ResponseInterface::CODE_REQUESTED_RANGE_UNSATISFIABLE,
9✔
76
            ResponseInterface::CODE_EXPECTATION_FAILED,
9✔
77
            ResponseInterface::CODE_UNPROCESSABLE_ENTITY,
9✔
78
            ResponseInterface::CODE_LOCKED,
9✔
79
            ResponseInterface::CODE_METHOD_FAILURE,
9✔
80
            ResponseInterface::CODE_UPGRADE_REQUIRED,
9✔
81
            ResponseInterface::CODE_PRECONDITION_REQUIRED,
9✔
82
            ResponseInterface::CODE_TOO_MANY_REQUESTS,
9✔
83
            ResponseInterface::CODE_REQUEST_HEADER_FIELDS_TOO_LARGE,
9✔
84
            ResponseInterface::CODE_INTERNAL_SERVER_ERROR,
9✔
85
            ResponseInterface::CODE_NOT_IMPLEMENTED,
9✔
86
            ResponseInterface::CODE_BAD_GATEWAY_OU_PROXY_ERROR,
9✔
87
            ResponseInterface::CODE_SERVICE_UNAVAILABLE,
9✔
88
            ResponseInterface::CODE_GATEWAY_TIME_OUT,
9✔
89
            ResponseInterface::CODE_HTTP_VERSION_NOT_SUPPORTED,
9✔
90
            ResponseInterface::CODE_VARIANT_ALSO_NEGOTIATES,
9✔
91
            ResponseInterface::CODE_INSUFFICIENT_STORAGE,
9✔
92
            ResponseInterface::CODE_LOOP_DETECTED,
9✔
93
            ResponseInterface::CODE_NOT_EXTENDED,
9✔
94
            ResponseInterface::CODE_NETWORK_AUTHENTICATION_REQUIRED,
9✔
95
        ];
7✔
96
    }
97

98
    /**
99
     * Enumerate the methods.
100
     *
101
     * @return string[] Returns the methods enumeration.
102
     */
103
    public static function enumMethods(): array {
104

105
        return [
7✔
106
            RequestInterface::METHOD_DELETE,
9✔
107
            RequestInterface::METHOD_GET,
7✔
108
            RequestInterface::METHOD_HEAD,
7✔
109
            RequestInterface::METHOD_OPTIONS,
7✔
110
            RequestInterface::METHOD_PATCH,
7✔
111
            RequestInterface::METHOD_POST,
7✔
112
            RequestInterface::METHOD_PUT,
7✔
113
        ];
7✔
114
    }
115

116
    /**
117
     * Initialize a stream.
118
     *
119
     * @param string $url The URL.
120
     * @param Configuration $config The configuration.
121
     * @return resource Returns the stream.
122
     */
123
    public static function initStream(string $url, Configuration $config) {
124

125
        $stream = curl_init();
18✔
126

127
        curl_setopt($stream, CURLOPT_URL, $url);
18✔
128

129
        if (true === $config->getAllowEncoding()) {
18✔
130
            curl_setopt($stream, CURLOPT_ENCODING, "");
×
131
        }
132

133
        if (0 < $config->getConnectTimeout()) {
18✔
134
            curl_setopt($stream, CURLOPT_CONNECTTIMEOUT, $config->getConnectTimeout());
×
135
        }
136

137
        return $stream;
18✔
138
    }
139

140
    /**
141
     * Set the headers.
142
     *
143
     * @param resource $stream The stream.
144
     * @param array<string,string> $headers The headers.
145
     * @return void
146
     */
147
    public static function setHeaders($stream, array $headers): void {
148
        curl_setopt($stream, CURLOPT_HEADER, 1);
9✔
149
        curl_setopt($stream, CURLOPT_HTTPHEADER, $headers);
9✔
150
    }
2✔
151

152
    /**
153
     * Set POST.
154
     *
155
     * @param resource $stream The stream.
156
     * @param string $method The HTTP method.
157
     * @param string $postData The POST data.
158
     * @return void
159
     */
160
    public static function setPost($stream, string $method, string $postData): void {
161

162
        switch ($method) {
1✔
163

164
            case RequestInterface::METHOD_DELETE:
1✔
165
            case RequestInterface::METHOD_OPTIONS:
1✔
166
            case RequestInterface::METHOD_PATCH:
1✔
167
            case RequestInterface::METHOD_PUT:
1✔
168
                curl_setopt($stream, CURLOPT_CUSTOMREQUEST, $method);
×
169
                curl_setopt($stream, CURLOPT_POSTFIELDS, $postData);
×
170
                break;
×
171

172
            case RequestInterface::METHOD_HEAD:
1✔
173
                curl_setopt($stream, CURLOPT_NOBODY, true);
×
174
                break;
×
175

176
            case RequestInterface::METHOD_POST:
1✔
177
                curl_setopt($stream, CURLOPT_POST, true);
9✔
178
                curl_setopt($stream, CURLOPT_POSTFIELDS, $postData);
9✔
179
                break;
9✔
180
        }
181
    }
2✔
182

183
    /**
184
     * Set proxy.
185
     *
186
     * @param resource $stream The stream.
187
     * @param Configuration $config The configuration.
188
     * @return void
189
     */
190
    public static function setProxy($stream, Configuration $config): void {
191

192
        if (null !== $config->getProxyHost()) {
18✔
193
            curl_setopt($stream, CURLOPT_PROXY, $config->getProxyHost());
9✔
194
        }
195

196
        if (null !== $config->getProxyPort()) {
18✔
197
            curl_setopt($stream, CURLOPT_PROXYPORT, $config->getProxyPort());
9✔
198
        }
199

200
        if (null !== $config->getProxyType()) {
18✔
201
            curl_setopt($stream, CURLOPT_PROXYTYPE, $config->getProxyType());
9✔
202
        }
203

204
        if (null !== $config->getProxyUsername()) {
18✔
205
            curl_setopt($stream, CURLOPT_PROXYUSERPWD, implode(":", [$config->getProxyUsername(), $config->getProxyPassword()]));
9✔
206
        }
207
    }
4✔
208

209
    /**
210
     * Set return transfer.
211
     *
212
     * @param resource $stream The stream.
213
     * @return void
214
     */
215
    public static function setReturnTransfer($stream): void {
216
        curl_setopt($stream, CURLOPT_RETURNTRANSFER, true);
9✔
217
    }
2✔
218

219
    /**
220
     * Set SSL.
221
     *
222
     * @param resource $stream The stream.
223
     * @param Configuration $config The configuration.
224
     * @return void
225
     */
226
    public static function setSsl($stream, Configuration $config): void {
227
        if (false === $config->getSslVerification()) {
9✔
228
            curl_setopt($stream, CURLOPT_SSL_VERIFYHOST, 0);
×
229
            curl_setopt($stream, CURLOPT_SSL_VERIFYPEER, 0);
×
230
        }
231
    }
2✔
232

233
    /**
234
     * Set timeout.
235
     *
236
     * @param resource $stream The stream.
237
     * @param Configuration $config The configuration.
238
     * @return void
239
     */
240
    public static function setTimeout($stream, Configuration $config): void {
241
        if (0 < $config->getRequestTimeout()) {
9✔
242
            curl_setopt($stream, CURLOPT_TIMEOUT, $config->getRequestTimeout());
×
243
        }
244
    }
2✔
245

246
    /**
247
     * Set user agent.
248
     *
249
     * @param resource $stream The stream.
250
     * @param Configuration $config The configuration.
251
     * @return void
252
     */
253
    public static function setUserAgent($stream, Configuration $config): void {
254
        curl_setopt($stream, CURLOPT_USERAGENT, $config->getUserAgent());
9✔
255
    }
2✔
256

257
    /**
258
     * Set verbose.
259
     *
260
     * @param resource $stream The stream.
261
     * @param Configuration $config The configuration.
262
     * @param string $url The URL.
263
     * @param string $postData The POST data.
264
     * @return void
265
     * @throws Throwable Throws an exception if an error occurs.
266
     */
267
    public static function setVerbose($stream, Configuration $config, string $url, string $postData): void {
268

269
        if (true === $config->getDebug()) {
9✔
270

271
            curl_setopt($stream, CURLOPT_STDERR, fopen($config->getDebugFile(), "a"));
×
272
            curl_setopt($stream, CURLOPT_VERBOSE, 0);
×
273

274
            $msg = (new DateTime())->format("c") . " [DEBUG] $url" . PHP_EOL . "HTTP request body ~BEGIN~" . PHP_EOL . print_r($postData, true) . PHP_EOL . "~END~" . PHP_EOL;
×
275
            error_log($msg, 3, $config->getDebugFile());
×
276
        } else {
277

278
            if (true === $config->getVerbose()) {
9✔
279
                curl_setopt($stream, CURLOPT_VERBOSE, 1);
×
280
            } else {
281
                curl_setopt($stream, CURLOPT_VERBOSE, 0);
9✔
282
            }
283
        }
284
    }
2✔
285
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc