Passed
Push — master ( d72fa1...8ee621 )
by Daimona
01:55
created

TaskDataProvider   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 39
dl 0
loc 102
rs 10
c 0
b 0
f 0
wmc 15

6 Methods

Rating   Name   Duplication   Size   Complexity  
A addCreatedPages() 0 2 1
A getValidTimestamp() 0 13 4
A removeUser() 0 2 1
A getUsersToProcess() 0 20 5
A getCreatedPages() 0 2 1
A getOpenPages() 0 19 3
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 $users;
13
14
	/** @var string[] */
15
	private $createdPages = [];
16
17
	/**
18
	 * Get a list of users to execute tasks on.
19
	 *
20
	 * @return array[]
21
	 */
22
	public function getUsersToProcess() : array {
23
		if ( $this->users === null ) {
24
			$this->getLogger()->debug( 'Retrieving users list' );
25
			$content = $this->getController()->getPageContent( $this->getConfig()->get( 'list-title' ) );
26
			$listUsers = json_decode( $content, true );
27
28
			$this->users = [];
29
			foreach ( $listUsers as $user => $groups ) {
30
				$timestamp = $this->getValidTimestamp( $groups );
31
32
				if ( date( 'd/m', $timestamp ) === date( 'd/m' ) &&
33
					// Don't trigger if the date is actually today
34
					date( 'd/m/Y', $timestamp ) !== date( 'd/m/Y' )
35
				) {
36
					$this->users[ $user ] = $groups;
37
				}
38
			}
39
		}
40
41
		return $this->users;
42
	}
43
44
	/**
45
	 * Get the valid timestamp for the given groups
46
	 *
47
	 * @param array $groups
48
	 * @return int
49
	 */
50
	private function getValidTimestamp( array $groups ) : int {
51
		$checkuser = isset( $groups[ 'checkuser' ] ) ?
52
			\DateTime::createFromFormat( 'd/m/Y', $groups[ 'checkuser' ] )->getTimestamp() :
53
			0;
54
		$bureaucrat = isset( $groups[ 'bureaucrat' ] ) ?
55
			\DateTime::createFromFormat( 'd/m/Y', $groups[ 'bureaucrat' ] )->getTimestamp() :
56
			0;
57
58
		$timestamp = max( $bureaucrat, $checkuser );
59
		if ( $timestamp === 0 ) {
60
			$timestamp = \DateTime::createFromFormat( 'd/m/Y', $groups[ 'sysop' ] )->getTimestamp();
61
		}
62
		return $timestamp;
63
	}
64
65
	/**
66
	 * Get a list of all open procedures
67
	 *
68
	 * @return string[] Their titles
69
	 */
70
	public function getOpenPages() : array {
71
		$baseTitle = $this->getConfig()->get( 'ric-main-page' );
72
		$params = [
73
			'action' => 'query',
74
			'prop' => 'templates',
75
			'titles' => $baseTitle,
76
			'tl_namespace' => 4,
77
			'tllimit' => 'max'
78
		];
79
80
		$res = RequestBase::newFromParams( $params )->execute();
81
		$pages = $res->query->pages;
82
		$ret = [];
83
		foreach ( reset( $pages )->templates as $page ) {
84
			if ( preg_match( "!$baseTitle\/[^\/]+\/\d!", $page->title ) !== false ) {
85
				$ret[] = $page->title;
86
			}
87
		}
88
		return $ret;
89
	}
90
91
	/**
92
	 * Discard an user from the current list
93
	 *
94
	 * @param string $name
95
	 */
96
	public function removeUser( string $name ) {
97
		unset( $this->users[ $name ] );
98
	}
99
100
	/**
101
	 * @return string[]
102
	 */
103
	public function getCreatedPages() : array {
104
		return $this->createdPages;
105
	}
106
107
	/**
108
	 * @param string $title
109
	 */
110
	public function addCreatedPages( string $title ) {
111
		$this->createdPages[] = $title;
112
	}
113
}
114