Passed
Push — master ( c79f8b...c97e85 )
by Roeland
30:07 queued 17:39
created

Application::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 2
rs 10
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016 Morris Jobke <[email protected]>
4
 *
5
 * @license GNU AGPL version 3 or any later version
6
 *
7
 * This program is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU Affero General Public License as
9
 * published by the Free Software Foundation, either version 3 of the
10
 * License, or (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
 *
20
 */
21
22
namespace OCA\WorkflowEngine\AppInfo;
23
24
use OCA\WorkflowEngine\Controller\RequestTime;
25
use OCA\WorkflowEngine\Helper\LogContext;
26
use OCA\WorkflowEngine\Listener\LoadAdditionalSettingsScriptsListener;
27
use OCA\WorkflowEngine\Manager;
28
use OCA\WorkflowEngine\Service\Logger;
29
use OCP\AppFramework\App;
30
use OCP\AppFramework\Bootstrap\IBootContext;
31
use OCP\AppFramework\Bootstrap\IBootstrap;
32
use OCP\AppFramework\Bootstrap\IRegistrationContext;
33
use OCP\AppFramework\QueryException;
34
use OCP\EventDispatcher\Event;
35
use OCP\EventDispatcher\IEventDispatcher;
36
use OCP\ILogger;
37
use OCP\IServerContainer;
38
use OCP\WorkflowEngine\IEntity;
39
use OCP\WorkflowEngine\IEntityCompat;
40
use OCP\WorkflowEngine\IOperation;
41
use OCP\WorkflowEngine\IOperationCompat;
42
43
class Application extends App implements IBootstrap {
44
	public const APP_ID = 'workflowengine';
45
46
	public function __construct() {
47
		parent::__construct(self::APP_ID);
48
	}
49
50
	public function register(IRegistrationContext $context): void {
51
		$context->registerServiceAlias('RequestTimeController', RequestTime::class);
52
		$context->registerEventListener(
53
			'OCP\WorkflowEngine::loadAdditionalSettingScripts',
54
			LoadAdditionalSettingsScriptsListener::class,
55
			-100
56
		);
57
	}
58
59
	public function boot(IBootContext $context): void {
60
		$this->registerRuleListeners(
61
			$context->getAppContainer()->query(IEventDispatcher::class),
62
			$context->getServerContainer(),
63
			$context->getAppContainer()->query(ILogger::class)
64
		);
65
	}
66
67
	private function registerRuleListeners(IEventDispatcher $dispatcher,
68
										   IServerContainer $container,
69
										   ILogger $logger): void {
70
		$manager = $container->query(Manager::class);
71
		$configuredEvents = $manager->getAllConfiguredEvents();
72
73
		foreach ($configuredEvents as $operationClass => $events) {
74
			foreach ($events as $entityClass => $eventNames) {
75
				array_map(function (string $eventName) use ($manager, $container, $dispatcher, $logger, $operationClass, $entityClass) {
76
					$dispatcher->addListener(
77
						$eventName,
78
						function ($event) use ($manager, $container, $eventName, $logger, $operationClass, $entityClass) {
79
							$ruleMatcher = $manager->getRuleMatcher();
80
							try {
81
								/** @var IEntity $entity */
82
								$entity = $container->query($entityClass);
83
								/** @var IOperation $operation */
84
								$operation = $container->query($operationClass);
85
86
								$ruleMatcher->setEntity($entity);
87
								$ruleMatcher->setOperation($operation);
88
89
								$ctx = new LogContext();
90
								$ctx
91
									->setOperation($operation)
92
									->setEntity($entity)
93
									->setEventName($eventName);
94
95
								/** @var Logger $flowLogger */
96
								$flowLogger = $container->query(Logger::class);
97
								$flowLogger->logEventInit($ctx);
98
99
								if ($event instanceof Event) {
100
									$entity->prepareRuleMatcher($ruleMatcher, $eventName, $event);
101
									$operation->onEvent($eventName, $event, $ruleMatcher);
102
								} elseif ($entity instanceof IEntityCompat && $operation instanceof IOperationCompat) {
103
									// TODO: Remove this block (and the compat classes) in the first major release in 2023
104
									$entity->prepareRuleMatcherCompat($ruleMatcher, $eventName, $event);
0 ignored issues
show
Deprecated Code introduced by
The function OCP\WorkflowEngine\IEnti...pareRuleMatcherCompat() has been deprecated. ( Ignorable by Annotation )

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

104
									/** @scrutinizer ignore-deprecated */ $entity->prepareRuleMatcherCompat($ruleMatcher, $eventName, $event);
Loading history...
105
									$operation->onEventCompat($eventName, $event, $ruleMatcher);
0 ignored issues
show
Deprecated Code introduced by
The function OCP\WorkflowEngine\IOper...Compat::onEventCompat() has been deprecated. ( Ignorable by Annotation )

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

105
									/** @scrutinizer ignore-deprecated */ $operation->onEventCompat($eventName, $event, $ruleMatcher);
Loading history...
106
								} else {
107
									$logger->debug(
108
										'Cannot handle event {name} of {event} against entity {entity} and operation {operation}',
109
										[
110
											'app' => self::APP_ID,
111
											'name' => $eventName,
112
											'event' => get_class($event),
113
											'entity' => $entityClass,
114
											'operation' => $operationClass,
115
										]
116
									);
117
								}
118
								$flowLogger->logEventDone($ctx);
119
							} catch (QueryException $e) {
120
								// Ignore query exceptions since they might occur when an entity/operation were setup before by an app that is disabled now
121
							}
122
						}
123
					);
124
				}, $eventNames ?? []);
125
			}
126
		}
127
	}
128
}
129