Completed
Push — master ( 7a6bdd...324ce1 )
by Sébastien
09:34
created

MasterSlaveConnection   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 125
Duplicated Lines 0 %

Test Coverage

Coverage 85.19%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 12
eloc 22
c 1
b 0
f 0
dl 0
loc 125
ccs 23
cts 27
cp 0.8519
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A force() 0 5 1
A executeQuery() 0 9 3
A getConnection() 0 12 3
A close() 0 5 1
A quote() 0 3 1
A getReadConnection() 0 3 1
A __construct() 0 9 2
1
<?php
2
3
namespace Bdf\Prime\Connection;
4
5
use Bdf\Prime\ConnectionManager;
0 ignored issues
show
introduced by
Unused use statement
Loading history...
6
use Doctrine\Common\EventManager;
7
use Doctrine\DBAL\Cache\QueryCacheProfile;
8
use Doctrine\DBAL\Configuration;
9
use Doctrine\DBAL\Driver;
10
use LogicException;
11
12
/**
13
 * MasterSlaveConnection
14
 *
15
 * The master / slave connection is a connection to a master server with a connection wrapper to a slave server.
16
 * Only method SimpleConnection#executeQuery will be redirect to the salve.
17
 *
18
 * SimpleConnection#quote also use slave.
19
 *
20
 * Becareful those methods are used on master:
21
 *
22
 *   SimpleConnection#prepare
23
 *   SimpleConnection#query
24
 *
25
 * @package Bdf\Prime\Connection
0 ignored issues
show
Coding Style Documentation introduced by
@package tag is not allowed in class comment
Loading history...
26
 */
27
class MasterSlaveConnection extends SimpleConnection implements SubConnectionManagerInterface
28
{
29
    /**
30
     * The connection specifically for read operations
31
     * 
32
     * This connection is used only for the method SimpleConnection#executeQuery
0 ignored issues
show
introduced by
Doc comment long description must end with a full stop
Loading history...
33
     * 
34
     * @var SimpleConnection
35
     */
36
    private $readConnection;
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line(s) before first member var; 0 found
Loading history...
37
38
    /**
39
     * Force the read on master
40
     *
41
     * @var boolean
0 ignored issues
show
introduced by
Expected "bool" but found "boolean" for @var tag in member variable comment
Loading history...
42
     */
43
    private $force = false;
44
45
    /**
46
     * Initializes a new instance of the Connection class.
47
     * 
48
     * Here's a read connection configuration
0 ignored issues
show
introduced by
Doc comment long description must end with a full stop
Loading history...
49
     * 
50
     * @example
51
     *
52
     * $conn = DriverManager::getConnection([
53
     *    'driver' => 'pdo_mysql',
54
     *    'user'     => '',
55
     *    'password' => '',
56
     *    'host'     => '',
57
     *    'dbname'   => '',
58
     *    'read' => [
59
     *        'user'     => 'slave',
60
     *        'password' => '',
61
     *        'host'     => '',
62
     *        'dbname'   => '',
63
     *    ]
64
     * ]);
65
     *
66
     * @param array                              $params       The connection parameters.
0 ignored issues
show
Coding Style introduced by
Parameter tags must be defined first in a doc comment
Loading history...
67
     * @param \Doctrine\DBAL\Driver              $driver       The driver to use.
68
     * @param \Doctrine\DBAL\Configuration|null  $config       The configuration, optional.
69
     * @param \Doctrine\Common\EventManager|null $eventManager The event manager, optional.
70
     */
0 ignored issues
show
Coding Style Documentation introduced by
Missing @throws tag in function comment
Loading history...
71 8
    public function __construct(array $params, Driver $driver, Configuration $config = null, EventManager $eventManager = null)
72
    {
73 8
        if (!isset($params['read'])) {
74
            throw new LogicException('Master/slave connection needs readable connection in parameters');
75
        }
76
77 8
        $this->readConnection = $params['read'];
78
79 8
        parent::__construct($params, $driver, $config, $eventManager);
80 8
    }
81
82
    /**
83
     * Get the read connection
84
     * 
85
     * @return SimpleConnection
86
     */
87 8
    public function getReadConnection()
88
    {
89 8
        return $this->readConnection;
90
    }
91
92
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $name should have a doc-comment as per coding-style.
Loading history...
93
     * {@inheritdoc}
94
     */
0 ignored issues
show
Coding Style Documentation introduced by
Missing @throws tag in function comment
Loading history...
95 3
    public function getConnection($name)
96
    {
97 3
        if ($name === 'read') {
98 2
            return $this->readConnection;
99
        }
100
101
        // Force the read on master if it is the awaiting connection
102 3
        if ($name === 'master') {
103 2
            return $this->force();
104
        }
105
106 1
        throw new LogicException('The sub connection "'.$name.'" is unknown in the master / slave connection');
107
    }
108
109
    /**
110
     * Force next read on master connection once
111
     * This flag will change after the execution of method executeQuery
0 ignored issues
show
introduced by
Doc comment short description must be on a single line, further text should be a separate paragraph
Loading history...
112
     *
113
     * @return $this
114
     */
115 3
    public function force()
116
    {
117 3
        $this->force = true;
118
119 3
        return $this;
120
    }
121
122
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $query should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $params should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $types should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $qcp should have a doc-comment as per coding-style.
Loading history...
123
     * {@inheritdoc}
124
     */
125 8
    public function executeQuery($query, array $params = [], $types = [], QueryCacheProfile $qcp = null)
126
    {
127 8
        if ($this->getTransactionNestingLevel() <= 0 && $this->force !== true) {
128 8
            return $this->readConnection->executeQuery($query, $params, $types, $qcp);
129
        }
130
131 1
        $this->force = false;
132
133 1
        return parent::executeQuery($query, $params, $types, $qcp);
134
    }
135
136
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $input should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $type should have a doc-comment as per coding-style.
Loading history...
137
     * {@inheritdoc}
138
     */
139 1
    public function quote($input, $type = null)
140
    {
141 1
        return $this->readConnection->quote($input, $type);
142
    }
143
144
    /**
145
     * {@inheritdoc}
146
     */
147
    public function close()
148
    {
149
        parent::close();
150
151
        $this->readConnection->close();
152
    }
153
}
154