Passed
Push — master ( 33abbd...74427d )
by Daimona
02:18
created

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