Completed
Push — master ( d0db13...08221d )
by De Cramer
10s
created

Factory::createConnection()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 21
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 2
Bugs 0 Features 1
Metric Value
dl 0
loc 21
ccs 0
cts 10
cp 0
rs 9.3142
c 2
b 0
f 1
cc 3
eloc 10
nc 3
nop 1
crap 12
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
16
/**
17
 * Service factory to create connection to the dedicated server.
18
 *
19
 * @package eXpansion\Framework\Core\Services\DedicatedConnection
20
 */
21
class Factory
22
{
23
    /** @var Connection */
24
    protected $connection;
25
26
    /** @var string The name/ip of the host */
27
    protected $host;
28
29
    /** @var int */
30
    protected $port;
31
32
    /** @var int */
33
    protected $timeout;
34
35
    /** @var string */
36
    protected $user;
37
38
    /** @var string */
39
    protected $password;
40
41
    /** @var LoggerInterface */
42
    protected $logger;
43
44
    /** @var Console */
45
    protected $console;
46
47
    /**
48
     * Factory constructor.
49
     *
50
     * @param string $host
51
     * @param int $port
52
     * @param int $timeout
53
     * @param string $user
54
     * @param string $password
55
     * @param LoggerInterface $logger
56
     */
57 164
    public function __construct(
58
        $host,
59
        $port,
60
        $timeout,
61
        $user,
62
        $password,
63
        LoggerInterface $logger,
64
        Console $console
65
    ) {
66 164
        $this->host = $host;
67 164
        $this->port = $port;
68 164
        $this->timeout = $timeout;
69 164
        $this->user = $user;
70 164
        $this->password = $password;
71 164
        $this->logger = $logger;
72 164
        $this->console = $console;
73 164
    }
74
75
    /**
76
     * Attempt to connect to the dedicated server.
77
     *
78
     * @param int $maxAttempts
79
     *
80
     * @return Connection
81
     * @throws TransportException when connection fails.
82
     */
83
    public function createConnection($maxAttempts = 3)
84
    {
85
86
        if (is_null($this->connection)) {
87
            $lastExcelption = $this->attemptConnection($maxAttempts);
88
89
            if (!is_null($lastExcelption)) {
90
                $this->console->getSfStyleOutput()->error(
91
                    [
92
                        "Looks like your Dedicated server is either offline or has wrong config settings",
93
                        "Error message: " . $lastExcelption->getMessage()
94
                    ]
95
                );
96
                $this->logger->error("Unable to open connection for Dedicated server", ["exception" => $lastExcelption]);
97
98
                throw $lastExcelption;
99
            }
100
        }
101
102
        return $this->connection;
103
    }
104
105
    /**
106
     * @param $maxAttempts
107
     *
108
     * @return \Exception|TransportException|null
109
     */
110
    protected function attemptConnection($maxAttempts)
111
    {
112
        $attempts = 0;
113
        $lastExcelption = null;
114
115
        do {
116
117
            if (!is_null($lastExcelption)) {
118
                // Not first error.
119
                $lastExcelption = null;
120
121
                $this->console->getSfStyleOutput()->block(
122
                    "Will attempt to re-connect to dedicated server in 30seconds"
123
                );
124
                sleep(30);
125
            }
126
127
            try {
128
                $this->console->writeln('Attempting to connect to the dedicated server!');
129
130
                $this->connection = Connection::factory(
131
                    $this->host,
132
                    $this->port,
133
                    $this->timeout,
134
                    $this->user,
135
                    $this->password
136
                );
137
138
            } catch (\Exception $e) {
139
                $lastExcelption = $e;
140
                $attempts++;
141
                $remainingAttemps = $maxAttempts - $attempts;
142
143
                $this->console->getSfStyleOutput()->error(
144
                    [
145
                        "Cound't connect to the dedicated server !",
146
                        "Attempt : $attempts, Remaining attemps : $remainingAttemps ",
147
                        $e->getMessage(),
148
                    ]
149
                );
150
            }
151
        } while($attempts < $maxAttempts && !is_null($lastExcelption));
152
153
        return $lastExcelption;
154
    }
155
156
    /**
157
     * Get connection to the dedicated.
158
     *
159
     * @return Connection
160
     */
161
    public function getConnection()
162
    {
163
        return $this->connection;
164
    }
165
}
166