Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
IPCountInfoRetriever
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
3 / 3
4
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getName
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 retrieveFor
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2namespace MediaWiki\IPInfo\InfoRetriever;
3
4use MediaWiki\IPInfo\Info\IPCountInfo;
5use MediaWiki\IPInfo\TempUserIPLookup;
6use MediaWiki\User\UserIdentity;
7
8/**
9 * Retrieves the number of unique IP addresses used by a temporary user account.
10 */
11class IPCountInfoRetriever extends BaseInfoRetriever {
12
13    public const NAME = 'ipinfo-source-ip-count';
14
15    private TempUserIPLookup $tempUserIPLookup;
16
17    public function __construct( TempUserIPLookup $tempUserIPLookup ) {
18        $this->tempUserIPLookup = $tempUserIPLookup;
19    }
20
21    public function getName(): string {
22        return self::NAME;
23    }
24
25    public function retrieveFor( UserIdentity $user, ?string $ip ): IPCountInfo {
26        // Showing the count of unique IP addresses only makes sense for temporary users,
27        // since anonymous users are identified by their IP address and therefore by definition
28        // have a unique IP address count of 1.
29        // So, don't attempt to fetch or show this data for them.
30        if ( !$user->isRegistered() ) {
31            return new IPCountInfo( null );
32        }
33
34        $count = $this->tempUserIPLookup->getDistinctAddressCount( $user );
35        return new IPCountInfo( $count );
36    }
37}