Completed
Push — master ( fa1c03...012c49 )
by WEBEWEB
03:05
created

CURLFactory   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 9

Importance

Changes 0
Metric Value
wmc 9
lcom 0
cbo 9
dl 0
loc 47
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
D getInstance() 0 35 9
1
<?php
2
3
/**
4
 * This file is part of the curl-library package.
5
 *
6
 * (c) 2017 NdC/WBW
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\CURL\Factory;
13
14
use WBW\Library\Core\Exception\HTTP\InvalidHTTPMethodException;
15
use WBW\Library\Core\HTTP\HTTPMethodInterface;
16
use WBW\Library\CURL\Configuration\CURLConfiguration;
17
use WBW\Library\CURL\Request\CURLDeleteRequest;
18
use WBW\Library\CURL\Request\CURLGetRequest;
19
use WBW\Library\CURL\Request\CURLHeadRequest;
20
use WBW\Library\CURL\Request\CURLOptionsRequest;
21
use WBW\Library\CURL\Request\CURLPatchRequest;
22
use WBW\Library\CURL\Request\CURLPostRequest;
23
use WBW\Library\CURL\Request\CURLPutRequest;
24
use WBW\Library\CURL\Request\CURLRequestInterface;
25
26
/**
27
 * cURL factory.
28
 *
29
 * @author NdC/WBW <https://github.com/webeweb/>
30
 * @package WBW\Library\CURL\Factory
31
 * @final
32
 */
33
final class CURLFactory implements HTTPMethodInterface {
34
35
	/**
36
	 * Get an instance.
37
	 *
38
	 * @param string $method The method.
39
	 * @param CURLConfiguration $configuration The configuration.
40
	 * @return CURLRequestInterface Returns the cURL request.
41
	 * @throws InvalidHTTPMethodException Throws an invalid HTTP method exception if the method is not implemented.
42
	 */
43
	public static function getInstance($method, CURLConfiguration $configuration = null, $resourcePath = null) {
44
45
		// Check the configuration.
46
		if (null === $configuration) {
47
			$configuration = new CURLConfiguration();
48
		}
49
50
		// Switch into $method.
51
		switch ($method) {
52
53
			case self::METHOD_DELETE:
54
				return new CURLDeleteRequest($configuration, $resourcePath);
55
56
			case self::METHOD_GET:
57
				return new CURLGetRequest($configuration, $resourcePath);
58
59
			case self::METHOD_HEAD:
60
				return new CURLHeadRequest($configuration, $resourcePath);
61
62
			case self::METHOD_OPTIONS:
63
				return new CURLOptionsRequest($configuration, $resourcePath);
64
65
			case self::METHOD_PATCH:
66
				return new CURLPatchRequest($configuration, $resourcePath);
67
68
			case self::METHOD_POST:
69
				return new CURLPostRequest($configuration, $resourcePath);
70
71
			case self::METHOD_PUT:
72
				return new CURLPutRequest($configuration, $resourcePath);
73
74
			default:
75
				throw new InvalidHTTPMethodException($method);
76
		}
77
	}
78
79
}
80