Completed
Pull Request — master (#64)
by
unknown
01:32
created

AbstractClient::prepareEPPServices()   A

Complexity

Conditions 5
Paths 3

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

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