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

User   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Importance

Changes 5
Bugs 0 Features 0
Metric Value
eloc 19
dl 0
loc 83
rs 10
c 5
b 0
f 0
wmc 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getRegex() 0 2 1
A getUserInfo() 0 6 2
A inGroup() 0 2 1
A __construct() 0 3 1
A __toString() 0 2 1
A getName() 0 2 1
A getGroups() 0 2 1
A getAliases() 0 6 2
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[]|null */
14
	private $groups;
15
	/** @var string[]|null */
16
	private $aliases;
17
18
	/**
19
	 * @param string $name
20
	 * @param Wiki $wiki
21
	 */
22
	public function __construct( string $name, Wiki $wiki ) {
23
		parent::__construct( $wiki );
24
		$this->name = $name;
25
	}
26
27
	/**
28
	 * @return string
29
	 */
30
	public function getName() : string {
31
		return $this->name;
32
	}
33
34
	/**
35
	 * Get a list of groups this user belongs to
36
	 *
37
	 * @return string[]
38
	 */
39
	public function getGroups() : array {
40
		return array_keys( array_diff_key( $this->getUserInfo(), PageBotList::NON_GROUP_KEYS ) );
41
	}
42
43
	/**
44
	 * Get some info about this user, including flag dates.
45
	 *
46
	 * @return string[]
47
	 */
48
	public function getUserInfo() : array {
49
		if ( $this->groups === null ) {
50
			$usersList = PageBotList::get( $this->wiki )->getAdminsList();
51
			$this->groups = $usersList[ $this->name ];
52
		}
53
		return $this->groups;
54
	}
55
56
	/**
57
	 * Whether the user is in the given group
58
	 *
59
	 * @param string $groupName
60
	 * @return bool
61
	 */
62
	public function inGroup( string $groupName ) : bool {
63
		return in_array( $groupName, $this->getGroups() );
64
	}
65
66
	/**
67
	 * Returns a regex for matching the name of this user
68
	 *
69
	 * @inheritDoc
70
	 */
71
	public function getRegex() : string {
72
		return str_replace( ' ', '[ _]', preg_quote( $this->name ) );
73
	}
74
75
	/**
76
	 * Get a list of aliases for this user.
77
	 *
78
	 * @return string[]
79
	 */
80
	public function getAliases() : array {
81
		if ( $this->aliases === null ) {
82
			$usersList = PageBotList::get( $this->wiki )->getAdminsList();
83
			$this->aliases = $usersList[ $this->name ];
84
		}
85
		return $this->aliases;
86
	}
87
88
	/**
89
	 * @return string
90
	 */
91
	public function __toString() {
92
		return $this->name;
93
	}
94
}
95