Completed
Push — master ( 339f55...221a44 )
by smiley
02:37
created

HTTPOptionsTrait::HTTPOptionsTrait()   B

Complexity

Conditions 8
Paths 8

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
nc 8
nop 0
dl 0
loc 20
rs 8.4444
c 0
b 0
f 0
1
<?php
2
/**
3
 * Trait HTTPOptionsTrait
4
 *
5
 * @filesource   HTTPOptionsTrait.php
6
 * @created      28.08.2018
7
 * @package      chillerlan\HTTP
8
 * @author       Smiley <[email protected]>
9
 * @copyright    2018 Smiley
10
 * @license      MIT
11
 */
12
13
namespace chillerlan\HTTP;
14
15
trait HTTPOptionsTrait{
16
17
	/**
18
	 * @var string
19
	 */
20
	public $user_agent = 'chillerlanHttpInterface/2.0 +https://github.com/chillerlan/php-httpinterface';
21
22
	/**
23
	 * options for each curl instance
24
	 *
25
	 * this array is being merged into the default options as the last thing before curl_exec().
26
	 * none of the values (except existence of the CA file) will be checked - that's up to the implementation.
27
	 *
28
	 * @var array
29
	 */
30
	public $curl_options = [];
31
32
	/**
33
	 * CA Root Certificates for use with CURL/SSL (if not configured in php.ini)
34
	 *
35
	 * @var string
36
	 * @link https://curl.haxx.se/ca/cacert.pem
37
	 * @link https://raw.githubusercontent.com/bagder/ca-bundle/master/ca-bundle.crt
38
	 */
39
	public $ca_info = null;
40
41
	/**
42
	 * HTTPOptionsTrait constructor
43
	 *
44
	 * @throws \chillerlan\HTTP\ClientException
45
	 */
46
	protected function HTTPOptionsTrait():void{
47
48
		if(!is_array($this->curl_options)){
49
			$this->curl_options = [];
50
		}
51
52
		// we cannot verify a peer against a non-existent ca file, so turn it off in that case
53
		if(!$this->ca_info || !is_file($this->ca_info)
54
		   || (isset($this->curl_options[CURLOPT_CAINFO]) && !is_file($this->curl_options[CURLOPT_CAINFO]))){
55
56
			$this->curl_options += [
57
				CURLOPT_SSL_VERIFYPEER => false,
58
				CURLOPT_CAINFO         => null,
59
			];
60
		}
61
62
		if(!is_string($this->user_agent) || empty(trim($this->user_agent))){
63
			throw new ClientException('invalid user agent');
64
		}
65
	}
66
67
}
68