Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 29
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 / 29
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 / 11
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 * Manage a single hardcoded database connection.
27 *
28 * @ingroup Database
29 */
30class LBFactorySingle extends LBFactory {
31    /** @var LoadBalancerSingle */
32    private $lb;
33
34    /**
35     * You probably want to use {@link newFromConnection} instead.
36     *
37     * @param array $conf An associative array containing one of the following:
38     *  - connection: The IDatabase connection object to use
39     *  - lb: The ILoadBalancer object to use
40     */
41    public function __construct( array $conf ) {
42        parent::__construct( $conf );
43
44        if ( isset( $conf['lb'] ) ) {
45            $lb = $conf['lb'];
46        } else {
47            if ( !isset( $conf['connection'] ) ) {
48                throw new InvalidArgumentException( "Missing 'connection' argument." );
49            }
50
51            $lb = new LoadBalancerSingle( array_merge(
52                $this->baseLoadBalancerParams(),
53                $conf
54            ) );
55        }
56
57        $this->initLoadBalancer( $lb );
58
59        $this->lb = $lb;
60    }
61
62    /**
63     * @param IDatabase $db Live connection handle
64     * @param array $params Parameter map to LBFactorySingle::__constructs()
65     * @return LBFactorySingle
66     * @since 1.28
67     */
68    public static function newFromConnection( IDatabase $db, array $params = [] ) {
69        return new static( array_merge(
70            [ 'localDomain' => $db->getDomainID() ],
71            $params,
72            [ 'connection' => $db ]
73        ) );
74    }
75
76    /**
77     * @param array $params Parameter map to LBFactorySingle::__construct()
78     *        and LoadBalancerDisabled::__construct()
79     * @return LBFactorySingle
80     * @since 1.40
81     */
82    public static function newDisabled( array $params = [] ) {
83        return new static( array_merge(
84            [ 'lb' => new LoadBalancerDisabled( $params ) ],
85            $params
86        ) );
87    }
88
89    public function newMainLB( $domain = false ): ILoadBalancerForOwner {
90        // @phan-suppress-previous-line PhanPluginNeverReturnMethod
91        throw new BadMethodCallException( "Method is not supported." );
92    }
93
94    public function getMainLB( $domain = false ): ILoadBalancer {
95        return $this->lb;
96    }
97
98    public function newExternalLB( $cluster ): ILoadBalancerForOwner {
99        // @phan-suppress-previous-line PhanPluginNeverReturnMethod
100        throw new BadMethodCallException( "Method is not supported." );
101    }
102
103    public function getExternalLB( $cluster ): ILoadBalancer {
104        // @phan-suppress-previous-line PhanPluginNeverReturnMethod
105        throw new BadMethodCallException( "Method is not supported." );
106    }
107
108    public function getAllMainLBs(): array {
109        return [ self::CLUSTER_MAIN_DEFAULT => $this->lb ];
110    }
111
112    public function getAllExternalLBs(): array {
113        return [];
114    }
115
116    protected function getLBsForOwner() {
117        if ( isset( $this->lb ) ) { // may not be set during _destruct()
118            yield $this->lb;
119        }
120    }
121
122    public function __destruct() {
123        // do nothing since the connection was injected
124    }
125}