Passed
Push — master ( 935a55...baa252 )
by Daimona
01:37
created

PageBotList::isOverrideExpired()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 3
eloc 7
c 1
b 1
f 0
nc 3
nop 1
dl 0
loc 11
rs 10
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
	 * @param array $groups
81
	 * @return bool
82
	 */
83
	public static function isOverrideExpired( array $groups ) : bool {
84
		if ( !isset( $groups['override'] ) ) {
85
			return false;
86
		}
87
88
		$flagTS = self::getValidFlagTimestamp( $groups );
89
		$usualTS = strtotime( date( 'Y' ) . '-' . date( 'm-d', $flagTS ) );
90
		$overrideTS = \DateTime::createFromFormat( 'd/m/Y', $groups['override'] )->getTimestamp();
91
		$delay = 60 * 60 * 24 * 3;
92
93
		return time() > $usualTS + $delay && time() > $overrideTS + $delay;
94
	}
95
96
	/**
97
	 * Get the actual list of admins
98
	 *
99
	 * @return User[]
100
	 */
101
	public function getAdminsList() : array {
102
		if ( $this->adminsList === null ) {
103
			$this->adminsList = [];
104
			foreach ( $this->getDecodedContent() as $user => $info ) {
105
				$userObj = new User( $user, $this->wiki );
106
				$userObj->setInfo( $info );
107
				$this->adminsList[ $user ] = $userObj;
108
			}
109
		}
110
		return $this->adminsList;
111
	}
112
113
	/**
114
	 * Get the JSON-decoded content of the list
115
	 *
116
	 * @return array[]
117
	 */
118
	public function getDecodedContent() : array {
119
		return json_decode( $this->getContent(), true );
120
	}
121
}
122