Passed
Push — master ( 11341e...e193e1 )
by Roeland
35:40 queued 24:02
created
apps/workflowengine/lib/Service/RuleMatcher.php 1 patch
Indentation   +188 added lines, -188 removed lines patch added patch discarded remove patch
@@ -43,192 +43,192 @@
 block discarded – undo
43 43
 
44 44
 class RuleMatcher implements IRuleMatcher {
45 45
 
46
-	/** @var IUserSession */
47
-	protected $session;
48
-	/** @var IManager */
49
-	protected $manager;
50
-	/** @var array */
51
-	protected $contexts;
52
-	/** @var IServerContainer */
53
-	protected $container;
54
-	/** @var array */
55
-	protected $fileInfo = [];
56
-	/** @var IL10N */
57
-	protected $l;
58
-	/** @var IOperation */
59
-	protected $operation;
60
-	/** @var IEntity */
61
-	protected $entity;
62
-	/** @var Logger */
63
-	protected $logger;
64
-
65
-	public function __construct(
66
-		IUserSession $session,
67
-		IServerContainer $container,
68
-		IL10N $l,
69
-		Manager $manager,
70
-		Logger $logger
71
-	) {
72
-		$this->session = $session;
73
-		$this->manager = $manager;
74
-		$this->container = $container;
75
-		$this->l = $l;
76
-		$this->logger = $logger;
77
-	}
78
-
79
-	public function setFileInfo(IStorage $storage, string $path, bool $isDir = false): void {
80
-		$this->fileInfo['storage'] = $storage;
81
-		$this->fileInfo['path'] = $path;
82
-		$this->fileInfo['isDir'] = $isDir;
83
-	}
84
-
85
-	public function setEntitySubject(IEntity $entity, $subject): void {
86
-		$this->contexts[get_class($entity)] = [$entity, $subject];
87
-	}
88
-
89
-	public function setOperation(IOperation $operation): void {
90
-		if($this->operation !== null) {
91
-			throw new RuntimeException('This method must not be called more than once');
92
-		}
93
-		$this->operation = $operation;
94
-	}
95
-
96
-	public function setEntity(IEntity $entity): void {
97
-		if($this->entity !== null) {
98
-			throw new RuntimeException('This method must not be called more than once');
99
-		}
100
-		$this->entity = $entity;
101
-	}
102
-
103
-	public function getEntity(): IEntity {
104
-		if($this->entity === null) {
105
-			throw new \LogicException('Entity was not set yet');
106
-		}
107
-		return $this->entity;
108
-	}
109
-
110
-	public function getFlows(bool $returnFirstMatchingOperationOnly = true): array {
111
-		if(!$this->operation) {
112
-			throw new RuntimeException('Operation is not set');
113
-		}
114
-		return $this->getMatchingOperations(get_class($this->operation), $returnFirstMatchingOperationOnly);
115
-	}
116
-
117
-	public function getMatchingOperations(string $class, bool $returnFirstMatchingOperationOnly = true): array {
118
-		$scopes[] = new ScopeContext(IManager::SCOPE_ADMIN);
119
-		$user = $this->session->getUser();
120
-		if($user !== null) {
121
-			$scopes[] = new ScopeContext(IManager::SCOPE_USER, $user->getUID());
122
-		}
123
-
124
-		$ctx = new LogContext();
125
-		$ctx
126
-			->setScopes($scopes)
127
-			->setEntity($this->entity)
128
-			->setOperation($this->operation);
129
-		$this->logger->logFlowRequests($ctx);
130
-
131
-		$operations = [];
132
-		foreach ($scopes as $scope) {
133
-			$operations = array_merge($operations, $this->manager->getOperations($class, $scope));
134
-		}
135
-
136
-		if($this->entity instanceof IEntity) {
137
-			$additionalScopes = $this->manager->getAllConfiguredScopesForOperation($class);
138
-			foreach ($additionalScopes as $hash => $scopeCandidate) {
139
-				/** @var ScopeContext $scopeCandidate */
140
-				if ($scopeCandidate->getScope() !== IManager::SCOPE_USER || in_array($scopeCandidate, $scopes)) {
141
-					continue;
142
-				}
143
-				if ($this->entity->isLegitimatedForUserId($scopeCandidate->getScopeId())) {
144
-					$ctx = new LogContext();
145
-					$ctx
146
-						->setScopes([$scopeCandidate])
147
-						->setEntity($this->entity)
148
-						->setOperation($this->operation);
149
-					$this->logger->logScopeExpansion($ctx);
150
-					$operations = array_merge($operations, $this->manager->getOperations($class, $scopeCandidate));
151
-				}
152
-			}
153
-		}
154
-
155
-		$matches = [];
156
-		foreach ($operations as $operation) {
157
-			$checkIds = json_decode($operation['checks'], true);
158
-			$checks = $this->manager->getChecks($checkIds);
159
-
160
-			foreach ($checks as $check) {
161
-				if (!$this->check($check)) {
162
-					// Check did not match, continue with the next operation
163
-					continue 2;
164
-				}
165
-			}
166
-
167
-			$ctx = new LogContext();
168
-			$ctx
169
-				->setEntity($this->entity)
170
-				->setOperation($this->operation)
171
-				->setConfiguration($operation);
172
-			$this->logger->logPassedCheck($ctx);
173
-
174
-			if ($returnFirstMatchingOperationOnly) {
175
-				$ctx = new LogContext();
176
-				$ctx
177
-					->setEntity($this->entity)
178
-					->setOperation($this->operation)
179
-					->setConfiguration($operation);
180
-				$this->logger->logRunSingle($ctx);
181
-				return $operation;
182
-			}
183
-			$matches[] = $operation;
184
-		}
185
-
186
-		$ctx = new LogContext();
187
-		$ctx
188
-			->setEntity($this->entity)
189
-			->setOperation($this->operation);
190
-		if(!empty($matches)) {
191
-			$ctx->setConfiguration($matches);
192
-			$this->logger->logRunAll($ctx);
193
-		} else {
194
-			$this->logger->logRunNone($ctx);
195
-		}
196
-
197
-		return $matches;
198
-	}
199
-
200
-	/**
201
-	 * @param array $check
202
-	 * @return bool
203
-	 */
204
-	public function check(array $check) {
205
-		try {
206
-			$checkInstance = $this->container->query($check['class']);
207
-		} catch (QueryException $e) {
208
-			// Check does not exist, assume it matches.
209
-			return true;
210
-		}
211
-
212
-		if ($checkInstance instanceof IFileCheck) {
213
-			if (empty($this->fileInfo)) {
214
-				throw new RuntimeException('Must set file info before running the check');
215
-			}
216
-			$checkInstance->setFileInfo($this->fileInfo['storage'], $this->fileInfo['path'], $this->fileInfo['isDir']);
217
-		} elseif ($checkInstance instanceof IEntityCheck) {
218
-			foreach($this->contexts as $entityInfo) {
219
-				list($entity, $subject) = $entityInfo;
220
-				$checkInstance->setEntitySubject($entity, $subject);
221
-			}
222
-		} else if(!$checkInstance instanceof ICheck) {
223
-			// Check is invalid
224
-			throw new \UnexpectedValueException($this->l->t('Check %s is invalid or does not exist', $check['class']));
225
-		}
226
-		return $checkInstance->executeCheck($check['operator'], $check['value']);
227
-	}
228
-
229
-	protected function logCandidate() {
230
-		$logContext = new LogContext();
231
-		$logContext
232
-			->setOperation();
233
-	}
46
+    /** @var IUserSession */
47
+    protected $session;
48
+    /** @var IManager */
49
+    protected $manager;
50
+    /** @var array */
51
+    protected $contexts;
52
+    /** @var IServerContainer */
53
+    protected $container;
54
+    /** @var array */
55
+    protected $fileInfo = [];
56
+    /** @var IL10N */
57
+    protected $l;
58
+    /** @var IOperation */
59
+    protected $operation;
60
+    /** @var IEntity */
61
+    protected $entity;
62
+    /** @var Logger */
63
+    protected $logger;
64
+
65
+    public function __construct(
66
+        IUserSession $session,
67
+        IServerContainer $container,
68
+        IL10N $l,
69
+        Manager $manager,
70
+        Logger $logger
71
+    ) {
72
+        $this->session = $session;
73
+        $this->manager = $manager;
74
+        $this->container = $container;
75
+        $this->l = $l;
76
+        $this->logger = $logger;
77
+    }
78
+
79
+    public function setFileInfo(IStorage $storage, string $path, bool $isDir = false): void {
80
+        $this->fileInfo['storage'] = $storage;
81
+        $this->fileInfo['path'] = $path;
82
+        $this->fileInfo['isDir'] = $isDir;
83
+    }
84
+
85
+    public function setEntitySubject(IEntity $entity, $subject): void {
86
+        $this->contexts[get_class($entity)] = [$entity, $subject];
87
+    }
88
+
89
+    public function setOperation(IOperation $operation): void {
90
+        if($this->operation !== null) {
91
+            throw new RuntimeException('This method must not be called more than once');
92
+        }
93
+        $this->operation = $operation;
94
+    }
95
+
96
+    public function setEntity(IEntity $entity): void {
97
+        if($this->entity !== null) {
98
+            throw new RuntimeException('This method must not be called more than once');
99
+        }
100
+        $this->entity = $entity;
101
+    }
102
+
103
+    public function getEntity(): IEntity {
104
+        if($this->entity === null) {
105
+            throw new \LogicException('Entity was not set yet');
106
+        }
107
+        return $this->entity;
108
+    }
109
+
110
+    public function getFlows(bool $returnFirstMatchingOperationOnly = true): array {
111
+        if(!$this->operation) {
112
+            throw new RuntimeException('Operation is not set');
113
+        }
114
+        return $this->getMatchingOperations(get_class($this->operation), $returnFirstMatchingOperationOnly);
115
+    }
116
+
117
+    public function getMatchingOperations(string $class, bool $returnFirstMatchingOperationOnly = true): array {
118
+        $scopes[] = new ScopeContext(IManager::SCOPE_ADMIN);
119
+        $user = $this->session->getUser();
120
+        if($user !== null) {
121
+            $scopes[] = new ScopeContext(IManager::SCOPE_USER, $user->getUID());
122
+        }
123
+
124
+        $ctx = new LogContext();
125
+        $ctx
126
+            ->setScopes($scopes)
127
+            ->setEntity($this->entity)
128
+            ->setOperation($this->operation);
129
+        $this->logger->logFlowRequests($ctx);
130
+
131
+        $operations = [];
132
+        foreach ($scopes as $scope) {
133
+            $operations = array_merge($operations, $this->manager->getOperations($class, $scope));
134
+        }
135
+
136
+        if($this->entity instanceof IEntity) {
137
+            $additionalScopes = $this->manager->getAllConfiguredScopesForOperation($class);
138
+            foreach ($additionalScopes as $hash => $scopeCandidate) {
139
+                /** @var ScopeContext $scopeCandidate */
140
+                if ($scopeCandidate->getScope() !== IManager::SCOPE_USER || in_array($scopeCandidate, $scopes)) {
141
+                    continue;
142
+                }
143
+                if ($this->entity->isLegitimatedForUserId($scopeCandidate->getScopeId())) {
144
+                    $ctx = new LogContext();
145
+                    $ctx
146
+                        ->setScopes([$scopeCandidate])
147
+                        ->setEntity($this->entity)
148
+                        ->setOperation($this->operation);
149
+                    $this->logger->logScopeExpansion($ctx);
150
+                    $operations = array_merge($operations, $this->manager->getOperations($class, $scopeCandidate));
151
+                }
152
+            }
153
+        }
154
+
155
+        $matches = [];
156
+        foreach ($operations as $operation) {
157
+            $checkIds = json_decode($operation['checks'], true);
158
+            $checks = $this->manager->getChecks($checkIds);
159
+
160
+            foreach ($checks as $check) {
161
+                if (!$this->check($check)) {
162
+                    // Check did not match, continue with the next operation
163
+                    continue 2;
164
+                }
165
+            }
166
+
167
+            $ctx = new LogContext();
168
+            $ctx
169
+                ->setEntity($this->entity)
170
+                ->setOperation($this->operation)
171
+                ->setConfiguration($operation);
172
+            $this->logger->logPassedCheck($ctx);
173
+
174
+            if ($returnFirstMatchingOperationOnly) {
175
+                $ctx = new LogContext();
176
+                $ctx
177
+                    ->setEntity($this->entity)
178
+                    ->setOperation($this->operation)
179
+                    ->setConfiguration($operation);
180
+                $this->logger->logRunSingle($ctx);
181
+                return $operation;
182
+            }
183
+            $matches[] = $operation;
184
+        }
185
+
186
+        $ctx = new LogContext();
187
+        $ctx
188
+            ->setEntity($this->entity)
189
+            ->setOperation($this->operation);
190
+        if(!empty($matches)) {
191
+            $ctx->setConfiguration($matches);
192
+            $this->logger->logRunAll($ctx);
193
+        } else {
194
+            $this->logger->logRunNone($ctx);
195
+        }
196
+
197
+        return $matches;
198
+    }
199
+
200
+    /**
201
+     * @param array $check
202
+     * @return bool
203
+     */
204
+    public function check(array $check) {
205
+        try {
206
+            $checkInstance = $this->container->query($check['class']);
207
+        } catch (QueryException $e) {
208
+            // Check does not exist, assume it matches.
209
+            return true;
210
+        }
211
+
212
+        if ($checkInstance instanceof IFileCheck) {
213
+            if (empty($this->fileInfo)) {
214
+                throw new RuntimeException('Must set file info before running the check');
215
+            }
216
+            $checkInstance->setFileInfo($this->fileInfo['storage'], $this->fileInfo['path'], $this->fileInfo['isDir']);
217
+        } elseif ($checkInstance instanceof IEntityCheck) {
218
+            foreach($this->contexts as $entityInfo) {
219
+                list($entity, $subject) = $entityInfo;
220
+                $checkInstance->setEntitySubject($entity, $subject);
221
+            }
222
+        } else if(!$checkInstance instanceof ICheck) {
223
+            // Check is invalid
224
+            throw new \UnexpectedValueException($this->l->t('Check %s is invalid or does not exist', $check['class']));
225
+        }
226
+        return $checkInstance->executeCheck($check['operator'], $check['value']);
227
+    }
228
+
229
+    protected function logCandidate() {
230
+        $logContext = new LogContext();
231
+        $logContext
232
+            ->setOperation();
233
+    }
234 234
 }
Please login to merge, or discard this patch.