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

ActionGroupAddAction::run()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 26
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
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\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\ActionQuery;
11
use keeko\core\model\GroupQuery;
12
13
/**
14
 */
15
class ActionGroupAddAction 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->addGroup($group);
50
			$action->save();	
51
		}
52
53
		// run response
54
		return $this->response->run($request, $action);
55
	}
56
}
57