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

ActionUpdateActionTrait   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
c 1
b 0
f 0
lcom 1
cbo 5
dl 0
loc 39
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configureParams() 0 3 1
A run() 0 23 3
1
<?php
2
namespace keeko\core\action\base;
3
4
use Symfony\Component\HttpFoundation\Request;
5
use Symfony\Component\HttpFoundation\Response;
6
use Symfony\Component\OptionsResolver\OptionsResolver;
7
use phootwork\json\Json;
8
use keeko\core\model\Action;
9
use keeko\core\model\ActionQuery;
10
use keeko\framework\exceptions\ValidationException;
11
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
12
13
/**
14
 * Base methods for keeko\core\action\ActionUpdateAction
15
 * 
16
 * This code is automatically created. Modifications will probably be overwritten.
17
 * 
18
 * @author gossi
19
 */
20
trait ActionUpdateActionTrait {
21
22
	/**
23
	 * @param OptionsResolver $resolver
24
	 */
25
	public function configureParams(OptionsResolver $resolver) {
26
		$resolver->setRequired(['id']);
27
	}
28
29
	/**
30
	 * Automatically generated run method
31
	 * 
32
	 * @param Request $request
33
	 * @return Response
34
	 */
35
	public function run(Request $request) {
36
		// read
37
		$id = $this->getParam('id');
0 ignored issues
show
Bug introduced by
It seems like getParam() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
38
		$action = ActionQuery::create()->findOneById($id);
39
40
		// check existence
41
		if ($action === null) {
42
			throw new ResourceNotFoundException('Action not found.');
43
		}
44
45
		// hydrate
46
		$data = 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...
47
		$serializer = Action::getSerializer();
48
		$action = $serializer->hydrate($action, $data);
49
50
		// validate
51
		if (!$action->validate()) {
52
			throw new ValidationException($action->getValidationFailures());
53
		} else {
54
			$action->save();
55
			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...
56
		}
57
	}
58
}
59