Completed
Push — master ( f1a924...85530c )
by mw
07:12
created

RecursiveGroupMembersIterator::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 4
cts 4
cp 1
rs 10
cc 1
eloc 3
nc 1
nop 2
crap 1
1
<?php
2
3
namespace SMW\Notifications\Iterators;
4
5
use SMW\Store;
6
use SMW\DIProperty;
7
use SMW\DIWikiPage;
8
use SMW\Notifications\PropertyRegistry;
9
use RecursiveIterator;
10
use ArrayIterator;
11
use Iterator;
12
use Hooks;
13
14
/**
15
 * The purpose of this iterator is to lazy load subjects (== users) that are members
16
 * of a selected group.
17
 *
18
 * @license GNU GPL v2+
19
 * @since 1.0
20
 *
21
 * @author mwjames
22
 */
23
class RecursiveGroupMembersIterator implements RecursiveIterator {
24
25
	/**
26
	 * @var Store
27
	 */
28
	private $store;
29
30
	/**
31
	 * @var array $primaryKey The name of the primary key(s)
32
	 */
33
	protected $groups;
34
35
	/**
36
	 * @var array $current The current iterator value
37
	 */
38
	private $current = [];
39
40
	/**
41
	 * @var integer key 0-indexed number of pages fetched since self::reset()
42
	 */
43
	private $key;
44
45
	/**
46
	 * @var string
47
	 */
48
	private $agentName = '';
49
50
	/**
51
	 * @var boolean
52
	 */
53
	private $notifyAgent = false;
54
55
	/**
56
	 * @var DIWikiPage|null
57
	 */
58
	private $subject = null;
59
60
	/**
61
	 * @since 1.0
62
	 *
63
	 * @param Iterator|array $groups
64
	 * @param Store $store
65
	 */
66 11
	public function __construct( $groups, Store $store ) {
67 11
		$this->groups = $groups;
0 ignored issues
show
Documentation Bug introduced by
It seems like $groups can also be of type object<Iterator>. However, the property $groups is declared as type array. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
68 11
		$this->store = $store;
69 11
	}
70
71
	/**
72
	 * @since 1.0
73
	 *
74
	 * @param string $agentName
75
	 */
76 4
	public function setAgentName( $agentName ) {
77 4
		$this->agentName = str_replace( ' ', '_', $agentName );
78 4
	}
79
80
	/**
81
	 * @since 1.0
82
	 *
83
	 * @param boolean $notifyAgent
84
	 */
85 2
	public function notifyAgent( $notifyAgent ) {
86 2
		$this->notifyAgent = (bool)$notifyAgent;
87 2
	}
88
89
	/**
90
	 * @since 1.0
91
	 *
92
	 * @param DIWikiPage|null $subject
93
	 */
94 3
	public function setSubject( DIWikiPage $subject = null ) {
95 3
		$this->subject = $subject;
96 3
	}
97
98
	/**
99
	 * @since 1.0
100
	 *
101
	 * @return array The most recently fetched set of rows from the database
102
	 */
103 9
	public function current() {
104 9
		return $this->current;
105
	}
106
107
	/**
108
	 * @since 1.0
109
	 *
110
	 * @return integer 0-indexed count of the page number fetched
111
	 */
112
	public function key() {
113
		return $this->key;
114
	}
115
116
	/**
117
	 * @since 1.0
118
	 *
119
	 * Reset the iterator to the beginning of the table.
120
	 */
121 3
	public function rewind() {
122 3
		$this->key = -1; // self::next() will turn this into 0
123 3
		$this->current = [];
124 3
		$this->next();
125 3
	}
126
127
	/**
128
	 * @since 1.0
129
	 *
130
	 * @return bool True when the iterator is in a valid state
131
	 */
132 3
	public function valid() {
133 3
		return (bool)$this->current;
134
	}
135
136
	/**
137
	 * @since 1.0
138
	 *
139
	 * @return bool True when this result set has rows
140
	 */
141
	public function hasChildren() {
142
		return $this->current && count( $this->current );
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->current of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
143
	}
144
145
	/**
146
	 * @since 1.0
147
	 *
148
	 * @return RecursiveIterator
149
	 */
150
	public function getChildren() {
151
		return new ChildlessRecursiveIterator( $this->current );
152
	}
153
154
	/**
155
	 * Fetch the next set of rows from the database.
156
	 *
157
	 * @since 1.0
158
	 *
159
	 * {@inheritDoc}
160
	 */
161 10
	public function next() {
162
163
		// Initial state, transform it to a workable state
164 10
		if ( $this->key === -1 ) {
165 3
			$this->initGroups();
166 3
		}
167
168 10
		$group = array_shift( $this->groups );
169
170 10
		if ( $group === false || $group === array() || $group === null ) {
171 4
			$this->current = array();
172 4
			return false;
173
		}
174
175 9
		$recipients = array();
176
177 9
		foreach ( $group as $groupName ) {
178
179 9
			wfDebugLog( 'smw', __METHOD__ . ' groupName: ' . $groupName->getString() );
180
181 9
			$members = $this->store->getPropertySubjects(
182 9
				new DIProperty( PropertyRegistry::NOTIFICATIONS_GROUP_MEMBER_OF ),
183
				$groupName
184 9
			);
185
186 9
			$this->doFilterGroupMembers( $members, $groupName, $recipients );
187 9
		}
188
189 9
		$this->current = array_values( $recipients );
190 9
		$this->key++;
191 9
	}
192
193 9
	private function doFilterGroupMembers( $members, $groupName, &$recipients ) {
194
195 9
		foreach ( $members as $member ) {
196
197 9
			if ( !$this->notifyAgent && $this->agentName === $member->getDBKey() ) {
198 2
				continue;
199
			}
200
201 9
			if ( Hooks::run( 'SMW::Notifications::UserCanReceiveNotification', array( $this->subject, $this->agentName, $groupName, $member ) ) === false ) {
202
				continue;
203
			}
204
205
			// Avoids duplicate members in a group but this will not avoid
206
			// duplicates beyond the group because we don't track them otherwise
207
			// we would require an in-memory hash table
208 9
			$recipients[$member->getHash()] = $member->getDBKey();
209 9
		}
210 9
	}
211
212 3
	private function initGroups() {
213
214
		// It might be that we used an Array or CallbackIterator to avoid
215
		// an initial data load, resolve the iterator now to get a list
216
		// of groups
217 3
		if ( $this->groups instanceof Iterator ) {
218 1
			$this->groups = iterator_to_array( $this->groups, false );
219 1
		}
220
221
		// Remove any keys used earlier to filter duplicates
222 3
		$this->groups = array_values( $this->groups );
223 3
	}
224
225
}
226