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

AWorkflowController   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 124
Duplicated Lines 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 45
c 2
b 1
f 0
dl 0
loc 124
rs 10
wmc 18

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A create() 0 19 4
A update() 0 19 4
A show() 0 11 2
A index() 0 10 3
A destroy() 0 10 4
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\Controller;
26
27
use Doctrine\DBAL\DBALException;
28
use OCA\WorkflowEngine\Helper\ScopeContext;
29
use OCA\WorkflowEngine\Manager;
30
use OCP\AppFramework\Http\DataResponse;
31
use OCP\AppFramework\OCS\OCSBadRequestException;
32
use OCP\AppFramework\OCS\OCSException;
33
use OCP\AppFramework\OCS\OCSForbiddenException;
34
use OCP\AppFramework\OCSController;
35
use OCP\IRequest;
36
37
abstract class AWorkflowController extends OCSController {
38
39
	/** @var Manager */
40
	protected $manager;
41
42
	public function __construct(
43
		$appName,
44
		IRequest $request,
45
		Manager $manager
46
	) {
47
		parent::__construct($appName, $request);
48
49
		$this->manager = $manager;
50
	}
51
52
	/**
53
	 * @throws OCSForbiddenException
54
	 */
55
	abstract protected function getScopeContext(): ScopeContext;
56
57
	/**
58
	 * Example: curl -u joann -H "OCS-APIREQUEST: true" "http://my.nc.srvr/ocs/v2.php/apps/workflowengine/api/v1/workflows/global?format=json"
59
	 *
60
	 * @throws OCSForbiddenException
61
	 */
62
	public function index(): DataResponse {
63
		$operationsByClass = $this->manager->getAllOperations($this->getScopeContext());
64
65
		foreach ($operationsByClass as &$operations) {
66
			foreach ($operations as &$operation) {
67
				$operation = $this->manager->formatOperation($operation);
68
			}
69
		}
70
71
		return new DataResponse($operationsByClass);
72
	}
73
74
	/**
75
	 * Example: curl -u joann -H "OCS-APIREQUEST: true" "http://my.nc.srvr/ocs/v2.php/apps/workflowengine/api/v1/workflows/global/OCA\\Workflow_DocToPdf\\Operation?format=json"
76
	 *
77
	 * @throws OCSForbiddenException
78
	 */
79
	public function show(string $id): DataResponse {
80
		$context = $this->getScopeContext();
81
82
		// The ID corresponds to a class name
83
		$operations = $this->manager->getOperations($id, $context);
84
85
		foreach ($operations as &$operation) {
86
			$operation = $this->manager->formatOperation($operation);
87
		}
88
89
		return new DataResponse($operations);
90
	}
91
92
	/**
93
	 * @throws OCSBadRequestException
94
	 * @throws OCSForbiddenException
95
	 * @throws OCSException
96
	 */
97
	public function create(
98
		string $class,
99
		string $name,
100
		array $checks,
101
		string $operation,
102
		string $entity,
103
		array $events
104
	): DataResponse {
105
		$context = $this->getScopeContext();
106
		try {
107
			$operation = $this->manager->addOperation($class, $name, $checks, $operation, $context, $entity, $events);
108
			$operation = $this->manager->formatOperation($operation);
109
			return new DataResponse($operation);
110
		} catch (\UnexpectedValueException $e) {
111
			throw new OCSBadRequestException($e->getMessage(), $e);
112
		} catch (\DomainException $e) {
113
			throw new OCSForbiddenException($e->getMessage(), $e);
114
		} catch(DBALException $e) {
115
			throw new OCSException('An internal error occurred', $e->getCode(), $e);
116
		}
117
	}
118
119
	/**
120
	 * @throws OCSBadRequestException
121
	 * @throws OCSForbiddenException
122
	 * @throws OCSException
123
	 */
124
	public function update(
125
		int $id,
126
		string $name,
127
		array $checks,
128
		string $operation,
129
		string $entity,
130
		array $events
131
	): DataResponse {
132
		try {
133
			$context = $this->getScopeContext();
134
			$operation = $this->manager->updateOperation($id, $name, $checks, $operation, $context, $entity, $events);
135
			$operation = $this->manager->formatOperation($operation);
136
			return new DataResponse($operation);
137
		} catch (\UnexpectedValueException $e) {
138
			throw new OCSBadRequestException($e->getMessage(), $e);
139
		} catch (\DomainException $e) {
140
			throw new OCSForbiddenException($e->getMessage(), $e);
141
		} catch(DBALException $e) {
142
			throw new OCSException('An internal error occurred', $e->getCode(), $e);
143
		}
144
	}
145
146
	/**
147
	 * @throws OCSBadRequestException
148
	 * @throws OCSForbiddenException
149
	 * @throws OCSException
150
	 */
151
	public function destroy(int $id): DataResponse {
152
		try {
153
			$deleted = $this->manager->deleteOperation($id, $this->getScopeContext());
154
			return new DataResponse($deleted);
0 ignored issues
show
Bug introduced by
$deleted of type boolean is incompatible with the type array|object expected by parameter $data of OCP\AppFramework\Http\DataResponse::__construct(). ( Ignorable by Annotation )

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

154
			return new DataResponse(/** @scrutinizer ignore-type */ $deleted);
Loading history...
155
		} catch (\UnexpectedValueException $e) {
156
			throw new OCSBadRequestException($e->getMessage(), $e);
157
		} catch (\DomainException $e) {
158
			throw new OCSForbiddenException($e->getMessage(), $e);
159
		} catch(DBALException $e) {
160
			throw new OCSException('An internal error occurred', $e->getCode(), $e);
161
		}
162
	}
163
}
164