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

AbstractClient::__construct()   F

Complexity

Conditions 22
Paths 12960

Size

Total Lines 80

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 80
rs 0
c 0
b 0
f 0
nc 12960
cc 22
nop 1

How to fix   Long Method    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
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
    abstract public function connect($newPassword = false);
26
    
27
    abstract public function close();
28
    
29
    abstract public function request(FrameInterface $frame);
30
    
31
    abstract protected function log($message);
32
    
33
    public function __construct(array $config)
34
    {
35
        if (!empty($config['host'])) {
36
            $this->host = (string) $config['host'];
37
        }
38
        
39
        if (!empty($config['port'])) {
40
            $this->port = (int)$config['port'];
41
        } else {
42
            $this->port = false;
43
        }
44
        
45
        if (!empty($config['username'])) {
46
            $this->username = (string) $config['username'];
47
        }
48
        
49
        if (!empty($config['password'])) {
50
            $this->password = (string) $config['password'];
51
        }
52
        
53
        if (!empty($config['services']) && is_array($config['services'])) {
54
            $this->services = $config['services'];
55
            
56
            if (!empty($config['serviceExtensions']) && is_array($config['serviceExtensions'])) {
57
                $this->serviceExtensions = $config['serviceExtensions'];
58
            }
59
        }
60
        
61
        if ((!empty($config['ssl']) && is_bool($config['ssl']))) {
62
            $this->ssl = $config['ssl'];
63
        } else {
64
            $this->ssl = false;
65
        }
66
        
67
        if (!empty($config['local_cert'])) {
68
            $this->local_cert = (string) $config['local_cert'];
69
            
70
            if (!is_readable($this->local_cert)) {
71
                throw new \Exception(sprintf('unable to read local_cert: %s', $this->local_cert));
72
            }
73
        }
74
        
75
        if (!empty($config['ca_cert'])) {
76
            $this->ca_cert = (string) $config['ca_cert'];
77
            
78
            if (!is_readable($this->ca_cert)) {
79
                throw new \Exception(sprintf('unable to read ca_cert: %s', $this->ca_cert));
80
            }
81
        }
82
        
83
        if (!empty($config['pk_cert'])) {
84
            $this->pk_cert = (string) $config['pk_cert'];
85
            
86
            if (!is_readable($this->pk_cert)) {
87
                throw new \Exception(sprintf('unable to read pk_cert: %s', $this->pk_cert));
88
            }
89
        }
90
        
91
        if (!empty($config['passphrase'])) {
92
            $this->passphrase = (string) $config['passphrase'];
93
        }
94
        
95
        if (!empty($config['debug']) && is_bool($config['debug'])) {
96
            $this->debug = $config['debug'];
97
        } else {
98
            $this->debug = false;
99
        }
100
        
101
        if (!empty($config['connect_timeout'])) {
102
            $this->connect_timeout = (int) $config['connect_timeout'];
103
        } else {
104
            $this->connect_timeout = 16;
105
        }
106
        
107
        if (!empty($config['timeout'])) {
108
            $this->timeout = (int) $config['timeout'];
109
        } else {
110
            $this->timeout = 32;
111
        }
112
    }
113
    
114
    protected function generateClientTransactionId()
115
    {
116
        return Random::id(64, $this->username);
117
    }
118
    
119
    protected function login($newPassword = false)
120
    {
121
        // send login command
122
        $login = new LoginCommand();
123
        $login->setClientId($this->username);
124
        $login->setPassword($this->password);
125
        if ($newPassword){
126
            $login->setNewPassword($newPassword);
127
        }
128
        $login->setVersion('1.0');
129
        $login->setLanguage('en');
130
        
131
        if (!empty($this->services) && is_array($this->services)) {
132
            foreach ($this->services as $urn) {
133
                $login->addService($urn);
134
            }
135
            
136
            if (!empty($this->serviceExtensions) && is_array($this->serviceExtensions)) {
137
                foreach ($this->serviceExtensions as $extension) {
138
                    $login->addServiceExtension($extension);
139
                }
140
            }
141
        }
142
        
143
        $response = $this->request($login);
144
        unset($login);
145
        
146
        // check if login was successful
147
        if (!($response instanceof ResponseFrame)) {
148
            throw new \Exception('there was a problem logging onto the EPP server');
149
        } elseif ($response->code() !== 1000) {
150
            throw new \Exception($response->message(), $response->code());
151
        }
152
        
153
        return $response;
154
    }
155
}
156
157