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

ActionSerializerTrait   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 133
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 18
c 1
b 0
f 0
lcom 1
cbo 6
dl 0
loc 133
rs 10

13 Methods

Rating   Name   Duplication   Size   Complexity  
A addGroups() 0 8 3
A getAttributes() 0 10 1
A getFields() 0 3 1
A getId() 0 3 1
A getRelationships() 0 6 1
A getSortFields() 0 3 1
A getType() 0 3 1
A group() 0 4 1
A hydrate() 0 11 2
A module() 0 8 1
A removeGroups() 0 8 3
A setGroups() 0 4 1
A setModule() 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\Module;
7
use Tobscure\JsonApi\Resource;
8
use keeko\core\model\Group;
9
use keeko\core\model\GroupQuery;
10
use Tobscure\JsonApi\Collection;
11
use keeko\core\model\GroupActionQuery;
12
13
/**
14
 */
15
trait ActionSerializerTrait {
16
17
	/**
18
	 * @param mixed $model
19
	 * @param mixed $data
20
	 */
21
	public function addGroups($model, $data) {
22
		foreach ($data as $item) {
23
			$group = GroupQuery::create()->findOneById($item['id']);
24
			if ($group !== null) {
25
				$model->addGroup($group);
26
			}
27
		}
28
	}
29
30
	/**
31
	 * @param mixed $model
32
	 * @param array $fields
33
	 */
34
	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...
35
		return [
36
			'id' => $model->Id(),
37
			'name' => $model->Name(),
38
			'title' => $model->Title(),
39
			'description' => $model->Description(),
40
			'class_name' => $model->ClassName(),
41
			'module_id' => $model->ModuleId(),
42
		];
43
	}
44
45
	/**
46
	 */
47
	public function getFields() {
48
		return ['id', 'name', 'title', 'description', 'class_name', 'module_id'];
49
	}
50
51
	/**
52
	 * @param mixed $model
53
	 */
54
	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...
55
		return $model->getId();
56
	}
57
58
	/**
59
	 */
60
	public function getRelationships() {
61
		return [
62
			'module' => Module::getSerializer()->getType(null),
63
			'group' => Group::getSerializer()->getType(null)
64
		];
65
	}
66
67
	/**
68
	 */
69
	public function getSortFields() {
70
		return ['id', 'name', 'title', 'description', 'class_name', 'module_id'];
71
	}
72
73
	/**
74
	 * @param mixed $model
75
	 */
76
	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...
77
		return 'core/action';
78
	}
79
80
	/**
81
	 * @param mixed $model
82
	 * @param mixed $related
83
	 */
84
	public function group($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...
85
		$relationship = new Relationship(new Collection($model->getGroups(), Group::getSerializer()));
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 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...
94
		// attributes
95
		$attribs = isset($data['attributes']) ? $data['attributes'] : [];
96
97
		$model = HydrateUtils::hydrate($attribs, $model, ['id', 'name', 'title', 'description', 'class_name', 'module_id']);
98
99
		// relationships
100
		$this->hydrateRelationships($model, $data);
0 ignored issues
show
Bug introduced by
The method hydrateRelationships() does not exist on keeko\core\serializer\base\ActionSerializerTrait. 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...
101
102
		return $model;
103
	}
104
105
	/**
106
	 * @param mixed $model
107
	 * @param mixed $related
108
	 */
109
	public function module($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...
110
		$serializer = Module::getSerializer();
111
		$relationship = new Relationship(new Resource($model->getModule(), $serializer));
112
		$relationship->setLinks([
113
			'related' => '%apiurl%' . $serializer->getType(null) . '/' . $serializer->getId($model)
114
		]);
115
		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...
116
	}
117
118
	/**
119
	 * @param mixed $model
120
	 * @param mixed $data
121
	 */
122
	public function removeGroups($model, $data) {
123
		foreach ($data as $item) {
124
			$group = GroupQuery::create()->findOneById($item['id']);
125
			if ($group !== null) {
126
				$model->removeGroup($group);
127
			}
128
		}
129
	}
130
131
	/**
132
	 * @param mixed $model
133
	 * @param mixed $data
134
	 */
135
	public function setGroups($model, $data) {
136
		GroupActionQuery::create()->filterByGroup($model)->delete();
137
		$this->addGroups($model, $data);
138
	}
139
140
	/**
141
	 * @param mixed $model
142
	 * @param mixed $data
143
	 */
144
	public function setModule($model, $data) {
145
		$model->setModuleId($data['id']);
146
	}
147
}
148