Completed
Push — master ( a67311...a01af7 )
by smiley
02:59
created

Request   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 10
Bugs 0 Features 0
Metric Value
wmc 16
c 10
b 0
f 0
lcom 1
cbo 4
dl 0
loc 112
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 2
A setOptions() 0 11 2
A getResponse() 0 5 1
A fetch() 0 9 3
A extractShortUrl() 0 9 2
B extract() 0 24 6
1
<?php
2
/**
3
 * Class Request
4
 *
5
 * @filesource   Request.php
6
 * @created      13.02.2016
7
 * @package      chillerlan\TinyCurl
8
 * @author       Smiley <[email protected]>
9
 * @copyright    2016 Smiley
10
 * @license      MIT
11
 */
12
13
namespace chillerlan\TinyCurl;
14
15
use chillerlan\TinyCurl\Response\Response;
16
17
/**
18
 *
19
 */
20
class Request{
21
22
	/**
23
	 * The cURL connection
24
	 *
25
	 * @var resource
26
	 */
27
	protected $curl;
28
29
	/**
30
	 * @var \chillerlan\TinyCurl\RequestOptions
31
	 */
32
	protected $options;
33
34
	/**
35
	 * Request constructor.
36
	 *
37
	 * @param \chillerlan\TinyCurl\RequestOptions|null $options
38
	 */
39
	public function __construct(RequestOptions $options = null){
40
		$this->setOptions($options ?: new RequestOptions);
41
	}
42
43
	/**
44
	 * @param \chillerlan\TinyCurl\RequestOptions $options
45
	 */
46
	public function setOptions(RequestOptions $options){
47
		$this->options = $options;
48
49
		$ca_info = is_file($this->options->ca_info) ? $this->options->ca_info : null;
50
		$this->options->curl_options += [
51
			CURLOPT_RETURNTRANSFER => true,
52
			CURLOPT_SSL_VERIFYPEER => (bool)$ca_info,
53
			CURLOPT_SSL_VERIFYHOST => 2, // Support for value 1 removed in cURL 7.28.1
54
			CURLOPT_CAINFO         => $ca_info,
55
		];
56
	}
57
58
	/**
59
	 * @param string $url
60
	 *
61
	 * @return \chillerlan\TinyCurl\Response\Response
62
	 */
63
	protected function getResponse($url){
64
		curl_setopt_array($this->curl, $this->options->curl_options + [CURLOPT_URL => $url]);
65
66
		return new Response($this->curl);
67
	}
68
69
	/**
70
	 * @param \chillerlan\TinyCurl\URL $url
71
	 *
72
	 * @return \chillerlan\TinyCurl\Response\Response
73
	 * @throws \chillerlan\TinyCurl\RequestException
74
	 */
75
	public function fetch(URL $url){
76
		$this->curl = curl_init();
77
78
		if(!$url->host || !in_array($url->scheme, ['http', 'https', 'ftp'], true)){
79
			throw new RequestException('$url');
80
		}
81
82
		return $this->getResponse((string)$url);
83
	}
84
85
	/**
86
	 * @param string $url
87
	 *
88
	 * @return array<string>
89
	 */
90
	public function extractShortUrl($url){
91
		$urls = [$url];
92
93
		while($url = $this->extract($url)){
94
			$urls[] = $url;
95
		}
96
97
		return $urls;
98
	}
99
100
	/**
101
	 * @param string $url
102
	 *
103
	 * @return string
104
	 * @link http://www.internoetics.com/2012/11/12/resolve-short-urls-to-their-destination-url-php-api/
105
	 */
106
	protected function extract($url){
107
		$this->curl = curl_init();
108
109
		curl_setopt_array($this->curl, [
110
			CURLOPT_FOLLOWLOCATION => false
111
		]);
112
113
		$response = $this->getResponse($url);
114
115
		$info    = $response->info;
116
		$headers = $response->headers;
117
118
		switch(true){
119
			// check curl_info()
120
			case in_array($info->http_code, range(300, 308), true) && isset($info->redirect_url) && !empty($info->redirect_url):
121
				return $info->redirect_url;
122
			// look for a location header
123
			case isset($headers->location) && !empty($headers->location):
124
				return $headers->location; // @codeCoverageIgnore
125
			default:
126
				return '';
127
		}
128
129
	}
130
131
}
132