Passed
Push — master ( bac275...6454e3 )
by Daimona
02:17
created

PageBotList::getValidTimestamp()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 8
nc 8
nop 1
dl 0
loc 13
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
	 * Get the valid timestamp for the given groups
33
	 *
34
	 * @param array $groups
35
	 * @return int
36
	 */
37
	public static function getValidTimestamp( array $groups ): int {
38
		$checkuser = isset( $groups['checkuser'] ) ?
39
			\DateTime::createFromFormat( 'd/m/Y', $groups['checkuser'] )->getTimestamp() :
40
			0;
41
		$bureaucrat = isset( $groups['bureaucrat'] ) ?
42
			\DateTime::createFromFormat( 'd/m/Y', $groups['bureaucrat'] )->getTimestamp() :
43
			0;
44
45
		$timestamp = max( $bureaucrat, $checkuser );
46
		if ( $timestamp === 0 ) {
47
			$timestamp = \DateTime::createFromFormat( 'd/m/Y', $groups['sysop'] )->getTimestamp();
48
		}
49
		return $timestamp;
50
	}
51
52
	/**
53
	 * Get the actual list of admins
54
	 *
55
	 * @return array[]
56
	 */
57
	public function getAdminsList() : array {
58
		return json_decode( $this->getContent(), true );
59
	}
60
}
61