ExtendedServer::__toString()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Ez\DbLinker;
4
5
6
use Doctrine\DBAL\Connection;
7
use Doctrine\DBAL\DriverManager;
8
9
abstract class ExtendedServer
10
{
11
    protected $host;
12
    protected $port;
13
    protected $user;
14
    protected $password;
15
    protected $dbname;
16
    protected $extraParams = [];
17
    protected $driver;
18
    protected $driverOptions;
19
20
    /**
21
     * @var Connection
22
     */
23
    protected $connection;
24
25
    public function __construct(array $params)
26
    {
27
        foreach ($params as $key => $value) {
28
            switch ($key) {
29
                case 'host':
30
                case 'port':
31
                case 'user':
32
                case 'password':
33
                case 'dbname':
34
                case 'weight':
35
                case 'driver':
36
                case 'driverOptions':
37
                    $this->$key = $value;
38
                break;
39
            default:
40
                throw new \Exception($key . ' = ' . $value);
41
                $extraParams[$key] = $value;
0 ignored issues
show
Unused Code introduced by
$extraParams[$key] = $value; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
42
            }
43
        }
44
    }
45
46
    public function dbalConfig(): array {
47
        return [
48
            'host' => $this->host,
49
            'port' => $this->port,
50
            'user' => $this->user,
51
            'password' => $this->password,
52
            'dbname' => $this->dbname,
53
            'driver' => $this->driver,
54
            'driverOptions' => $this->driverOptions,
55
        ];
56
    }
57
58
    public function isConnected(): bool {
59
        return $this->connection !== null;
60
    }
61
62
    public function __toString()
63
    {
64
        return get_class($this) . ' - ' . $this->user . '@' . $this->host . ':' . $this->port . '/' . $this->dbname;
65
    }
66
67
    public function close() {
68
        if ($this->isConnected()) {
69
//            echo \get_class($this) . ' ' . $this . PHP_EOL;
70
            $this->connection->close();
71
        }
72
    }
73
74
    /**
75
     * @return \Doctrine\DBAL\Connection
76
     * @throws \Doctrine\DBAL\DBALException
77
     */
78
    public function connection(): Connection {
79
        if (!$this->connection) {
80
            $this->connection = DriverManager::getConnection($this->dbalConfig());
81
        }
82
        return $this->connection;
83
    }
84
85
    public function __destruct()
86
    {
87
        $this->close();
88
    }
89
}