Completed
Push — master ( 72ae75...318d68 )
by Joas
36:13 queued 23:35
created

FlowOperations::addOperation()   A

Complexity

Conditions 2
Paths 4

Size

Total Lines 9
Code Lines 7

Duplication

Lines 9
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 7
c 1
b 0
f 1
nc 4
nop 4
dl 9
loc 9
rs 9.6666
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
use OCP\WorkflowEngine\RegisterCheckEvent;
30
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
31
32
class FlowOperations extends Controller {
33
34
	/** @var Manager */
35
	protected $manager;
36
37
	/** @var EventDispatcherInterface */
38
	protected $dispatcher;
39
40
	/**
41
	 * @param IRequest $request
42
	 * @param Manager $manager
43
	 * @param EventDispatcherInterface $dispatcher
44
	 */
45
	public function __construct(IRequest $request, Manager $manager, EventDispatcherInterface $dispatcher) {
46
		parent::__construct('workflowengine', $request);
47
		$this->manager = $manager;
48
		$this->dispatcher = $dispatcher;
49
	}
50
51
	/**
52
	 * @NoCSRFRequired
53
	 *
54
	 * @return JSONResponse
55
	 */
56
	public function getChecks() {
57
		$event = new RegisterCheckEvent();
58
		$this->dispatcher->dispatch('OCP\WorkflowEngine\RegisterCheckEvent', $event);
59
60
		return new JSONResponse($event->getChecks());
61
	}
62
63
	/**
64
	 * @NoCSRFRequired
65
	 *
66
	 * @param string $class
67
	 * @return JSONResponse
68
	 */
69
	public function getOperations($class) {
70
		$operations = $this->manager->getOperations($class);
71
72
		foreach ($operations as &$operation) {
73
			$operation = $this->prepareOperation($operation);
74
		}
75
76
		return new JSONResponse($operations);
77
	}
78
79
	/**
80
	 * @param string $class
81
	 * @param string $name
82
	 * @param array[] $checks
83
	 * @param string $operation
84
	 * @return JSONResponse The added element
85
	 */
86 View Code Duplication
	public function addOperation($class, $name, $checks, $operation) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
87
		try {
88
			$operation = $this->manager->addOperation($class, $name, $checks, $operation);
89
			$operation = $this->prepareOperation($operation);
90
			return new JSONResponse($operation);
91
		} catch (\UnexpectedValueException $e) {
92
			return new JSONResponse($e->getMessage(), Http::STATUS_BAD_REQUEST);
0 ignored issues
show
Documentation introduced by
$e->getMessage() is of type string, but the function expects a array|object.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
93
		}
94
	}
95
96
	/**
97
	 * @param int $id
98
	 * @param string $name
99
	 * @param array[] $checks
100
	 * @param string $operation
101
	 * @return JSONResponse The updated element
102
	 */
103 View Code Duplication
	public function updateOperation($id, $name, $checks, $operation) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
104
		try {
105
			$operation = $this->manager->updateOperation($id, $name, $checks, $operation);
106
			$operation = $this->prepareOperation($operation);
107
			return new JSONResponse($operation);
108
		} catch (\UnexpectedValueException $e) {
109
			return new JSONResponse($e->getMessage(), Http::STATUS_BAD_REQUEST);
0 ignored issues
show
Documentation introduced by
$e->getMessage() is of type string, but the function expects a array|object.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
110
		}
111
	}
112
113
	/**
114
	 * @param int $id
115
	 * @return JSONResponse
116
	 */
117
	public function deleteOperation($id) {
118
		$deleted = $this->manager->deleteOperation((int) $id);
119
		return new JSONResponse($deleted);
0 ignored issues
show
Documentation introduced by
$deleted is of type boolean, but the function expects a array|object.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
120
	}
121
122
	/**
123
	 * @param array $operation
124
	 * @return array
125
	 */
126
	protected function prepareOperation(array $operation) {
127
		$checkIds = json_decode($operation['checks']);
128
		$checks = $this->manager->getChecks($checkIds);
129
130
		$operation['checks'] = [];
131
		foreach ($checks as $check) {
132
			// Remove internal values
133
			unset($check['id']);
134
			unset($check['hash']);
135
136
			$operation['checks'][] = $check;
137
		}
138
139
		return $operation;
140
	}
141
}
142