Passed
Push — master ( adc4b8...42aaff )
by Daimona
01:39
created

TaskDataProvider   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 137
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 52
dl 0
loc 137
rs 10
c 0
b 0
f 0
wmc 22

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getValidTimestamp() 0 13 4
A getUsersList() 0 8 2
A getUsersToProcess() 0 16 5
A addCreatedPages() 0 2 1
A removeUser() 0 2 1
A getPagesToClose() 0 12 4
A getCreatedPages() 0 2 1
A getOpenPages() 0 23 4
1
<?php declare( strict_types=1 );
2
3
namespace BotRiconferme;
4
5
use BotRiconferme\Request\RequestBase;
6
7
/**
8
 * Object holding data to be shared between different tasks.
9
 */
10
class TaskDataProvider extends ContextSource {
11
	/** @var array[] */
12
	private $processUsers;
13
	/** @var array[] */
14
	private $allUsers;
15
	/** @var PageRiconferma[] */
16
	private $createdPages = [];
17
18
	/**
19
	 * Get the full content of the JSON users list
20
	 *
21
	 * @return array[]
22
	 */
23
	public function getUsersList() : array {
24
		if ( $this->allUsers === null ) {
25
			$this->getLogger()->debug( 'Retrieving users list' );
26
			$content = $this->getController()->getPageContent( $this->getConfig()->get( 'list-title' ) );
27
			$this->allUsers = json_decode( $content, true );
28
		}
29
30
		return $this->allUsers;
31
	}
32
33
	/**
34
	 * Get a list of users to execute tasks on.
35
	 *
36
	 * @return array[]
37
	 */
38
	public function getUsersToProcess() : array {
39
		if ( $this->processUsers === null ) {
40
			$this->processUsers = [];
41
			foreach ( $this->getUsersList() as $user => $groups ) {
42
				$timestamp = $this->getValidTimestamp( $groups );
43
44
				if ( date( 'd/m', $timestamp ) === date( 'd/m' ) &&
45
					// Don't trigger if the date is actually today
46
					date( 'd/m/Y', $timestamp ) !== date( 'd/m/Y' )
47
				) {
48
					$this->processUsers[ $user ] = $groups;
49
				}
50
			}
51
		}
52
53
		return $this->processUsers;
54
	}
55
56
	/**
57
	 * Get the valid timestamp for the given groups
58
	 *
59
	 * @param array $groups
60
	 * @return int
61
	 */
62
	private function getValidTimestamp( array $groups ) : int {
63
		$checkuser = isset( $groups[ 'checkuser' ] ) ?
64
			\DateTime::createFromFormat( 'd/m/Y', $groups[ 'checkuser' ] )->getTimestamp() :
65
			0;
66
		$bureaucrat = isset( $groups[ 'bureaucrat' ] ) ?
67
			\DateTime::createFromFormat( 'd/m/Y', $groups[ 'bureaucrat' ] )->getTimestamp() :
68
			0;
69
70
		$timestamp = max( $bureaucrat, $checkuser );
71
		if ( $timestamp === 0 ) {
72
			$timestamp = \DateTime::createFromFormat( 'd/m/Y', $groups[ 'sysop' ] )->getTimestamp();
73
		}
74
		return $timestamp;
75
	}
76
77
	/**
78
	 * Get a list of all open procedures
79
	 *
80
	 * @return PageRiconferma[]
81
	 */
82
	public function getOpenPages() : array {
83
		static $list = null;
84
		if ( $list === null ) {
85
			$baseTitle = $this->getConfig()->get( 'ric-main-page' );
86
			$params = [
87
				'action' => 'query',
88
				'prop' => 'templates',
89
				'titles' => $baseTitle,
90
				'tl_namespace' => 4,
91
				'tllimit' => 'max'
92
			];
93
94
			$res = RequestBase::newFromParams( $params )->execute();
95
			$pages = $res->query->pages;
96
			$list = [];
97
			foreach ( reset( $pages )->templates as $page ) {
98
				if ( preg_match( "!$baseTitle\/[^\/]+\/\d!", $page->title ) !== false ) {
99
					$list[] = new PageRiconferma( $page->title );
100
				}
101
			}
102
		}
103
104
		return $list;
105
	}
106
107
	/**
108
	 * Get a list of all procedures to be closed
109
	 *
110
	 * @return PageRiconferma[]
111
	 */
112
	public function getPagesToClose() : array {
113
		static $list = null;
114
		if ( $list === null ) {
115
			$allPages = $this->getOpenPages();
116
			$list = [];
117
			foreach ( $allPages as $page ) {
118
				if ( time() > $page->getEndTimestamp() ) {
119
					$list[] = $page;
120
				}
121
			}
122
		}
123
		return $list;
124
	}
125
126
	/**
127
	 * Discard an user from the current list
128
	 *
129
	 * @param string $name
130
	 */
131
	public function removeUser( string $name ) {
132
		unset( $this->processUsers[ $name ] );
133
	}
134
135
	/**
136
	 * @return PageRiconferma[]
137
	 */
138
	public function getCreatedPages() : array {
139
		return $this->createdPages;
140
	}
141
142
	/**
143
	 * @param PageRiconferma $page
144
	 */
145
	public function addCreatedPages( PageRiconferma $page ) {
146
		$this->createdPages[] = $page;
147
	}
148
}
149