CurlFactory::newCurlRequest()   B
last analyzed

Complexity

Conditions 9
Paths 16

Size

Total Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 32
rs 8.0555
c 0
b 0
f 0
cc 9
nc 16
nop 3
1
<?php
2
3
/*
4
 * This file is part of the core-library package.
5
 *
6
 * (c) 2018 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
namespace WBW\Library\Core\Network\CURL\Factory;
13
14
use InvalidArgumentException;
15
use WBW\Library\Core\Network\CURL\API\CurlRequestInterface;
16
use WBW\Library\Core\Network\CURL\API\CurlResponseInterface;
17
use WBW\Library\Core\Network\CURL\Configuration\CurlConfiguration;
18
use WBW\Library\Core\Network\CURL\Request\CurlDeleteRequest;
19
use WBW\Library\Core\Network\CURL\Request\CurlGetRequest;
20
use WBW\Library\Core\Network\CURL\Request\CurlHeadRequest;
21
use WBW\Library\Core\Network\CURL\Request\CurlOptionsRequest;
22
use WBW\Library\Core\Network\CURL\Request\CurlPatchRequest;
23
use WBW\Library\Core\Network\CURL\Request\CurlPostRequest;
24
use WBW\Library\Core\Network\CURL\Request\CurlPutRequest;
25
use WBW\Library\Core\Network\CURL\Response\CurlResponse;
26
use WBW\Library\Core\Network\HTTP\HttpInterface;
27
28
/**
29
 * cURL factory.
30
 *
31
 * @author webeweb <https://github.com/webeweb/>
32
 * @package WBW\Library\Core\Network\CURL\Factory
33
 */
34
class CurlFactory implements HttpInterface {
35
36
    /**
37
     * Create a cURL request.
38
     *
39
     * @param string $method The method.
40
     * @param CurlConfiguration|null $configuration The configuration.
41
     * @param string|null $resourcePath The resource path.
42
     * @return CurlRequestInterface Returns the cURL request.
43
     */
44
    public static function newCurlRequest(string $method, CurlConfiguration $configuration = null, string $resourcePath = null) {
45
46
        if (null === $configuration) {
47
            $configuration = new CurlConfiguration();
48
        }
49
50
        switch ($method) {
51
52
            case self::HTTP_METHOD_DELETE:
53
                return new CurlDeleteRequest($configuration, $resourcePath);
54
55
            case self::HTTP_METHOD_GET:
56
                return new CurlGetRequest($configuration, $resourcePath);
57
58
            case self::HTTP_METHOD_HEAD:
59
                return new CurlHeadRequest($configuration, $resourcePath);
60
61
            case self::HTTP_METHOD_OPTIONS:
62
                return new CurlOptionsRequest($configuration, $resourcePath);
63
64
            case self::HTTP_METHOD_PATCH:
65
                return new CurlPatchRequest($configuration, $resourcePath);
66
67
            case self::HTTP_METHOD_POST:
68
                return new CurlPostRequest($configuration, $resourcePath);
69
70
            case self::HTTP_METHOD_PUT:
71
                return new CurlPutRequest($configuration, $resourcePath);
72
        }
73
74
        throw new InvalidArgumentException(sprintf('The HTTP method "%s" is invalid', $method));
75
    }
76
77
    /**
78
     * Create a cURL response.
79
     *
80
     * @return CurlResponseInterface Returns the cURL response.
81
     */
82
    public static function newCurlResponse(): CurlResponseInterface {
83
        return new CurlResponse();
84
    }
85
}
86