Passed
Push — master ( b552da...09c1d8 )
by Roeland
12:09 queued 12s
created

RuleMatcher::getEntity()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 5
rs 10
cc 2
nc 2
nop 0
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): void {
75
		$this->fileInfo['storage'] = $storage;
76
		$this->fileInfo['path'] = $path;
77
	}
78
79
	public function setEntitySubject(IEntity $entity, $subject): void {
80
		$this->contexts[get_class($entity)] = [$entity, $subject];
81
	}
82
83
	public function setOperation(IOperation $operation): void {
84
		if($this->operation !== null) {
85
			throw new RuntimeException('This method must not be called more than once');
86
		}
87
		$this->operation = $operation;
88
	}
89
90
	public function setEntity(IEntity $entity): void {
91
		if($this->entity !== null) {
92
			throw new RuntimeException('This method must not be called more than once');
93
		}
94
		$this->entity = $entity;
95
	}
96
97
	public function getEntity(): IEntity {
98
		if($this->entity === null) {
99
			throw new \LogicException('Entity was not set yet');
100
		}
101
		return $this->entity;
102
	}
103
104
	public function getFlows(bool $returnFirstMatchingOperationOnly = true): array {
105
		if(!$this->operation) {
106
			throw new RuntimeException('Operation is not set');
107
		}
108
		return $this->getMatchingOperations(get_class($this->operation), $returnFirstMatchingOperationOnly);
109
	}
110
111
	public function getMatchingOperations(string $class, bool $returnFirstMatchingOperationOnly = true): array {
112
		$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...
113
		$user = $this->session->getUser();
114
		if($user !== null) {
115
			$scopes[] = new ScopeContext(IManager::SCOPE_USER, $user->getUID());
116
		}
117
118
		$operations = [];
119
		foreach ($scopes as $scope) {
120
			$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

120
			$operations = array_merge($operations, $this->manager->/** @scrutinizer ignore-call */ getOperations($class, $scope));
Loading history...
121
		}
122
123
		$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

123
		/** @scrutinizer ignore-call */ 
124
  $additionalScopes = $this->manager->getAllConfiguredScopesForOperation($class);
Loading history...
124
		foreach ($additionalScopes as $hash => $scopeCandidate) {
125
			/** @var ScopeContext $scopeCandidate */
126
			if ($scopeCandidate->getScope() !== IManager::SCOPE_USER) {
127
				continue;
128
			}
129
			if ($this->entity->isLegitimatedForUserId($scopeCandidate->getScopeId())) {
130
				$operations = array_merge($operations, $this->manager->getOperations($class, $scopeCandidate));
131
			}
132
		}
133
134
		$matches = [];
135
		foreach ($operations as $operation) {
136
			$checkIds = json_decode($operation['checks'], true);
137
			$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

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