Completed
Pull Request — master (#1)
by Timothy
04:56
created

CheckConnection::checkMasterSlaveConnection()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 21
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 21
rs 8.7624
cc 5
eloc 11
nc 8
nop 1
1
<?php
2
3
namespace Abacaphiliac\DoctrineORMDiagnosticsModule;
4
5
use Doctrine\DBAL\Connection;
6
use Doctrine\DBAL\Connections\MasterSlaveConnection;
7
use Doctrine\DBAL\Sharding\PoolingShardConnection;
8
use ZendDiagnostics\Check\AbstractCheck;
9
use ZendDiagnostics\Result\Failure;
10
use ZendDiagnostics\Result\ResultInterface;
11
use ZendDiagnostics\Result\Success;
12
13
class CheckConnection extends AbstractCheck
14
{
15
    /** @var Connection */
16
    private $connection;
17
18
    /**
19
     * CheckOrmDefaultEntityManager constructor.
20
     * @param Connection $connection
21
     */
22
    public function __construct(Connection $connection)
23
    {
24
        $this->connection = $connection;
25
    }
26
    
27
    /**
28
     * @return ResultInterface
29
     * @throws \Zend\ServiceManager\Exception\ServiceNotFoundException
30
     */
31
    public function check()
32
    {
33
        if ($this->connection instanceof MasterSlaveConnection) {
34
            return $this->checkMasterSlaveConnection($this->connection);
35
        }
36
        
37
        if ($this->connection instanceof PoolingShardConnection) {
38
            return $this->checkPoolingShardConnection($this->connection);
39
        }
40
        
41
        return $this->checkStandardConnection($this->connection);
42
    }
43
44
    /**
45
     * @param Connection $connection
46
     * @return Success|Failure
47
     */
48
    private function checkStandardConnection(Connection $connection)
49
    {
50
        if ($connection->ping()) {
51
            return new Success(get_class($connection));
52
        }
53
54
        return new Failure(get_class($connection));
55
    }
56
57
    /**
58
     * @param MasterSlaveConnection $connection
59
     * @return Success|Failure
60
     */
61
    private function checkMasterSlaveConnection(MasterSlaveConnection $connection)
62
    {
63
        // TODO Check all slaves, instead of random one if possible.
64
        
65
        $connection->connect('slave');
66
        $isSlaveConnected = $connection->ping();
67
68
        $connection->connect('master');
69
        $isMasterConnected = $connection->ping();
70
71
        $data = [
72
            'slave' => $isSlaveConnected ? 'connected' : 'not connected',
73
            'master' => $isMasterConnected ? 'connected' : 'not connected',
74
        ];
75
76
        if ($isMasterConnected && $isSlaveConnected) {
77
            return new Success(get_class($connection), $data);
78
        }
79
80
        return new Failure(get_class($connection), $data);
81
    }
82
83
    /**
84
     * @param PoolingShardConnection $connection
85
     * @return Success|Failure
86
     */
87
    private function checkPoolingShardConnection(PoolingShardConnection $connection)
88
    {
89
        // TODO Check all shards, instead of just the active one.
90
        
91
        $isConnected = $connection->ping();
92
        
93
        $data = [
94
            $connection->getActiveShardId() => $isConnected ? 'connected' : 'not connected'
95
        ];
96
97
        if ($isConnected) {
98
            return new Success(get_class($connection), $data);
99
        }
100
101
        return new Failure(get_class($connection), $data);
102
    }
103
}
104