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

AbstractClient   A

Complexity

Total Complexity 33

Size/Duplication

Total Lines 162
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 33
lcom 1
cbo 3
dl 0
loc 162
rs 9.76
c 0
b 0
f 0

7 Methods

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