Passed
Push — master ( 2187f8...15d39c )
by Joas
11:45 queued 12s
created

ASettings::operatorsToArray()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 16
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 3
eloc 12
c 1
b 0
f 1
nc 1
nop 1
dl 0
loc 16
rs 9.8666
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\Settings;
26
27
use OCA\WorkflowEngine\AppInfo\Application;
28
use OCA\WorkflowEngine\Manager;
29
use OCP\AppFramework\Http\TemplateResponse;
30
use OCP\IInitialStateService;
31
use OCP\IL10N;
32
use OCP\Settings\ISettings;
33
use OCP\WorkflowEngine\ICheck;
34
use OCP\WorkflowEngine\IComplexOperation;
35
use OCP\WorkflowEngine\IEntity;
36
use OCP\WorkflowEngine\IEntityEvent;
37
use OCP\WorkflowEngine\IOperation;
38
use OCP\WorkflowEngine\ISpecificOperation;
39
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
40
41
abstract class ASettings implements ISettings {
42
	/** @var IL10N */
43
	private $l10n;
44
45
	/** @var string */
46
	private $appName;
47
48
	/** @var EventDispatcherInterface */
49
	private $eventDispatcher;
50
51
	/** @var Manager */
52
	private $manager;
53
54
	/** @var IInitialStateService */
55
	private $initialStateService;
56
57
	/**
58
	 * @param string $appName
59
	 * @param IL10N $l
60
	 * @param EventDispatcherInterface $eventDispatcher
61
	 */
62
	public function __construct(
63
		$appName,
64
		IL10N $l,
65
		EventDispatcherInterface $eventDispatcher,
66
		Manager $manager,
67
		IInitialStateService $initialStateService
68
	) {
69
		$this->appName = $appName;
70
		$this->l10n = $l;
71
		$this->eventDispatcher = $eventDispatcher;
72
		$this->manager = $manager;
73
		$this->initialStateService = $initialStateService;
74
	}
75
76
	abstract function getScope(): int;
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
77
78
	/**
79
	 * @return TemplateResponse
80
	 */
81
	public function getForm() {
82
		$this->eventDispatcher->dispatch('OCP\WorkflowEngine::loadAdditionalSettingScripts');
0 ignored issues
show
Bug introduced by
'OCP\WorkflowEngine::loa...ditionalSettingScripts' of type string is incompatible with the type object expected by parameter $event of Symfony\Contracts\EventD...erInterface::dispatch(). ( Ignorable by Annotation )

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

82
		$this->eventDispatcher->dispatch(/** @scrutinizer ignore-type */ 'OCP\WorkflowEngine::loadAdditionalSettingScripts');
Loading history...
83
84
		$entities = $this->manager->getEntitiesList();
85
		$this->initialStateService->provideInitialState(
86
			Application::APP_ID,
87
			'entities',
88
			$this->entitiesToArray($entities)
89
		);
90
91
		$operators = $this->manager->getOperatorList();
92
		$this->initialStateService->provideInitialState(
93
			Application::APP_ID,
94
			'operators',
95
			$this->operatorsToArray($operators)
96
		);
97
98
		$checks = $this->manager->getCheckList();
99
		$this->initialStateService->provideInitialState(
100
			Application::APP_ID,
101
			'checks',
102
			$this->checksToArray($checks)
103
		);
104
105
		$this->initialStateService->provideInitialState(
106
			Application::APP_ID,
107
			'scope',
108
			$this->getScope()
109
		);
110
111
		return new TemplateResponse(Application::APP_ID, 'settings', [], 'blank');
112
	}
113
114
	/**
115
	 * @return string the section ID, e.g. 'sharing'
116
	 */
117
	public function getSection() {
118
		return 'workflow';
119
	}
120
121
	/**
122
	 * @return int whether the form should be rather on the top or bottom of
123
	 * the admin section. The forms are arranged in ascending order of the
124
	 * priority values. It is required to return a value between 0 and 100.
125
	 *
126
	 * E.g.: 70
127
	 */
128
	public function getPriority() {
129
		return 0;
130
	}
131
132
	private function entitiesToArray(array $entities) {
133
		return array_map(function (IEntity $entity) {
134
			$events = array_map(function(IEntityEvent $entityEvent) {
135
				return [
136
					'eventName' => $entityEvent->getEventName(),
137
					'displayName' => $entityEvent->getDisplayName()
138
				];
139
			}, $entity->getEvents());
140
141
			return [
142
				'id' => get_class($entity),
143
				'icon' => $entity->getIcon(),
144
				'name' => $entity->getName(),
145
				'events' => $events,
146
			];
147
		}, $entities);
148
	}
149
150
	private function operatorsToArray(array $operators) {
151
		$operators = array_filter($operators, function(IOperation $operator) {
152
			return $operator->isAvailableForScope($this->getScope());
153
		});
154
155
		return array_map(function (IOperation $operator) {
156
			return [
157
				'id' => get_class($operator),
158
				'icon' => $operator->getIcon(),
159
				'name' => $operator->getDisplayName(),
160
				'description' => $operator->getDescription(),
161
				'fixedEntity' => $operator instanceof ISpecificOperation ? $operator->getEntityId() : '',
162
				'isComplex' => $operator instanceof IComplexOperation,
163
				'triggerHint' => $operator instanceof IComplexOperation ? $operator->getTriggerHint() : '',
164
			];
165
		}, $operators);
166
	}
167
168
	private function checksToArray(array $checks) {
169
		$checks = array_filter($checks, function(ICheck $check) {
170
			return $check->isAvailableForScope($this->getScope());
171
		});
172
173
		return array_map(function (ICheck $check) {
174
			return [
175
				'id' => get_class($check),
176
				'supportedEntities' => $check->supportedEntities(),
177
			];
178
		}, $checks);
179
	}
180
}
181