Passed
Push — master ( 59402b...899021 )
by John
11:15 queued 10s
created

RuleMatcher::getMatchingOperations()   C

Complexity

Conditions 12
Paths 48

Size

Total Lines 44
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 25
c 1
b 0
f 0
dl 0
loc 44
rs 6.9666
cc 12
nc 48
nop 2

How to fix   Complexity   

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
declare(strict_types=1);
3
/**
4
 * @copyright Copyright (c) 2019 Arthur Schiwon <[email protected]>
5
 *
6
 * @author Arthur Schiwon <[email protected]>
7
 *
8
 * @license GNU AGPL version 3 or any later version
9
 *
10
 * This program is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU Affero General Public License as
12
 * published by the Free Software Foundation, either version 3 of the
13
 * License, or (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 * GNU Affero General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU Affero General Public License
21
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
 *
23
 */
24
25
namespace OCA\WorkflowEngine\Service;
26
27
use OCA\WorkflowEngine\Helper\ScopeContext;
28
use OCA\WorkflowEngine\Manager;
29
use OCP\AppFramework\QueryException;
30
use OCP\Files\Storage\IStorage;
31
use OCP\IL10N;
32
use OCP\IServerContainer;
33
use OCP\IUserSession;
34
use OCP\WorkflowEngine\ICheck;
35
use OCP\WorkflowEngine\IEntity;
36
use OCP\WorkflowEngine\IEntityCheck;
37
use OCP\WorkflowEngine\IFileCheck;
38
use OCP\WorkflowEngine\IManager;
39
use OCP\WorkflowEngine\IOperation;
40
use OCP\WorkflowEngine\IRuleMatcher;
41
use RuntimeException;
42
43
class RuleMatcher implements IRuleMatcher {
44
45
	/** @var IUserSession */
46
	protected $session;
47
	/** @var IManager */
48
	protected $manager;
49
	/** @var array */
50
	protected $contexts;
51
	/** @var IServerContainer */
52
	protected $container;
53
	/** @var array */
54
	protected $fileInfo = [];
55
	/** @var IL10N */
56
	protected $l;
57
	/** @var IOperation */
58
	protected $operation;
59
	/** @var IEntity */
60
	protected $entity;
61
62
	public function __construct(
63
		IUserSession $session,
64
		IServerContainer $container,
65
		IL10N $l,
66
		Manager $manager
67
	) {
68
		$this->session = $session;
69
		$this->manager = $manager;
70
		$this->container = $container;
71
		$this->l = $l;
72
	}
73
74
	public function setFileInfo(IStorage $storage, string $path, bool $isDir = false): void {
75
		$this->fileInfo['storage'] = $storage;
76
		$this->fileInfo['path'] = $path;
77
		$this->fileInfo['isDir'] = $isDir;
78
	}
79
80
	public function setEntitySubject(IEntity $entity, $subject): void {
81
		$this->contexts[get_class($entity)] = [$entity, $subject];
82
	}
83
84
	public function setOperation(IOperation $operation): void {
85
		if($this->operation !== null) {
86
			throw new RuntimeException('This method must not be called more than once');
87
		}
88
		$this->operation = $operation;
89
	}
90
91
	public function setEntity(IEntity $entity): void {
92
		if($this->entity !== null) {
93
			throw new RuntimeException('This method must not be called more than once');
94
		}
95
		$this->entity = $entity;
96
	}
97
98
	public function getEntity(): IEntity {
99
		if($this->entity === null) {
100
			throw new \LogicException('Entity was not set yet');
101
		}
102
		return $this->entity;
103
	}
104
105
	public function getFlows(bool $returnFirstMatchingOperationOnly = true): array {
106
		if(!$this->operation) {
107
			throw new RuntimeException('Operation is not set');
108
		}
109
		return $this->getMatchingOperations(get_class($this->operation), $returnFirstMatchingOperationOnly);
110
	}
111
112
	public function getMatchingOperations(string $class, bool $returnFirstMatchingOperationOnly = true): array {
113
		$scopes[] = new ScopeContext(IManager::SCOPE_ADMIN);
0 ignored issues
show
Comprehensibility Best Practice introduced by
$scopes was never initialized. Although not strictly required by PHP, it is generally a good practice to add $scopes = array(); before regardless.
Loading history...
114
		$user = $this->session->getUser();
115
		if($user !== null) {
116
			$scopes[] = new ScopeContext(IManager::SCOPE_USER, $user->getUID());
117
		}
118
119
		$operations = [];
120
		foreach ($scopes as $scope) {
121
			$operations = array_merge($operations, $this->manager->getOperations($class, $scope));
0 ignored issues
show
Bug introduced by
The method getOperations() does not exist on OCP\WorkflowEngine\IManager. Since it exists in all sub-types, consider adding an abstract or default implementation to OCP\WorkflowEngine\IManager. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

121
			$operations = array_merge($operations, $this->manager->/** @scrutinizer ignore-call */ getOperations($class, $scope));
Loading history...
122
		}
123
124
		if($this->entity instanceof IEntity) {
0 ignored issues
show
introduced by
$this->entity is always a sub-type of OCP\WorkflowEngine\IEntity.
Loading history...
125
			$additionalScopes = $this->manager->getAllConfiguredScopesForOperation($class);
0 ignored issues
show
Bug introduced by
The method getAllConfiguredScopesForOperation() does not exist on OCP\WorkflowEngine\IManager. Since it exists in all sub-types, consider adding an abstract or default implementation to OCP\WorkflowEngine\IManager. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

125
			/** @scrutinizer ignore-call */ 
126
   $additionalScopes = $this->manager->getAllConfiguredScopesForOperation($class);
Loading history...
126
			foreach ($additionalScopes as $hash => $scopeCandidate) {
127
				/** @var ScopeContext $scopeCandidate */
128
				if ($scopeCandidate->getScope() !== IManager::SCOPE_USER || in_array($scopeCandidate, $scopes)) {
129
					continue;
130
				}
131
				if ($this->entity->isLegitimatedForUserId($scopeCandidate->getScopeId())) {
132
					$operations = array_merge($operations, $this->manager->getOperations($class, $scopeCandidate));
133
				}
134
			}
135
		}
136
137
		$matches = [];
138
		foreach ($operations as $operation) {
139
			$checkIds = json_decode($operation['checks'], true);
140
			$checks = $this->manager->getChecks($checkIds);
0 ignored issues
show
Bug introduced by
The method getChecks() does not exist on OCP\WorkflowEngine\IManager. Since it exists in all sub-types, consider adding an abstract or default implementation to OCP\WorkflowEngine\IManager. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

140
			/** @scrutinizer ignore-call */ 
141
   $checks = $this->manager->getChecks($checkIds);
Loading history...
141
142
			foreach ($checks as $check) {
143
				if (!$this->check($check)) {
144
					// Check did not match, continue with the next operation
145
					continue 2;
146
				}
147
			}
148
149
			if ($returnFirstMatchingOperationOnly) {
150
				return $operation;
151
			}
152
			$matches[] = $operation;
153
		}
154
155
		return $matches;
156
	}
157
158
	/**
159
	 * @param array $check
160
	 * @return bool
161
	 */
162
	public function check(array $check) {
163
		try {
164
			$checkInstance = $this->container->query($check['class']);
165
		} catch (QueryException $e) {
166
			// Check does not exist, assume it matches.
167
			return true;
168
		}
169
170
		if ($checkInstance instanceof IFileCheck) {
171
			if (empty($this->fileInfo)) {
172
				throw new RuntimeException('Must set file info before running the check');
173
			}
174
			$checkInstance->setFileInfo($this->fileInfo['storage'], $this->fileInfo['path'], $this->fileInfo['isDir']);
175
		} elseif ($checkInstance instanceof IEntityCheck) {
176
			foreach($this->contexts as $entityInfo) {
177
				list($entity, $subject) = $entityInfo;
178
				$checkInstance->setEntitySubject($entity, $subject);
179
			}
180
		} else if(!$checkInstance instanceof ICheck) {
181
			// Check is invalid
182
			throw new \UnexpectedValueException($this->l->t('Check %s is invalid or does not exist', $check['class']));
183
		}
184
		return $checkInstance->executeCheck($check['operator'], $check['value']);
185
	}
186
}
187