ExtendedServer   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 18
lcom 1
cbo 2
dl 0
loc 81
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 20 10
A dbalConfig() 0 11 1
A isConnected() 0 3 1
A __toString() 0 4 1
A close() 0 6 2
A connection() 0 6 2
A __destruct() 0 4 1
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
}