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

ApplicationSerializerTrait::getId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 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\Package;
7
use Tobscure\JsonApi\Resource;
8
9
/**
10
 */
11
trait ApplicationSerializerTrait {
12
13
	/**
14
	 * @param mixed $model
15
	 * @param array $fields
16
	 */
17
	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...
18
		return [
19
			'class_name' => $model->ClassName(),
20
			'id' => $model->Id(),
21
			'name' => $model->Name(),
22
			'title' => $model->Title(),
23
			'description' => $model->Description(),
24
			'installed_version' => $model->InstalledVersion(),
25
		];
26
	}
27
28
	/**
29
	 */
30
	public function getFields() {
31
		return ['class_name', 'id', 'name', 'title', 'description', 'installed_version'];
32
	}
33
34
	/**
35
	 * @param mixed $model
36
	 */
37
	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...
38
		return $model->getId();
39
	}
40
41
	/**
42
	 */
43
	public function getRelationships() {
44
		return [
45
			'package' => Package::getSerializer()->getType(null)
46
		];
47
	}
48
49
	/**
50
	 */
51
	public function getSortFields() {
52
		return ['class_name', 'id', 'name', 'title', 'description', 'installed_version'];
53
	}
54
55
	/**
56
	 * @param mixed $model
57
	 */
58
	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...
59
		return 'core/application';
60
	}
61
62
	/**
63
	 * @param mixed $model
64
	 * @param mixed $data
65
	 */
66
	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...
67
		// attributes
68
		$attribs = isset($data['attributes']) ? $data['attributes'] : [];
69
70
		$model = HydrateUtils::hydrate($attribs, $model, ['class_name', 'id', 'name', 'title', 'description', 'installed_version']);
71
72
		// relationships
73
		$this->hydrateRelationships($model, $data);
0 ignored issues
show
Bug introduced by
The method hydrateRelationships() does not exist on keeko\core\serializer\ba...licationSerializerTrait. 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...
74
75
		return $model;
76
	}
77
78
	/**
79
	 * @param mixed $model
80
	 * @param mixed $related
81
	 */
82
	public function package($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...
83
		$serializer = Package::getSerializer();
84
		$relationship = new Relationship(new Resource($model->getPackage(), $serializer));
85
		$relationship->setLinks([
86
			'related' => '%apiurl%' . $serializer->getType(null) . '/' . $serializer->getId($model)
87
		]);
88
		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...
89
	}
90
91
	/**
92
	 * @param mixed $model
93
	 * @param mixed $data
94
	 */
95
	public function setPackage($model, $data) {
96
		$model->setPackageId($data['id']);
97
	}
98
}
99