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

ActionGroupUpdateAction   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 6
c 2
b 0
f 0
lcom 1
cbo 5
dl 0
loc 46
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configureParams() 0 3 1
B run() 0 30 5
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\GroupActionQuery;
11
use keeko\core\model\ActionQuery;
12
use keeko\core\model\GroupQuery;
13
14
/**
15
 */
16
class ActionGroupUpdateAction extends AbstractAction {
17
18
	/**
19
	 * @param OptionsResolver $resolver
20
	 */
21
	public function configureParams(OptionsResolver $resolver) {
22
		$resolver->setRequired(['id']);
23
	}
24
25
	/**
26
	 * Automatically generated run method
27
	 * 
28
	 * @param Request $request
29
	 * @return Response
30
	 */
31
	public function run(Request $request) {
32
		$body = $request->getContent();
33
		if (!isset($body['data'])) {
34
			throw new InvalidParameterException();
35
		}
36
		$data = $body['data'];
37
38
		$id = $this->getParam('id');
39
		$action = ActionQuery::create()->findOneById($id);
40
41
		if ($action === null) {
42
			throw new ResourceNotFoundException('action with id ' . $id . ' does not exist');
43
		}
44
45
		// remove all relationships before
46
		GroupActionQuery::create()->filterByAction($action)->delete();
47
48
		// add them
49
		foreach ($data as $entry) {
50
			if (!isset($entry['id'])) {
51
				throw new InvalidParameterException();
52
			}
53
			$group = GroupQuery::create()->findOneById($entry['id']);
54
			$action->addGroup($group);
55
			$action->save();	
56
		}
57
58
		// run response
59
		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...
60
	}
61
}
62