Passed
Push — master ( 6454e3...7bccfd )
by Daimona
02:03
created

PageBotList::getOverrideTimestamp()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php declare( strict_types=1 );
2
3
namespace BotRiconferme\Wiki\Page;
4
5
use BotRiconferme\Config;
6
7
/**
8
 * Singleton class representing the JSON list of admins
9
 */
10
class PageBotList extends Page {
11
	/**
12
	 * @private Use self::get()
13
	 */
14
	public function __construct() {
15
		parent::__construct( Config::getInstance()->get( 'list-title' ) );
16
	}
17
18
	/**
19
	 * Instance getter
20
	 *
21
	 * @return self
22
	 */
23
	public static function get() : self {
24
		static $instance = null;
25
		if ( $instance === null ) {
26
			$instance = new self;
27
		}
28
		return $instance;
29
	}
30
31
	/**
32
	 * @param string[] $groups
33
	 * @return int|null
34
	 */
35
	public static function getOverrideTimestamp( array $groups ) : ?int {
36
		if ( array_intersect_key( $groups, [ 'override-perm' => true, 'override' => true ] ) ) {
37
			// A one-time override takes precedence
38
			$date = $groups[ 'override' ] ?? $groups[ 'override-perm' ];
39
			return \DateTime::createFromFormat( 'd/m/Y', $date )->getTimestamp();
40
		}
41
		return null;
42
	}
43
44
	/**
45
	 * Get the valid timestamp for the given groups
46
	 *
47
	 * @param array $groups
48
	 * @return int
49
	 */
50
	public static function getValidFlagTimestamp( array $groups ): int {
51
		$checkuser = isset( $groups['checkuser'] ) ?
52
			\DateTime::createFromFormat( 'd/m/Y', $groups['checkuser'] )->getTimestamp() :
53
			0;
54
		$bureaucrat = isset( $groups['bureaucrat'] ) ?
55
			\DateTime::createFromFormat( 'd/m/Y', $groups['bureaucrat'] )->getTimestamp() :
56
			0;
57
58
		$timestamp = max( $bureaucrat, $checkuser );
59
		if ( $timestamp === 0 ) {
60
			$timestamp = \DateTime::createFromFormat( 'd/m/Y', $groups['sysop'] )->getTimestamp();
61
		}
62
		return $timestamp;
63
	}
64
65
	/**
66
	 * Get the actual list of admins
67
	 *
68
	 * @return array[]
69
	 */
70
	public function getAdminsList() : array {
71
		return json_decode( $this->getContent(), true );
72
	}
73
}
74