Completed
Push — master ( d6a6e9...1d4c8e )
by Thomas
10:31
created

ActionGroupUpdateAction   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
c 1
b 0
f 0
lcom 1
cbo 6
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\OptionsResolver\OptionsResolver;
6
use Symfony\Component\HttpFoundation\Request;
7
use Symfony\Component\HttpFoundation\Response;
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);
60
	}
61
}
62