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

ApplicationPackageUpdateAction::run()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 23
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 23
rs 8.7972
cc 4
eloc 15
nc 4
nop 1
1
<?php
2
namespace keeko\core\action;
3
4
use keeko\framework\foundation\AbstractAction;
5
use Symfony\Component\OptionsResolver\OptionsResolver;
6
use keeko\framework\exceptions\ValidationException;
7
use Tobscure\JsonApi\Exception\InvalidParameterException;
8
use phootwork\json\Json;
9
use Symfony\Component\HttpFoundation\Request;
10
use Symfony\Component\HttpFoundation\Response;
11
use keeko\core\model\ApplicationQuery;
12
13
/**
14
 */
15
class ApplicationPackageUpdateAction 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 = Json::decode($request->getContent());
1 ignored issue
show
Bug introduced by
It seems like $request->getContent() targeting Symfony\Component\HttpFo...n\Request::getContent() can also be of type resource; however, phootwork\json\Json::decode() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
32
		if (!isset($body['data'])) {
33
			throw new InvalidParameterException();
34
		}
35
		$data = $body['data'];
36
37
		if (!isset($data['id'])) {
38
			throw new InvalidParameterException();
39
		}
40
41
		$id = $this->getParam('id');
42
		$application = ApplicationQuery::create()->findOneById($id);
43
		$application->setPackageId($data['id']);
44
45
		// validate
46
		if (!$application->validate()) {
47
			throw new ValidationException($application->getValidationFailures());
48
		} else {
49
			$application->save();
50
			return $this->response->run($request, $application);
51
		}
52
	}
53
}
54