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

AbstractClient::close()

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 1
c 0
b 0
f 0
nc 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
    public function __construct(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['username'])) {
51
            $this->username = (string) $config['username'];
52
        }
53
54
        if (!empty($config['password'])) {
55
            $this->password = (string) $config['password'];
56
        }
57
58
        if (!empty($config['services']) && is_array($config['services'])) {
59
            $this->services = $config['services'];
60
61
            if (!empty($config['serviceExtensions']) && is_array($config['serviceExtensions'])) {
62
                $this->serviceExtensions = $config['serviceExtensions'];
63
            }
64
        }
65
66
        if ((!empty($config['ssl']) && is_bool($config['ssl']))) {
67
            $this->ssl = $config['ssl'];
68
        } else {
69
            $this->ssl = false;
70
        }
71
72
        if (!empty($config['local_cert'])) {
73
            $this->local_cert = (string) $config['local_cert'];
74
75
            if (!is_readable($this->local_cert)) {
76
                throw new \Exception(sprintf('unable to read local_cert: %s', $this->local_cert));
77
            }
78
        }
79
80
        if (!empty($config['ca_cert'])) {
81
            $this->ca_cert = (string) $config['ca_cert'];
82
83
            if (!is_readable($this->ca_cert)) {
84
                throw new \Exception(sprintf('unable to read ca_cert: %s', $this->ca_cert));
85
            }
86
        }
87
88
        if (!empty($config['pk_cert'])) {
89
            $this->pk_cert = (string) $config['pk_cert'];
90
91
            if (!is_readable($this->pk_cert)) {
92
                throw new \Exception(sprintf('unable to read pk_cert: %s', $this->pk_cert));
93
            }
94
        }
95
96
        if (!empty($config['passphrase'])) {
97
            $this->passphrase = (string) $config['passphrase'];
98
        }
99
100
        if (!empty($config['debug']) && is_bool($config['debug'])) {
101
            $this->debug = $config['debug'];
102
        } else {
103
            $this->debug = false;
104
        }
105
106
        if (!empty($config['connect_timeout'])) {
107
            $this->connect_timeout = (int) $config['connect_timeout'];
108
        } else {
109
            $this->connect_timeout = 16;
110
        }
111
112
        if (!empty($config['timeout'])) {
113
            $this->timeout = (int) $config['timeout'];
114
        } else {
115
            $this->timeout = 32;
116
        }
117
    }
118
119
    protected function generateClientTransactionId()
120
    {
121
        return Random::id(64, $this->username);
122
    }
123
124
    /**
125
     * Generate and send login frame
126
     *
127
     * @param bool|string $newPassword New password to set on longin, false if not changing pasword
128
     *
129
     * @throws \Exception On unsuccessful login
130
     *
131
     * @return \AfriCC\EPP\Frame\Response Login response
132
     */
133
    protected function login($newPassword = false)
134
    {
135
        // send login command
136
        $login = new LoginCommand();
137
        $login->setClientId($this->username);
138
        $login->setPassword($this->password);
139
        if ($newPassword) {
140
            $login->setNewPassword($newPassword);
141
        }
142
        $login->setVersion('1.0');
143
        $login->setLanguage('en');
144
145
        if (!empty($this->services) && is_array($this->services)) {
146
            foreach ($this->services as $urn) {
147
                $login->addService($urn);
148
            }
149
150
            if (!empty($this->serviceExtensions) && is_array($this->serviceExtensions)) {
151
                foreach ($this->serviceExtensions as $extension) {
152
                    $login->addServiceExtension($extension);
153
                }
154
            }
155
        }
156
157
        $response = $this->request($login);
158
        unset($login);
159
160
        // check if login was successful
161
        if (!($response instanceof ResponseFrame)) {
162
            throw new \Exception('there was a problem logging onto the EPP server');
163
        } elseif ($response->code() !== 1000) {
164
            throw new \Exception($response->message(), $response->code());
165
        }
166
167
        return $response;
168
    }
169
}
170