Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 30
0.00% covered (danger)
0.00%
0 / 11
CRAP
0.00% covered (danger)
0.00%
0 / 1
LBFactorySingle
0.00% covered (danger)
0.00%
0 / 30
0.00% covered (danger)
0.00%
0 / 11
210
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
12
 newFromConnection
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
 newDisabled
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 newMainLB
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getMainLB
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 newExternalLB
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getExternalLB
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getAllMainLBs
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getAllExternalLBs
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getLBsForOwner
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
6
 __destruct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 */
20namespace Wikimedia\Rdbms;
21
22use BadMethodCallException;
23use InvalidArgumentException;
24
25/**
26 * LoadBalancer manager for sites with one "main" cluster using only injected database connections
27 *
28 * This class assumes that there are no "external" clusters.
29 *
30 * LoadBalancerDisabled will be used if a null connection handle is injected.
31 *
32 * @see ILBFactory
33 * @ingroup Database
34 */
35class LBFactorySingle extends LBFactory {
36    /** @var LoadBalancerSingle|LoadBalancerDisabled */
37    private $mainLB;
38
39    /**
40     * @note Use of {@link newFromConnection} is preferable
41     *
42     * @param array $conf An associative array containing one of the following:
43     *  - connection: The IDatabase connection handle to use; null to disable access
44     */
45    public function __construct( array $conf ) {
46        parent::__construct( $conf );
47
48        if ( !array_key_exists( 'connection', $conf ) ) {
49            throw new InvalidArgumentException( "Missing 'connection' argument." );
50        }
51
52        $conn = $conf['connection'];
53        if ( $conn ) {
54            $mainLB = new LoadBalancerSingle( array_merge(
55                $this->baseLoadBalancerParams(),
56                [ 'connection' => $conn ]
57            ) );
58        } else {
59            $mainLB = new LoadBalancerDisabled( $this->baseLoadBalancerParams() );
60        }
61        $this->initLoadBalancer( $mainLB );
62
63        $this->mainLB = $mainLB;
64    }
65
66    /**
67     * @param IDatabase $db Live connection handle
68     * @param array $params Parameter map to LBFactorySingle::__construct()
69     * @return LBFactorySingle
70     * @since 1.28
71     */
72    public static function newFromConnection( IDatabase $db, array $params = [] ) {
73        return new static( array_merge(
74            [ 'localDomain' => $db->getDomainID() ],
75            $params,
76            [ 'connection' => $db ]
77        ) );
78    }
79
80    /**
81     * @param array $params Parameter map to LBFactorySingle::__construct()
82     * @return LBFactorySingle
83     * @since 1.40
84     */
85    public static function newDisabled( array $params = [] ) {
86        return new static( array_merge(
87            $params,
88            [ 'connection' => null ]
89        ) );
90    }
91
92    public function newMainLB( $domain = false ): ILoadBalancerForOwner {
93        // @phan-suppress-previous-line PhanPluginNeverReturnMethod
94        throw new BadMethodCallException( "Method is not supported." );
95    }
96
97    public function getMainLB( $domain = false ): ILoadBalancer {
98        return $this->mainLB;
99    }
100
101    public function newExternalLB( $cluster ): ILoadBalancerForOwner {
102        // @phan-suppress-previous-line PhanPluginNeverReturnMethod
103        throw new BadMethodCallException( "Method is not supported." );
104    }
105
106    public function getExternalLB( $cluster ): ILoadBalancer {
107        // @phan-suppress-previous-line PhanPluginNeverReturnMethod
108        throw new BadMethodCallException( "Method is not supported." );
109    }
110
111    public function getAllMainLBs(): array {
112        return [ self::CLUSTER_MAIN_DEFAULT => $this->mainLB ];
113    }
114
115    public function getAllExternalLBs(): array {
116        return [];
117    }
118
119    protected function getLBsForOwner() {
120        if ( $this->mainLB !== null ) {
121            yield $this->mainLB;
122        }
123    }
124
125    public function __destruct() {
126        // do nothing since the connection was injected
127    }
128}