Completed
Push — master ( a7ae23...b30b3b )
by Thomas
09:53
created

ApiSerializerTrait   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

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

9 Methods

Rating   Name   Duplication   Size   Complexity  
A action() 0 8 1
A getAttributes() 0 9 1
A getFields() 0 3 1
A getId() 0 3 1
A getRelationships() 0 5 1
A getSortFields() 0 3 1
A getType() 0 3 1
A hydrate() 0 11 2
A setAction() 0 3 1
1
<?php
2
namespace keeko\core\serializer\base;
3
4
use keeko\framework\utils\HydrateUtils;
5
use Tobscure\JsonApi\Relationship;
6
use keeko\core\model\Action;
7
use Tobscure\JsonApi\Resource;
8
9
/**
10
 */
11
trait ApiSerializerTrait {
12
13
	/**
14
	 * @param mixed $model
15
	 * @param mixed $related
16
	 */
17
	public function action($model, $related) {
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
18
		$serializer = Action::getSerializer();
19
		$relationship = new Relationship(new Resource($model->getAction(), $serializer));
20
		$relationship->setLinks([
21
			'related' => '%apiurl%' . $serializer->getType(null) . '/' . $serializer->getId($model)
22
		]);
23
		return $this->addRelationshipSelfLink($relationship, $model, $related);
0 ignored issues
show
Bug introduced by
It seems like addRelationshipSelfLink() 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...
24
	}
25
26
	/**
27
	 * @param mixed $model
28
	 * @param array $fields
29
	 */
30
	public function getAttributes($model, array $fields = null) {
1 ignored issue
show
Unused Code introduced by
The parameter $fields is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
31
		return [
32
			'id' => $model->Id(),
33
			'route' => $model->Route(),
34
			'method' => $model->Method(),
35
			'action_id' => $model->ActionId(),
36
			'required_params' => $model->RequiredParams(),
37
		];
38
	}
39
40
	/**
41
	 */
42
	public function getFields() {
43
		return ['id', 'route', 'method', 'action_id', 'required_params'];
44
	}
45
46
	/**
47
	 * @param mixed $model
48
	 */
49
	public function getId($model) {
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
50
		return $model->getId();
51
	}
52
53
	/**
54
	 */
55
	public function getRelationships() {
56
		return [
57
			'action' => Action::getSerializer()->getType(null)
58
		];
59
	}
60
61
	/**
62
	 */
63
	public function getSortFields() {
64
		return ['id', 'route', 'method', 'action_id', 'required_params'];
65
	}
66
67
	/**
68
	 * @param mixed $model
69
	 */
70
	public function getType($model) {
1 ignored issue
show
Unused Code introduced by
The parameter $model is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
71
		return 'core/api';
72
	}
73
74
	/**
75
	 * @param mixed $model
76
	 * @param mixed $data
77
	 */
78
	public function hydrate($model, $data) {
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
79
		// attributes
80
		$attribs = isset($data['attributes']) ? $data['attributes'] : [];
81
82
		$model = HydrateUtils::hydrate($attribs, $model, ['id', 'route', 'method', 'action_id', 'required_params']);
83
84
		// relationships
85
		$this->hydrateRelationships($model, $data);
0 ignored issues
show
Bug introduced by
The method hydrateRelationships() does not exist on keeko\core\serializer\base\ApiSerializerTrait. Did you maybe mean hydrate()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
86
87
		return $model;
88
	}
89
90
	/**
91
	 * @param mixed $model
92
	 * @param mixed $data
93
	 */
94
	public function setAction($model, $data) {
95
		$model->setActionId($data['id']);
96
	}
97
}
98