Passed
Push — master ( be6392...089fbf )
by Sebastian
02:55
created

URLInfo_ConnectionTester::initCURL()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
c 1
b 0
f 0
dl 0
loc 16
rs 10
cc 2
nc 2
nop 0
1
<?php
2
/**
3
 * File containing the {@see AppUtils\URLInfo_ConnectionTester} class.
4
 *
5
 * @package Application Utils
6
 * @subpackage URLInfo
7
 * @see AppUtils\URLInfo_ConnectionTester
8
 */
9
10
declare(strict_types=1);
11
12
namespace AppUtils;
13
14
/**
15
 * Used to test whether an URL exists / can be connected to.
16
 *
17
 * @package Application Utils
18
 * @subpackage URLInfo
19
 * @author Sebastian Mordziol <[email protected]>
20
 */
21
class URLInfo_ConnectionTester implements Interface_Optionable
22
{
23
    use Traits_Optionable;
24
    
25
   /**
26
    * @var URLInfo
27
    */
28
    private $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 an https connection.
47
    * 
48
    * @param bool $verifySSL
49
    * @return URLInfo_ConnectionTester
50
    */
51
    public function setVerifySSL(bool $verifySSL=true) : URLInfo_ConnectionTester
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) : URLInfo_ConnectionTester
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) : URLInfo_ConnectionTester#
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
        requireCURL();
93
        
94
        $ch = curl_init();
95
        
96
        if(!is_resource($ch))
97
        {
98
            throw new BaseException(
99
                'Could not initialize a new cURL instance.',
100
                'Calling curl_init returned false. Additional information is not available.',
101
                URLInfo::ERROR_CURL_INIT_FAILED
102
            );
103
        }
104
        
105
        return $ch;
106
    }
107
    
108
   /**
109
    * @param resource $ch
110
    */
111
    private function configureOptions($ch) : void
112
    {
113
        if($this->isVerboseModeEnabled())
114
        {
115
            curl_setopt($ch, CURLOPT_VERBOSE, true);
116
        }
117
        
118
        curl_setopt($ch, CURLOPT_URL, $this->url->getNormalized());
119
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
120
        curl_setopt($ch, CURLOPT_TIMEOUT, $this->getTimeout());
121
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
122
        
123
        if(!$this->isVerifySSLEnabled())
124
        {
125
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
126
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
127
        }
128
        
129
        if($this->url->hasUsername())
130
        {
131
            curl_setopt($ch, CURLOPT_USERNAME, $this->url->getUsername());
132
            curl_setopt($ch, CURLOPT_PASSWORD, $this->url->getPassword());
133
        }
134
    }
135
        
136
    public function canConnect() : bool
137
    {
138
        $ch = $this->initCURL();
139
        
140
        $this->configureOptions($ch);
141
        
142
        curl_exec($ch);
143
        
144
        $http_code = (int)curl_getinfo($ch, CURLINFO_HTTP_CODE);
145
        
146
        curl_close($ch);
147
        
148
        return ($http_code === 200) || ($http_code === 302);
149
    }
150
}
151