HTTPClient   A
last analyzed

Complexity

Total Complexity 24

Size/Duplication

Total Lines 163
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 3

Importance

Changes 0
Metric Value
wmc 24
lcom 2
cbo 3
dl 0
loc 163
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A log() 0 6 2
A active() 0 4 1
A __destruct() 0 4 1
A setupCurl() 0 15 3
A setupCurlOpts() 0 19 1
A setupCurlSSL() 0 18 5
A connect() 0 13 1
A close() 0 11 2
A __construct() 0 13 2
A prepareCookieJar() 0 13 3
A sendFrame() 0 5 1
A getFrame() 0 12 2
1
<?php
2
3
namespace AfriCC\EPP;
4
5
use AfriCC\EPP\Frame\Command\Logout as LogoutCommand;
6
7
/**
8
 * A high level HTTP(S) based client for the Extensible Provisioning Protocol (EPP)
9
 *
10
 * @see http://tools.ietf.org/html/rfc5734
11
 *
12
 * As this class deals directly with cURL it's untestable
13
 * @codeCoverageIgnore
14
 */
15
class HTTPClient extends AbstractClient implements ClientInterface
16
{
17
    protected $curl;
18
    protected $cookiejar;
19
20
    public function __construct(array $config, ObjectSpec $objectSpec = null)
21
    {
22
        parent::__construct($config, $objectSpec);
23
24
        $proto = \parse_url($this->host, PHP_URL_SCHEME);
25
        if ($proto == 'https') {
26
            $this->ssl = true;
27
        } else {
28
            $this->ssl = false;
29
        }
30
31
        $this->prepareCookieJar($config);
32
    }
33
34
    protected function prepareCookieJar(array $config)
35
    {
36
        $this->cookiejar = $this->getConfigDefault($config, 'cookiejar', tempnam(sys_get_temp_dir(), 'ehc'));
37
38
        if (!is_readable($this->cookiejar) || !is_writable($this->cookiejar)) {
39
            throw new \Exception(
40
                sprintf(
41
                    'unable to read/write cookiejar: %s',
42
                    $this->cookiejar
43
                    )
44
                );
45
        }
46
    }
47
48
    public function __destruct()
49
    {
50
        $this->close();
51
    }
52
53
    private function setupCurl()
54
    {
55
        $this->curl = curl_init($this->host);
56
57
        if ($this->curl === false) {
58
            throw new \Exception('Cannot initialize cURL extension');
59
        }
60
61
        $this->setupCurlOpts();
62
63
        // certs
64
        if ($this->ssl) {
65
            $this->setupCurlSSL();
66
        }
67
    }
68
69
    private function setupCurlOpts()
70
    {
71
        // set stream time out
72
        curl_setopt($this->curl, CURLOPT_TIMEOUT, $this->timeout);
73
        curl_setopt(
74
            $this->curl,
75
            CURLOPT_CONNECTTIMEOUT,
76
            $this->connect_timeout
77
            );
78
79
        // set necessary options
80
        curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, true);
81
        curl_setopt($this->curl, CURLOPT_FOLLOWLOCATION, true);
82
        curl_setopt($this->curl, CURLOPT_HEADER, false);
83
84
        // cookies
85
        curl_setopt($this->curl, CURLOPT_COOKIEFILE, $this->cookiejar);
86
        curl_setopt($this->curl, CURLOPT_COOKIEJAR, $this->cookiejar);
87
    }
88
89
    private function setupCurlSSL()
90
    {
91
        curl_setopt($this->curl, CURLOPT_SSL_VERIFYPEER, true);
92
        curl_setopt($this->curl, CURLOPT_SSLKEYTYPE, 'PEM');
93
94
        if ($this->ca_cert) {
95
            curl_setopt($this->curl, CURLOPT_CAINFO, $this->ca_cert);
96
        }
97
        if ($this->pk_cert) {
98
            curl_setopt($this->curl, CURLOPT_SSLKEY, $this->pk_cert);
99
        }
100
        if ($this->local_cert) {
101
            curl_setopt($this->curl, CURLOPT_SSLCERT, $this->local_cert);
102
        }
103
        if ($this->passphrase) {
104
            curl_setopt($this->curl, CURLOPT_SSLCERTPASSWD, $this->passphrase);
105
        }
106
    }
107
108
    /**
109
     * Open a new connection to the EPP server
110
     *
111
     * @param bool|string $newPassword String with new password to set upon login, false if no password
112
     */
113
    public function connect($newPassword = false)
114
    {
115
        $this->setupCurl();
116
117
        // get greeting
118
        $greeting = $this->request(new \AfriCC\EPP\Frame\Hello($this->objectSpec));
119
120
        // login
121
        $this->login($newPassword);
122
123
        // return greeting
124
        return $greeting;
125
    }
126
127
    /**
128
     * Closes a previously opened EPP connection
129
     */
130
    public function close()
131
    {
132
        if ($this->active()) {
133
            // send logout frame
134
            $this->request(new LogoutCommand($this->objectSpec));
135
136
            return curl_close($this->curl);
137
        }
138
139
        return false;
140
    }
141
142
    public function sendFrame(FrameInterface $frame)
143
    {
144
        $content = (string) $frame;
145
        curl_setopt($this->curl, CURLOPT_POSTFIELDS, $content);
146
    }
147
148
    public function getFrame()
149
    {
150
        $return = curl_exec($this->curl);
151
152
        if ($return === false) {
153
            $code = curl_errno($this->curl);
154
            $msg = curl_error($this->curl);
155
            throw new \Exception($msg, $code);
156
        }
157
158
        return $return;
159
    }
160
161
    protected function log($message)
162
    {
163
        if ($this->debug) {
164
            \error_log($message);
165
        }
166
    }
167
168
    /**
169
     * Check if curl session is still active
170
     *
171
     * @return bool
172
     */
173
    private function active()
174
    {
175
        return is_resource($this->curl);
176
    }
177
}
178