Completed
Push — master ( 701151...0cad11 )
by Günter
01:43
created

AbstractClient::prepareSSLOptions()   B

Complexity

Conditions 10
Paths 46

Size

Total Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 36
rs 7.6666
c 0
b 0
f 0
cc 10
nc 46
nop 1

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace AfriCC\EPP;
4
5
use AfriCC\EPP\Frame\Command\Login as LoginCommand;
6
use AfriCC\EPP\Frame\Response as ResponseFrame;
7
8
/**
9
 * An abstract client client for the Extensible Provisioning Protocol (EPP)
10
 *
11
 * @see http://tools.ietf.org/html/rfc5734
12
 *
13
 * As this class is absctract and relies on subclass implementation details it's untestable
14
 * @codeCoverageIgnore
15
 */
16
abstract class AbstractClient implements ClientInterface
17
{
18
    protected $host;
19
    protected $port;
20
    protected $username;
21
    protected $password;
22
    protected $services;
23
    protected $serviceExtensions;
24
    protected $ssl;
25
    protected $local_cert;
26
    protected $ca_cert;
27
    protected $pk_cert;
28
    protected $passphrase;
29
    protected $debug;
30
    protected $connect_timeout;
31
    protected $timeout;
32
33
    /**
34
     * {@inheritdoc}
35
     *
36
     * @see \AfriCC\EPP\ClientInterface::connect()
37
     */
38
    abstract public function connect($newPassword = false);
39
40
    abstract public function close();
41
42
    abstract public function request(FrameInterface $frame);
43
44
    abstract protected function log($message);
45
46
    protected function prepareConnectionOptions(array $config)
47
    {
48
        if (!empty($config['host'])) {
49
            $this->host = (string) $config['host'];
50
        }
51
52
        if (!empty($config['port'])) {
53
            $this->port = (int) $config['port'];
54
        } else {
55
            $this->port = false;
56
        }
57
58
        if (!empty($config['connect_timeout'])) {
59
            $this->connect_timeout = (int) $config['connect_timeout'];
60
        } else {
61
            $this->connect_timeout = 16;
62
        }
63
64
        if (!empty($config['timeout'])) {
65
            $this->timeout = (int) $config['timeout'];
66
        } else {
67
            $this->timeout = 32;
68
        }
69
    }
70
71
    protected function prepareCredentials(array $config)
72
    {
73
        if (!empty($config['username'])) {
74
            $this->username = (string) $config['username'];
75
        }
76
77
        if (!empty($config['password'])) {
78
            $this->password = (string) $config['password'];
79
        }
80
    }
81
82
    protected function prepareSSLOptions(array $config)
83
    {
84
        if ((!empty($config['ssl']) && is_bool($config['ssl']))) {
85
            $this->ssl = $config['ssl'];
86
        } else {
87
            $this->ssl = false;
88
        }
89
90
        if (!empty($config['local_cert'])) {
91
            $this->local_cert = (string) $config['local_cert'];
92
93
            if (!is_readable($this->local_cert)) {
94
                throw new \Exception(sprintf('unable to read local_cert: %s', $this->local_cert));
95
            }
96
        }
97
98
        if (!empty($config['ca_cert'])) {
99
            $this->ca_cert = (string) $config['ca_cert'];
100
101
            if (!is_readable($this->ca_cert)) {
102
                throw new \Exception(sprintf('unable to read ca_cert: %s', $this->ca_cert));
103
            }
104
        }
105
106
        if (!empty($config['pk_cert'])) {
107
            $this->pk_cert = (string) $config['pk_cert'];
108
109
            if (!is_readable($this->pk_cert)) {
110
                throw new \Exception(sprintf('unable to read pk_cert: %s', $this->pk_cert));
111
            }
112
        }
113
114
        if (!empty($config['passphrase'])) {
115
            $this->passphrase = (string) $config['passphrase'];
116
        }
117
    }
118
119
    protected function prepareEPPServices(array $config)
120
    {
121
        if (!empty($config['services']) && is_array($config['services'])) {
122
            $this->services = $config['services'];
123
124
            if (!empty($config['serviceExtensions']) && is_array($config['serviceExtensions'])) {
125
                $this->serviceExtensions = $config['serviceExtensions'];
126
            }
127
        }
128
    }
129
130
    public function __construct(array $config)
131
    {
132
        if (!empty($config['debug']) && is_bool($config['debug'])) {
133
            $this->debug = $config['debug'];
134
        } else {
135
            $this->debug = false;
136
        }
137
138
        $this->prepareConnectionOptions($config);
139
        $this->prepareCredentials($config);
140
        $this->prepareSSLOptions($config);
141
        $this->prepareEPPServices($config);
142
    }
143
144
    protected function generateClientTransactionId()
145
    {
146
        return Random::id(64, $this->username);
147
    }
148
149
    /**
150
     * Generate and send login frame
151
     *
152
     * @param bool|string $newPassword New password to set on longin, false if not changing pasword
153
     *
154
     * @throws \Exception On unsuccessful login
155
     *
156
     * @return \AfriCC\EPP\Frame\Response Login response
157
     */
158
    protected function login($newPassword = false)
159
    {
160
        // send login command
161
        $login = new LoginCommand();
162
        $login->setClientId($this->username);
163
        $login->setPassword($this->password);
164
        if ($newPassword) {
165
            $login->setNewPassword($newPassword);
166
        }
167
        $login->setVersion('1.0');
168
        $login->setLanguage('en');
169
170
        if (!empty($this->services) && is_array($this->services)) {
171
            foreach ($this->services as $urn) {
172
                $login->addService($urn);
173
            }
174
175
            if (!empty($this->serviceExtensions) && is_array($this->serviceExtensions)) {
176
                foreach ($this->serviceExtensions as $extension) {
177
                    $login->addServiceExtension($extension);
178
                }
179
            }
180
        }
181
182
        $response = $this->request($login);
183
        unset($login);
184
185
        // check if login was successful
186
        if (!($response instanceof ResponseFrame)) {
187
            throw new \Exception('there was a problem logging onto the EPP server');
188
        } elseif ($response->code() !== 1000) {
189
            throw new \Exception($response->message(), $response->code());
190
        }
191
192
        return $response;
193
    }
194
}
195