Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
25 / 25
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
ClientTierStore
100.00% covered (success)
100.00%
25 / 25
100.00% covered (success)
100.00%
3 / 3
4
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getClientTierName
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
2
 setClientTierName
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace MediaWiki\Extension\OAuthRateLimiter;
4
5use Wikimedia\Rdbms\ILBFactory;
6use Wikimedia\Rdbms\ILoadBalancer;
7
8class ClientTierStore {
9
10    /**
11     * @var ILoadBalancer
12     */
13    private $loadBalancer;
14
15    /**
16     * @var string|false
17     */
18    private $centralWiki;
19
20    /**
21     * @param ILBFactory $lbFactory
22     * @param string|false $centralWiki
23     */
24    public function __construct(
25        ILBFactory $lbFactory,
26        $centralWiki
27    ) {
28        $this->centralWiki = $centralWiki;
29        $this->loadBalancer = $lbFactory->getMainLB( $centralWiki );
30    }
31
32    /**
33     * @param string $clientID
34     * @return int|string|null
35     */
36    public function getClientTierName( string $clientID ) {
37        $dbr = $this->loadBalancer->getConnection( DB_REPLICA, [], $this->centralWiki );
38
39        $res = $dbr->newSelectQueryBuilder()
40            ->select( 'oarct_tier_name' )
41            ->from( 'oauth_ratelimit_client_tier' )
42            ->where( [ 'oarct_client_id' => $clientID ] )
43            ->caller( __METHOD__ )
44            ->fetchField();
45
46        if ( $res ) {
47            return $res;
48        }
49
50        return null;
51    }
52
53    /**
54     * @param string $clientID
55     * @param string $tierName
56     * @return bool True if successful, false otherwise
57     */
58    public function setClientTierName( string $clientID, string $tierName ): bool {
59        $dbw = $this->loadBalancer->getConnection( DB_PRIMARY, [], $this->centralWiki );
60
61        $dbw->newInsertQueryBuilder()
62            ->insertInto( 'oauth_ratelimit_client_tier' )
63            ->row( [
64                'oarct_client_id' => $clientID,
65                'oarct_tier_name' => $tierName,
66            ] )
67            ->onDuplicateKeyUpdate()
68            ->uniqueIndexFields( [ 'oarct_client_id' ] )
69            ->set( [ 'oarct_tier_name' => $tierName ] )
70            ->caller( __METHOD__ )
71            ->execute();
72
73        return (bool)$dbw->affectedRows();
74    }
75}