CheckConnection::checkStandardConnection()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

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