Passed
Push — master ( f2a928...da664e )
by Daimona
01:56
created

User::getGroupsWithDates()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php declare( strict_types=1 );
2
3
namespace BotRiconferme\Wiki;
4
5
use BotRiconferme\Wiki\Page\PageBotList;
6
7
/**
8
 * Class representing a single user. NOTE this can only represent users stored in the JSON list
9
 */
10
class User extends Element {
11
	/** @var string */
12
	private $name;
13
	/** @var string[] */
14
	private $groups;
15
16
	/**
17
	 * @param string $name
18
	 */
19
	public function __construct( string $name ) {
20
		$this->name = $name;
21
	}
22
23
	/**
24
	 * @return string
25
	 */
26
	public function getName() : string {
27
		return $this->name;
28
	}
29
30
	/**
31
	 * @return string[]
32
	 */
33
	public function getGroups() : array {
34
		return array_keys( $this->getGroupsWithDates() );
35
	}
36
37
	public function getGroupsWithDates() : array {
38
		if ( $this->groups === null ) {
39
			$usersList = PageBotList::get()->getAdminsList();
40
			$this->groups = $usersList[ $this->name ];
41
		}
42
		return $this->groups;
43
	}
44
45
	/**
46
	 * Whether the user is in the given group
47
	 *
48
	 * @param string $groupName
49
	 * @return bool
50
	 */
51
	public function inGroup( string $groupName ) : bool {
52
		return in_array( $groupName, $this->getGroups() );
53
	}
54
55
	/**
56
	 * Returns a regex for matching the name of this user
57
	 *
58
	 * @inheritDoc
59
	 */
60
	public function getRegex() : string {
61
		return str_replace( ' ', '[ _]', preg_quote( $this->name ) );
62
	}
63
64
	/**
65
	 * @return string
66
	 */
67
	public function __toString() {
68
		return $this->name;
69
	}
70
}
71