Completed
Pull Request — master (#300)
by De Cramer
04:22
created

Factory::getConnection()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 2
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 Maniaplanet\DedicatedServer\Connection;
12
use Maniaplanet\DedicatedServer\Xmlrpc\TransportException;
13
use Psr\Log\LoggerInterface;
14
15
/**
16
 * Service factory to create connection to the dedicated server.
17
 *
18
 * @package eXpansion\Framework\Core\Services\DedicatedConnection
19
 */
20
class Factory
21
{
22
    /** @var Connection */
23
    protected $connection;
24
25
    /** @var string The name/ip of the host */
26
    protected $host;
27
28
    /** @var int */
29
    protected $port;
30
31
    /** @var int */
32
    protected $timeout;
33
34
    /** @var string */
35
    protected $user;
36
37
    /** @var string */
38
    protected $password;
39
    /**
40
     * @var LoggerInterface
41
     */
42
    private $logger;
43
44
    /**
45
     * Factory constructor.
46
     *
47
     * @param string $host
48
     * @param int $port
49
     * @param int $timeout
50
     * @param string $user
51
     * @param string $password
52
     * @param LoggerInterface $logger
53
     */
54 164
    public function __construct($host, $port, $timeout, $user, $password, LoggerInterface $logger)
55
    {
56 164
        $this->host = $host;
57 164
        $this->port = $port;
58 164
        $this->timeout = $timeout;
59 164
        $this->user = $user;
60 164
        $this->password = $password;
61 164
        $this->logger = $logger;
62 164
    }
63
64
    /**
65
     * Connect to the dedicated server.
66
     *
67
     * @return Connection
68
     */
69
    public function createConnection()
70
    {
71
        if (!is_null($this->connection)) {
72
            try {
73
                $this->connection = Connection::factory(
74
                    $this->host,
75
                    $this->port,
76
                    $this->timeout,
77
                    $this->user,
78
                    $this->password
79
                );
80
            } catch (TransportException $ex) {
81
                echo "Looks like your Dedicated server is either offline or has wrong config settings.\n";
82
                echo "Error message: " . $ex->getMessage();
83
                $this->logger->error("Unable to open connection for Dedicated server", ["exception" => $ex]);
84
85
                throw $ex;
86
            }
87
        }
88
89
        return $this->connection;
90
    }
91
92
    /**
93
     * Get connection to the dedicated.
94
     *
95
     * @return Connection
96
     */
97
    public function getConnection()
98
    {
99
        return $this->connection;
100
    }
101
}
102