Factory::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 19
ccs 10
cts 10
cp 1
rs 9.6333
c 0
b 0
f 0
cc 1
nc 1
nop 8
crap 1

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: olive
5
 * Date: 12/03/2017
6
 * Time: 15:57
7
 */
8
9
namespace eXpansion\Framework\Core\Services\DedicatedConnection;
10
11
use eXpansion\Framework\Core\Services\Console;
12
use Maniaplanet\DedicatedServer\Connection;
13
use Maniaplanet\DedicatedServer\Xmlrpc\TransportException;
14
use Psr\Log\LoggerInterface;
15
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
16
use Symfony\Component\EventDispatcher\GenericEvent;
17
18
/**
19
 * Service factory to create connection to the dedicated server.
20
 *
21
 * @package eXpansion\Framework\Core\Services\DedicatedConnection
22
 */
23
class Factory
24
{
25
    const EVENT_CONNECTED = 'expansion.dedicated.connected';
26
27
    /** @var Connection */
28
    protected $connection;
29
30
    /** @var string The name/ip of the host */
31
    protected $host;
32
33
    /** @var int */
34
    protected $port;
35
36
    /** @var int */
37
    protected $timeout;
38
39
    /** @var string */
40
    protected $user;
41
42
    /** @var string */
43
    protected $password;
44
45
    /** @var LoggerInterface */
46
    protected $logger;
47
48
    /** @var Console */
49
    protected $console;
50
51
    /** @var EventDispatcherInterface */
52
    protected $eventDispatcher;
53
54
    /**
55
     * Factory constructor.
56
     *
57
     * @param                          $host
58
     * @param                          $port
59
     * @param                          $timeout
60
     * @param                          $user
61
     * @param                          $password
62
     * @param LoggerInterface          $logger
63
     * @param Console                  $console
64
     * @param EventDispatcherInterface $eventDispatcher
65
     */
66 16
    public function __construct(
67
        $host,
68
        $port,
69
        $timeout,
70
        $user,
71
        $password,
72
        LoggerInterface $logger,
73
        Console $console,
74
        EventDispatcherInterface $eventDispatcher
75
    ) {
76 16
        $this->host = $host;
77 16
        $this->port = $port;
78 16
        $this->timeout = $timeout;
79 16
        $this->user = $user;
80 16
        $this->password = $password;
81 16
        $this->logger = $logger;
82 16
        $this->console = $console;
83 16
        $this->eventDispatcher = $eventDispatcher;
84 16
    }
85
86
    /**
87
     * Attempt to connect to the dedicated server.
88
     *
89
     * @param int $maxAttempts
90
     *
91
     * @return Connection
92
     * @throws TransportException when connection fails.
93
     */
94
    public function createConnection($maxAttempts = 3)
95
    {
96
97
        if (is_null($this->connection)) {
98
            $lastExcelption = $this->attemptConnection($maxAttempts);
99
100
            if (!is_null($lastExcelption)) {
101
                $this->console->getSfStyleOutput()->error(
102
                    [
103
                        "Looks like your Dedicated server is either offline or has wrong config settings",
104
                        "Error message: " . $lastExcelption->getMessage()
105
                    ]
106
                );
107
                $this->logger->error(
108
                    "Unable to open connection for Dedicated server",
109
                    [
110
                        "exception" => $lastExcelption,
111
                        "host" => $this->host,
112
                        "port" => $this->port,
113
                        "timeout" => $this->timeout,
114
                        "user" => $this->user,
115
                    ]
116
                );
117
118
                throw $lastExcelption;
119
            }
120
121
            // Dispatch connected event.
122
            $event = new GenericEvent($this->connection);
123
            $this->eventDispatcher->dispatch(self::EVENT_CONNECTED, $event);
124
        }
125
126
        return $this->connection;
127
    }
128
129
    /**
130
     * @param $maxAttempts
131
     *
132
     * @return \Exception|TransportException|null
133
     */
134
    protected function attemptConnection($maxAttempts)
135
    {
136
        $attempts = 0;
137
        $lastExcelption = null;
138
139
        do {
140
141
            if (!is_null($lastExcelption)) {
142
                // Not first error.
143
                $lastExcelption = null;
144
145
                $this->console->getSfStyleOutput()->block(
146
                    "Will attempt to re-connect to dedicated server in 30seconds"
147
                );
148
                sleep(30);
149
            }
150
151
            try {
152
                $this->console->writeln('Attempting to connect to the dedicated server!');
153
                $this->connection = Connection::factory(
154
                    $this->host,
155
                    $this->port,
156
                    $this->timeout,
157
                    $this->user,
158
                    $this->password
159
                );
160
161
            } catch (\Exception $e) {
162
                $lastExcelption = $e;
163
                $attempts++;
164
                $remainingAttemps = $maxAttempts - $attempts;
165
166
                $this->console->getSfStyleOutput()->error(
167
                    [
168
                        "Couldn't connect to the dedicated server !",
169
                        "Attempt : $attempts, Remaining attempts : $remainingAttemps ",
170
                        $e->getMessage(),
171
                    ]
172
                );
173
                $this->console->getSfStyleOutput()->block([
174
                    "Host : " . $this->host,
175
                    "Port : " . $this->port,
176
                    "Timeout : " . $this->timeout,
177
                    "User : " . $this->user,
178
                    "Password : ****",
179
                ]);
180
            }
181
        } while($attempts < $maxAttempts && !is_null($lastExcelption));
182
183
        return $lastExcelption;
184
    }
185
186
    /**
187
     * Get connection to the dedicated.
188
     *
189
     * @return Connection
190
     */
191
    public function getConnection()
192
    {
193
        return $this->connection;
194
    }
195
}
196