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

UserLocator::doLocateEventSubscribers()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 66
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 40
CRAP Score 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 66
ccs 40
cts 40
cp 1
rs 8.6045
cc 5
eloc 35
nc 5
nop 1
crap 5

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace SMW\Notifications\ChangeNotification;
4
5
use SMW\ApplicationFactory;
6
use SMW\Notifications\IteratorFactory;
7
use EchoEvent;
8
use User;
9
10
/**
11
 * @license GNU GPL v2+
12
 * @since 1.0
13
 *
14
 * @author mwjames
15
 */
16
class UserLocator {
17
18
	/**
19
	 * Find out for which of the properties that have changed the assigned group
20
	 * and for each group do locate its members(users).
21
	 *
22
	 * @see EchoUserLocator::locateArticleCreator
23
	 * @since 1.0
24
	 *
25
	 * @param EchoEvent $event
26
	 *
27
	 * @return User[]|\Iterator
28
	 */
29 4
	public static function doLocateEventSubscribers( EchoEvent $event ) {
30
31 4
		$start = microtime( true );
32 4
		$extra = $event->getExtra();
33
34 4
		if ( !isset( $extra['subject'] ) || !isset( $extra['properties'] ) ) {
35 1
			return array();
36
		}
37
38 3
		$store = ApplicationFactory::getInstance()->getStore( '\SMW\SQLStore\SQLStore' );
39 3
		$iteratorFactory = new IteratorFactory();
40
41 3
		$notificationGroupsLocator = new NotificationGroupsLocator(
42 1
			$store
43 3
		);
44
45 3
		$subSemanticDataMatch = isset( $extra['subSemanticDataMatch'] ) ? $extra['subSemanticDataMatch'] : array();
46 3
		$type = $event->getType();
47
48 3
		if ( $type === ChangeNotificationFilter::SPECIFICATION_CHANGE ) {
49 2
			$groups = $notificationGroupsLocator->getSpecialGroupOnSpecificationChange();
50 2
		} else {
51
			// Find groups assigned to properties on a "lazy" request during the
52
			// iteration process
53 1
			$groups = $iteratorFactory->newMappingIterator(
54 1
				$extra['properties'],
55 1
				$notificationGroupsLocator->getNotificationsToGroupListByCallback( $subSemanticDataMatch )
56 1
			);
57
		}
58
59 3
		$recursiveGroupMemberIterator = $iteratorFactory->newRecursiveGroupMembersIterator(
60 3
			$groups,
61
			$store
62 3
		);
63
64 3
		$agentName = $event->getAgent()->getName();
65
66 3
		$recursiveGroupMemberIterator->notifyAgent(
67 3
			$event->getExtraParam( 'notifyAgent', false )
68 3
		);
69
70 3
		$recursiveGroupMemberIterator->setAgentName(
71
			$agentName
72 3
		);
73
74 3
		$recursiveGroupMemberIterator->setSubject(
75 3
			$extra['subject']
76 3
		);
77
78
		// Returns a flat array when iterating over the children
79 3
		$recursiveIteratorIterator = $iteratorFactory->newRecursiveIteratorIterator(
80
			$recursiveGroupMemberIterator
81 3
		);
82
83 3
		wfDebugLog( 'smw', 'Agent ' . $agentName );
84
85
		// Access the user only on request by the iterator
86 3
		$mappingIterator = $iteratorFactory->newMappingIterator( $recursiveIteratorIterator, function( $recipient ) {
87 1
			wfDebugLog( 'smw', 'User ' . $recipient );
88 1
			return User::newFromName( $recipient, false );
89 3
		} );
90
91 3
		wfDebugLog( 'smw', __METHOD__ . ' procTime (sec): ' . round( ( microtime( true ) - $start ), 7 ) );
92
93 3
		return $mappingIterator;
94
	}
95
96
}
97