Passed
Push — master ( b083ab...a3db06 )
by Sebastian
03:30
created

URIConnectionTester::configureOptions()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 12
c 0
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\Traits_Optionable;
15
use AppUtils\URLInfo;
16
17
/**
18
 * Used to test whether a URL exists / can be connected to.
19
 *
20
 * @package Application Utils
21
 * @subpackage URLInfo
22
 * @author Sebastian Mordziol <[email protected]>
23
 */
24
class URIConnectionTester implements Interface_Optionable
25
{
26
    use Traits_Optionable;
27
    
28
    private URLInfo $url;
29
    
30
    public function __construct(URLInfo $url)
31
    {
32
        $this->url = $url;
33
    }
34
    
35
    public function getDefaultOptions() : array
36
    {
37
        return array(
38
            'verify-ssl' => true,
39
            'curl-verbose' => false,
40
            'timeout' => 10
41
        );
42
    }
43
    
44
   /**
45
    * Whether to verify the host's SSL certificate, in
46
    * case of a https connection.
47
    * 
48
    * @param bool $verifySSL
49
    * @return URIConnectionTester
50
    */
51
    public function setVerifySSL(bool $verifySSL=true) : URIConnectionTester
52
    {
53
        $this->setOption('verify-ssl', $verifySSL);
54
            return $this;
55
    }
56
    
57
    public function isVerifySSLEnabled() : bool
58
    {
59
        return $this->getBoolOption('verify-ssl');
60
    }
61
    
62
    public function setVerboseMode(bool $enabled=true) : URIConnectionTester
63
    {
64
        $this->setOption('curl-verbose', $enabled);
65
        return $this;
66
    }
67
    
68
    public function isVerboseModeEnabled() : bool
69
    {
70
        return $this->getBoolOption('curl-verbose');
71
    }
72
    
73
    public function setTimeout(int $seconds) : URIConnectionTester#
74
    {
75
        $this->setOption('timeout', $seconds);
76
        return $this;
77
    }
78
    
79
    public function getTimeout() : int
80
    {
81
        return $this->getIntOption('timeout');
82
    }
83
    
84
   /**
85
    * Initializes the CURL instance.
86
    * 
87
    * @throws BaseException
88
    * @return resource
89
    */
90
    private function initCURL()
91
    {
92
        $ch = curl_init();
93
        
94
        if(!is_resource($ch))
95
        {
96
            throw new URLException(
97
                'Could not initialize a new cURL instance.',
98
                'Calling curl_init returned false. Additional information is not available.',
99
                URLInfo::ERROR_CURL_INIT_FAILED
100
            );
101
        }
102
        
103
        return $ch;
104
    }
105
    
106
   /**
107
    * @param resource $ch
108
    */
109
    private function configureOptions($ch) : void
110
    {
111
        if($this->isVerboseModeEnabled())
112
        {
113
            curl_setopt($ch, CURLOPT_VERBOSE, true);
114
        }
115
        
116
        curl_setopt($ch, CURLOPT_URL, $this->url->getNormalized());
117
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
118
        curl_setopt($ch, CURLOPT_TIMEOUT, $this->getTimeout());
119
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
120
        
121
        if(!$this->isVerifySSLEnabled())
122
        {
123
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
124
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
125
        }
126
        
127
        if($this->url->hasUsername())
128
        {
129
            curl_setopt($ch, CURLOPT_USERNAME, $this->url->getUsername());
130
            curl_setopt($ch, CURLOPT_PASSWORD, $this->url->getPassword());
131
        }
132
    }
133
        
134
    public function canConnect() : bool
135
    {
136
        $ch = $this->initCURL();
137
        
138
        $this->configureOptions($ch);
139
        
140
        curl_exec($ch);
141
        
142
        $http_code = (int)curl_getinfo($ch, CURLINFO_HTTP_CODE);
143
        
144
        curl_close($ch);
145
        
146
        return ($http_code === 200) || ($http_code === 302);
147
    }
148
}
149