UserLocator::doLocateEventSubscribers()   B
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 66

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 35
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 66
ccs 35
cts 35
cp 1
rs 8.4307
c 0
b 0
f 0
cc 5
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();
39 3
		$iteratorFactory = new IteratorFactory();
40
41 3
		$notificationGroupsLocator = new NotificationGroupsLocator(
42 3
			$store
43
		);
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
		} 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->getNotificationsToGroupListAsCallback( $subSemanticDataMatch )
56
			);
57
		}
58
59 3
		$recursiveMembersIterator = $iteratorFactory->newRecursiveMembersIterator(
60 3
			$groups,
61 3
			$store
62
		);
63
64 3
		$agentName = $event->getAgent()->getName();
65
66 3
		$recursiveMembersIterator->notifyAgent(
67 3
			$event->getExtraParam( 'notifyAgent', false )
68
		);
69
70 3
		$recursiveMembersIterator->setAgentName(
71 3
			$agentName
72
		);
73
74 3
		$recursiveMembersIterator->setSubject(
75 3
			$extra['subject']
76
		);
77
78
		// Returns a flat array when iterating over the children
79 3
		$recursiveIteratorIterator = $iteratorFactory->newRecursiveIteratorIterator(
80 3
			$recursiveMembersIterator
81
		);
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