Passed
Push — master ( 89d164...fd4e50 )
by Daimona
01:38
created

PageBotList   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 31
c 3
b 0
f 0
dl 0
loc 91
rs 10
wmc 14

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getValidFlagTimestamp() 0 13 4
A __construct() 0 2 1
A get() 0 6 2
A getOverrideTimestamp() 0 12 3
A getAdminsList() 0 10 3
A getDecodedContent() 0 2 1
1
<?php declare( strict_types=1 );
2
3
namespace BotRiconferme\Wiki\Page;
4
5
use BotRiconferme\Config;
6
use BotRiconferme\Wiki\User;
7
use BotRiconferme\Wiki\Wiki;
8
9
/**
10
 * Singleton class representing the JSON list of admins
11
 */
12
class PageBotList extends Page {
13
	public const NON_GROUP_KEYS = [ 'override', 'override-perm', 'aliases' ];
14
15
	/** @var User[]|null */
16
	private $adminsList;
17
18
	/**
19
	 * @private Use self::get()
20
	 * @param Wiki $wiki
21
	 */
22
	public function __construct( Wiki $wiki ) {
23
		parent::__construct( Config::getInstance()->get( 'list-title' ), $wiki );
24
	}
25
26
	/**
27
	 * Instance getter
28
	 *
29
	 * @param Wiki $wiki
30
	 * @return self
31
	 */
32
	public static function get( Wiki $wiki ) : self {
33
		static $instance = null;
34
		if ( $instance === null ) {
35
			$instance = new self( $wiki );
36
		}
37
		return $instance;
38
	}
39
40
	/**
41
	 * @param string[] $groups
42
	 * @return int|null
43
	 */
44
	public static function getOverrideTimestamp( array $groups ) : ?int {
45
		if ( !array_intersect_key( $groups, [ 'override-perm' => true, 'override' => true ] ) ) {
46
			return null;
47
		}
48
49
		// A one-time override takes precedence
50
		if ( array_key_exists( 'override', $groups ) ) {
51
			$date = $groups['override'];
52
		} else {
53
			$date = $groups['override-prem'] . '/' . date( 'Y' );
54
		}
55
		return \DateTime::createFromFormat( 'd/m/Y', $date )->getTimestamp();
56
	}
57
58
	/**
59
	 * Get the valid timestamp for the given groups
60
	 *
61
	 * @param array $groups
62
	 * @return int
63
	 */
64
	public static function getValidFlagTimestamp( array $groups ): int {
65
		$checkuser = isset( $groups['checkuser'] ) ?
66
			\DateTime::createFromFormat( 'd/m/Y', $groups['checkuser'] )->getTimestamp() :
67
			0;
68
		$bureaucrat = isset( $groups['bureaucrat'] ) ?
69
			\DateTime::createFromFormat( 'd/m/Y', $groups['bureaucrat'] )->getTimestamp() :
70
			0;
71
72
		$timestamp = max( $bureaucrat, $checkuser );
73
		if ( $timestamp === 0 ) {
74
			$timestamp = \DateTime::createFromFormat( 'd/m/Y', $groups['sysop'] )->getTimestamp();
75
		}
76
		return $timestamp;
77
	}
78
79
	/**
80
	 * Get the actual list of admins
81
	 *
82
	 * @return User[]
83
	 */
84
	public function getAdminsList() : array {
85
		if ( $this->adminsList === null ) {
86
			$this->adminsList = [];
87
			foreach ( $this->getDecodedContent() as $user => $info ) {
88
				$userObj = new User( $user, $this->wiki );
89
				$userObj->setInfo( $info );
90
				$this->adminsList[ $user ] = $userObj;
91
			}
92
		}
93
		return $this->adminsList;
94
	}
95
96
	/**
97
	 * Get the JSON-decoded content of the list
98
	 *
99
	 * @return array[]
100
	 */
101
	public function getDecodedContent() : array {
102
		return json_decode( $this->getContent(), true );
103
	}
104
}
105