Passed
Push — master ( 8eb6c3...8951af )
by Sebastian
08:56
created

URIConnectionTester::configureOptions()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
c 1
b 0
f 0
dl 0
loc 22
rs 9.8666
cc 4
nc 8
nop 1
1
<?php
2
/**
3
 * @package Application Utils
4
 * @subpackage URLInfo
5
 * @see \AppUtils\URLInfo\URIConnectionTester
6
 */
7
8
declare(strict_types=1);
9
10
namespace AppUtils\URLInfo;
11
12
use AppUtils\BaseException;
13
use AppUtils\Interface_Optionable;
14
use AppUtils\RequestHelper;
15
use AppUtils\Traits_Optionable;
16
use AppUtils\URLInfo;
17
use CurlHandle;
18
19
/**
20
 * Used to test whether a URL exists / can be connected to.
21
 *
22
 * @package Application Utils
23
 * @subpackage URLInfo
24
 * @author Sebastian Mordziol <[email protected]>
25
 */
26
class URIConnectionTester implements Interface_Optionable
27
{
28
    use Traits_Optionable;
29
    
30
    private URLInfo $url;
31
    
32
    public function __construct(URLInfo $url)
33
    {
34
        $this->url = $url;
35
    }
36
    
37
    public function getDefaultOptions() : array
38
    {
39
        return array(
40
            'verify-ssl' => true,
41
            'curl-verbose' => false,
42
            'timeout' => 10
43
        );
44
    }
45
    
46
   /**
47
    * Whether to verify the host's SSL certificate, in
48
    * case of a https connection.
49
    * 
50
    * @param bool $verifySSL
51
    * @return URIConnectionTester
52
    */
53
    public function setVerifySSL(bool $verifySSL=true) : URIConnectionTester
54
    {
55
        $this->setOption('verify-ssl', $verifySSL);
56
            return $this;
57
    }
58
    
59
    public function isVerifySSLEnabled() : bool
60
    {
61
        return $this->getBoolOption('verify-ssl');
62
    }
63
    
64
    public function setVerboseMode(bool $enabled=true) : URIConnectionTester
65
    {
66
        $this->setOption('curl-verbose', $enabled);
67
        return $this;
68
    }
69
    
70
    public function isVerboseModeEnabled() : bool
71
    {
72
        return $this->getBoolOption('curl-verbose');
73
    }
74
    
75
    public function setTimeout(int $seconds) : URIConnectionTester#
76
    {
77
        $this->setOption('timeout', $seconds);
78
        return $this;
79
    }
80
    
81
    public function getTimeout() : int
82
    {
83
        return $this->getIntOption('timeout');
84
    }
85
    
86
   /**
87
    * @param resource|CurlHandle $ch
88
    */
89
    private function configureOptions($ch) : void
90
    {
91
        if($this->isVerboseModeEnabled())
92
        {
93
            curl_setopt($ch, CURLOPT_VERBOSE, true);
94
        }
95
        
96
        curl_setopt($ch, CURLOPT_URL, $this->url->getNormalized());
97
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
98
        curl_setopt($ch, CURLOPT_TIMEOUT, $this->getTimeout());
99
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
100
        
101
        if(!$this->isVerifySSLEnabled())
102
        {
103
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
104
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
105
        }
106
        
107
        if($this->url->hasUsername())
108
        {
109
            curl_setopt($ch, CURLOPT_USERNAME, $this->url->getUsername());
110
            curl_setopt($ch, CURLOPT_PASSWORD, $this->url->getPassword());
111
        }
112
    }
113
        
114
    public function canConnect() : bool
115
    {
116
        $ch = RequestHelper::createCURL();
117
        
118
        $this->configureOptions($ch);
119
        
120
        curl_exec($ch);
121
        
122
        $http_code = (int)curl_getinfo($ch, CURLINFO_HTTP_CODE);
123
        
124
        curl_close($ch);
125
        
126
        return ($http_code === 200) || ($http_code === 302);
127
    }
128
}
129