dispatchUpdateJobFor()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 28
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 3

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 13
c 2
b 0
f 0
dl 0
loc 28
ccs 14
cts 14
cp 1
rs 9.8333
cc 3
nc 3
nop 2
crap 3
1
<?php
2
3
namespace SCI;
4
5
use SMW\Store;
0 ignored issues
show
Bug introduced by
The type SMW\Store was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use SMW\DIProperty;
0 ignored issues
show
Bug introduced by
The type SMW\DIProperty was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use SMW\DIWikiPage;
0 ignored issues
show
Bug introduced by
The type SMW\DIWikiPage was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use SMW\Services\ServicesFactory as ApplicationFactory;
0 ignored issues
show
Bug introduced by
The type SMW\Services\ServicesFactory was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use SMW\HashBuilder;
0 ignored issues
show
Bug introduced by
The type SMW\HashBuilder was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
use SMW\SQLStore\ChangeOp\ChangeOp;
0 ignored issues
show
Bug introduced by
The type SMW\SQLStore\ChangeOp\ChangeOp was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
12
/**
13
 * If a citation text was altered then lookup related references on pages used
14
 * and schedule a dispatch update job.
15
 *
16
 * @license GNU GPL v2+
17
 * @since 1.0
18
 *
19
 * @author mwjames
20
 */
21
class CitationTextChangeUpdateJobDispatcher {
22
23
	/**
24
	 * @var Store
25
	 */
26
	private $store;
27
28
	/**
29
	 * @var ReferenceBacklinksLookup
30
	 */
31
	private $referenceBacklinksLookup;
32
33
	/**
34
	 * @var boolean
35
	 */
36
	private $enabledUpdateJobState = true;
37
38
	/**
39
	 * @since  1.0
40
	 *
41
	 * @param Store $store
42
	 * @param ReferenceBacklinksLookup $referenceBacklinksLookup
43
	 */
44 19
	public function __construct( Store $store, ReferenceBacklinksLookup $referenceBacklinksLookup ) {
45 19
		$this->store = $store;
46 19
		$this->referenceBacklinksLookup = $referenceBacklinksLookup;
47 19
	}
48
49
	/**
50
	 * @since  1.0
51
	 *
52
	 * @param boolean $enabledUpdateJobState
53
	 */
54 15
	public function setEnabledUpdateJobState( $enabledUpdateJobState ) {
55 15
		$this->enabledUpdateJobState = $enabledUpdateJobState;
56 15
	}
57
58
	/**
59
	 * @since  1.0
60
	 *
61
	 * @param DIWikiPage $subject
62
	 * @param ChangeOp $changeOp
63
	 *
64
	 * @return boolean
65
	 */
66 18
	public function dispatchUpdateJobFor( DIWikiPage $subject, ChangeOp $changeOp ) {
67
68 18
		if ( !$this->enabledUpdateJobState ) {
69 15
			return false;
70
		}
71
72 3
		$tableName = $this->store->getPropertyTableInfoFetcher()->findTableIdForProperty(
73 3
			new DIProperty( PropertyRegistry::SCI_CITE_TEXT )
74
		);
75
76 3
		$subjectIdList = $this->getSubjectListFrom(
77 3
			$changeOp->getOrderedDiffByTable( $tableName )
78
		);
79
80 3
		if ( ( $jobList = $this->getDispatchableTargetList( $subjectIdList ) ) === [] ) {
81 1
			return false;
82
		}
83
84 2
		$updateDispatcherJob = ApplicationFactory::getInstance()->newJobFactory()->newUpdateDispatcherJob(
85 2
			$subject->getTitle(),
86
			[
87 2
				'job-list' => $jobList
88
			]
89
		);
90
91 2
		$updateDispatcherJob->insert();
92
93 2
		return true;
94
	}
95
96 3
	private function getSubjectListFrom( array $orderedDiff ) {
97
98 3
		$subjectIdList = [];
99
100
		// Find out whether a cite text object was altered
101 3
		foreach ( $orderedDiff as $key => $value ) {
102
103 2
			if ( strpos( $key, 'sci_cite_text' ) === false ) {
104
				continue;
105
			}
106
107 2
			if ( !isset( $value['delete'] ) ) {
108 1
				$value['delete'] = [];
109
			}
110
111 2
			foreach ( $value['delete'] as $delete ) {
112 1
				$subjectIdList[] = $delete['s_id'];
113
			}
114
115 2
			if ( !isset( $value['insert'] ) ) {
116 1
				$value['insert'] = [];
117
			}
118
119 2
			foreach ( $value['insert'] as $insert ) {
120 2
				$subjectIdList[] = $insert['s_id'];
121
			}
122
		}
123
124 3
		return $subjectIdList;
125
	}
126
127 3
	private function getDispatchableTargetList( array $subjectIdList ) {
128
129 3
		if ( $subjectIdList === [] ) {
130 1
			return [];
131
		}
132
133 2
		$hashList = $this->store->getObjectIds()->getDataItemPoolHashListFor( $subjectIdList );
134 2
		$referenceBacklinks = [];
135
136 2
		foreach ( $hashList as $hash ) {
137 2
			$referenceBacklinks += $this->referenceBacklinksLookup->findReferenceBacklinksFor(
138 2
				$this->referenceBacklinksLookup->tryToFindCitationKeyFor( DIWikiPage::doUnserialize( $hash ) )
139
			);
140
		}
141
142 2
		$targetBatch = [];
143
144 2
		foreach ( $referenceBacklinks as $referenceBacklink ) {
145 2
			$targetBatch[HashBuilder::getHashIdForDiWikiPage( $referenceBacklink )] = true;
146
		}
147
148 2
		return $targetBatch;
149
	}
150
151
}
152