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\Controller; |
23
|
|
|
|
24
|
|
|
use OCA\WorkflowEngine\Manager; |
25
|
|
|
use OCP\AppFramework\Controller; |
26
|
|
|
use OCP\AppFramework\Http; |
27
|
|
|
use OCP\AppFramework\Http\JSONResponse; |
28
|
|
|
use OCP\IRequest; |
29
|
|
|
|
30
|
|
|
class FlowOperations extends Controller { |
31
|
|
|
|
32
|
|
|
/** @var Manager */ |
33
|
|
|
protected $manager; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param IRequest $request |
37
|
|
|
* @param Manager $manager |
38
|
|
|
*/ |
39
|
|
|
public function __construct(IRequest $request, Manager $manager) { |
40
|
|
|
parent::__construct('workflowengine', $request); |
41
|
|
|
$this->manager = $manager; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @NoCSRFRequired |
46
|
|
|
* |
47
|
|
|
* @param string $class |
48
|
|
|
* @return JSONResponse |
49
|
|
|
*/ |
50
|
|
|
public function getOperations($class) { |
51
|
|
|
$operations = $this->manager->getOperations($class); |
52
|
|
|
|
53
|
|
|
foreach ($operations as &$operation) { |
54
|
|
|
$operation = $this->prepareOperation($operation); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return new JSONResponse($operations); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param string $class |
62
|
|
|
* @param string $name |
63
|
|
|
* @param array[] $checks |
64
|
|
|
* @param string $operation |
65
|
|
|
* @return JSONResponse The added element |
66
|
|
|
*/ |
67
|
|
View Code Duplication |
public function addOperation($class, $name, $checks, $operation) { |
|
|
|
|
68
|
|
|
try { |
69
|
|
|
$operation = $this->manager->addOperation($class, $name, $checks, $operation); |
70
|
|
|
$operation = $this->prepareOperation($operation); |
71
|
|
|
return new JSONResponse($operation); |
72
|
|
|
} catch (\UnexpectedValueException $e) { |
73
|
|
|
return new JSONResponse($e->getMessage(), Http::STATUS_BAD_REQUEST); |
|
|
|
|
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param int $id |
79
|
|
|
* @param string $name |
80
|
|
|
* @param array[] $checks |
81
|
|
|
* @param string $operation |
82
|
|
|
* @return JSONResponse The updated element |
83
|
|
|
*/ |
84
|
|
View Code Duplication |
public function updateOperation($id, $name, $checks, $operation) { |
|
|
|
|
85
|
|
|
try { |
86
|
|
|
$operation = $this->manager->updateOperation($id, $name, $checks, $operation); |
87
|
|
|
$operation = $this->prepareOperation($operation); |
88
|
|
|
return new JSONResponse($operation); |
89
|
|
|
} catch (\UnexpectedValueException $e) { |
90
|
|
|
return new JSONResponse($e->getMessage(), Http::STATUS_BAD_REQUEST); |
|
|
|
|
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param int $id |
96
|
|
|
* @return JSONResponse |
97
|
|
|
*/ |
98
|
|
|
public function deleteOperation($id) { |
99
|
|
|
$deleted = $this->manager->deleteOperation((int) $id); |
100
|
|
|
return new JSONResponse($deleted); |
|
|
|
|
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param array $operation |
105
|
|
|
* @return array |
106
|
|
|
*/ |
107
|
|
|
protected function prepareOperation(array $operation) { |
108
|
|
|
$checkIds = json_decode($operation['checks']); |
109
|
|
|
$checks = $this->manager->getChecks($checkIds); |
110
|
|
|
|
111
|
|
|
$operation['checks'] = []; |
112
|
|
|
foreach ($checks as $check) { |
113
|
|
|
// Remove internal values |
114
|
|
|
unset($check['id']); |
115
|
|
|
unset($check['hash']); |
116
|
|
|
|
117
|
|
|
$operation['checks'][] = $check; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
return $operation; |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.