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

ExtensionSerializerTrait   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 86
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 86
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getAttributes() 0 8 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 package() 0 8 1
A setPackage() 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\Package;
7
use Tobscure\JsonApi\Resource;
8
9
/**
10
 */
11
trait ExtensionSerializerTrait {
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
			'id' => $model->Id(),
20
			'key' => $model->Key(),
21
			'data' => $model->Data(),
22
			'package_id' => $model->PackageId(),
23
		];
24
	}
25
26
	/**
27
	 */
28
	public function getFields() {
29
		return ['id', 'key', 'data', 'package_id'];
30
	}
31
32
	/**
33
	 * @param mixed $model
34
	 */
35
	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...
36
		return $model->getId();
37
	}
38
39
	/**
40
	 */
41
	public function getRelationships() {
42
		return [
43
			'package' => Package::getSerializer()->getType(null)
44
		];
45
	}
46
47
	/**
48
	 */
49
	public function getSortFields() {
50
		return ['id', 'key', 'data', 'package_id'];
51
	}
52
53
	/**
54
	 * @param mixed $model
55
	 */
56
	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...
57
		return 'core/extension';
58
	}
59
60
	/**
61
	 * @param mixed $model
62
	 * @param mixed $data
63
	 */
64
	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...
65
		// attributes
66
		$attribs = isset($data['attributes']) ? $data['attributes'] : [];
67
68
		$model = HydrateUtils::hydrate($attribs, $model, ['id', 'key', 'data', 'package_id']);
69
70
		// relationships
71
		$this->hydrateRelationships($model, $data);
0 ignored issues
show
Bug introduced by
The method hydrateRelationships() does not exist on keeko\core\serializer\ba...xtensionSerializerTrait. 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...
72
73
		return $model;
74
	}
75
76
	/**
77
	 * @param mixed $model
78
	 * @param mixed $related
79
	 */
80
	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...
81
		$serializer = Package::getSerializer();
82
		$relationship = new Relationship(new Resource($model->getPackage(), $serializer));
83
		$relationship->setLinks([
84
			'related' => '%apiurl%' . $serializer->getType(null) . '/' . $serializer->getId($model)
85
		]);
86
		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...
87
	}
88
89
	/**
90
	 * @param mixed $model
91
	 * @param mixed $data
92
	 */
93
	public function setPackage($model, $data) {
94
		$model->setPackageId($data['id']);
95
	}
96
}
97