Completed
Push — master ( 415b34...7ef068 )
by Thomas
13:10 queued 06:22
created

ActionGroupRemoveAction::run()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 26
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 26
rs 8.439
cc 5
eloc 16
nc 5
nop 1
1
<?php
2
namespace keeko\core\action;
3
4
use keeko\framework\foundation\AbstractAction;
5
use Symfony\Component\HttpFoundation\Request;
6
use Symfony\Component\HttpFoundation\Response;
7
use Symfony\Component\OptionsResolver\OptionsResolver;
8
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
9
use Tobscure\JsonApi\Exception\InvalidParameterException;
10
use keeko\core\model\ActionQuery;
11
use keeko\core\model\GroupQuery;
12
13
/**
14
 */
15
class ActionGroupRemoveAction extends AbstractAction {
16
17
	/**
18
	 * @param OptionsResolver $resolver
19
	 */
20
	public function configureParams(OptionsResolver $resolver) {
21
		$resolver->setRequired(['id']);
22
	}
23
24
	/**
25
	 * Automatically generated run method
26
	 * 
27
	 * @param Request $request
28
	 * @return Response
29
	 */
30
	public function run(Request $request) {
31
		$body = $request->getContent();
32
		if (!isset($body['data'])) {
33
			throw new InvalidParameterException();
34
		}
35
		$data = $body['data'];
36
37
		$id = $this->getParam('id');
38
		$action = ActionQuery::create()->findOneById($id);
39
40
		if ($action === null) {
41
			throw new ResourceNotFoundException('action with id ' . $id . ' does not exist');
42
		} 
43
44
		foreach ($data as $entry) {
45
			if (!isset($entry['id'])) {
46
				throw new InvalidParameterException();
47
			}
48
			$group = GroupQuery::create()->findOneById($entry['id']);
49
			$action->removeGroup($group);
50
			$action->save();	
51
		}
52
53
		// run response
54
		return $this->response->run($request, $action);
0 ignored issues
show
Bug introduced by
The property response does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
55
	}
56
}
57