Passed
Push — master ( 64bfd4...13960b )
by John
11:26 queued 10s
created

RuleMatcher::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 4
dl 0
loc 5
rs 10
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\IRuleMatcher;
40
41
class RuleMatcher implements IRuleMatcher {
42
43
	/** @var IUserSession */
44
	protected $session;
45
	/** @var IManager */
46
	protected $manager;
47
	/** @var array */
48
	protected $contexts;
49
	/** @var IServerContainer */
50
	protected $container;
51
	/** @var array */
52
	protected $fileInfo = [];
53
	/** @var IL10N */
54
	protected $l;
55
56
	public function __construct(IUserSession $session, IServerContainer $container, IL10N $l, Manager $manager) {
57
		$this->session = $session;
58
		$this->manager = $manager;
59
		$this->container = $container;
60
		$this->l = $l;
61
	}
62
63
	public function setFileInfo(IStorage $storage, string $path): void {
64
		$this->fileInfo['storage'] = $storage;
65
		$this->fileInfo['path'] = $path;
66
	}
67
68
69
	public function setEntitySubject(IEntity $entity, $subject): void {
70
		$this->contexts[get_class($entity)] = [$entity, $subject];
71
	}
72
73
	public function getMatchingOperations(string $class, bool $returnFirstMatchingOperationOnly = true): array {
74
		$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...
75
		$user = $this->session->getUser();
76
		if($user !== null) {
77
			$scopes[] = new ScopeContext(IManager::SCOPE_USER, $user->getUID());
78
		}
79
80
		$operations = [];
81
		foreach ($scopes as $scope) {
82
			$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

82
			$operations = array_merge($operations, $this->manager->/** @scrutinizer ignore-call */ getOperations($class, $scope));
Loading history...
83
		}
84
85
		$matches = [];
86
		foreach ($operations as $operation) {
87
			$checkIds = json_decode($operation['checks'], true);
88
			$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

88
			/** @scrutinizer ignore-call */ 
89
   $checks = $this->manager->getChecks($checkIds);
Loading history...
89
90
			foreach ($checks as $check) {
91
				if (!$this->check($check)) {
92
					// Check did not match, continue with the next operation
93
					continue 2;
94
				}
95
			}
96
97
			if ($returnFirstMatchingOperationOnly) {
98
				return $operation;
99
			}
100
			$matches[] = $operation;
101
		}
102
103
		return $matches;
104
	}
105
106
	/**
107
	 * @param array $check
108
	 * @return bool
109
	 */
110
	public function check(array $check) {
111
		try {
112
			$checkInstance = $this->container->query($check['class']);
113
		} catch (QueryException $e) {
114
			// Check does not exist, assume it matches.
115
			return true;
116
		}
117
118
		if ($checkInstance instanceof IFileCheck) {
119
			if (empty($this->fileInfo)) {
120
				throw new \RuntimeException('Must set file info before running the check');
121
			}
122
			$checkInstance->setFileInfo($this->fileInfo['storage'], $this->fileInfo['path']);
123
		} elseif ($checkInstance instanceof IEntityCheck) {
124
			foreach($this->contexts as $entityInfo) {
125
				list($entity, $subject) = $entityInfo;
126
				$checkInstance->setEntitySubject($entity, $subject);
127
			}
128
		} else if(!$checkInstance instanceof ICheck) {
129
			// Check is invalid
130
			throw new \UnexpectedValueException($this->l->t('Check %s is invalid or does not exist', $check['class']));
131
		}
132
		return $checkInstance->executeCheck($check['operator'], $check['value']);
133
	}
134
}
135