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

GroupSerializer   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 3
Bugs 0 Features 1
Metric Value
wmc 16
c 3
b 0
f 1
lcom 0
cbo 3
dl 0
loc 112
rs 10

11 Methods

Rating   Name   Duplication   Size   Complexity  
A addUsers() 0 8 3
A getAttributes() 0 11 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 removeUsers() 0 8 3
A setUsers() 0 4 1
A user() 0 4 1
1
<?php
2
namespace keeko\core\model\serializer;
3
4
use keeko\framework\model\AbstractSerializer;
5
use keeko\framework\utils\HydrateUtils;
6
use Tobscure\JsonApi\Relationship;
7
use keeko\core\model\User;
8
use keeko\core\model\UserQuery;
9
use Tobscure\JsonApi\Collection;
10
use keeko\core\model\UserGroupQuery;
11
12
/**
13
 */
14
class GroupSerializer extends AbstractSerializer {
15
16
	/**
17
	 * @param mixed $model
18
	 * @param mixed $data
19
	 */
20
	public function addUsers($model, $data) {
21
		foreach ($data as $item) {
22
			$user = UserQuery::create()->findOneById($item['id']);
23
			if ($user !== null) {
24
				$model->addUser($user);
25
			}
26
		}
27
	}
28
29
	/**
30
	 * @param mixed $model
31
	 * @param array $fields
32
	 */
33
	public function getAttributes($model, array $fields = null) {
34
		return [
35
			'id' => $model->Id(),
36
			'owner_id' => $model->OwnerId(),
37
			'name' => $model->Name(),
38
			'is_guest' => $model->IsGuest(),
39
			'is_default' => $model->IsDefault(),
40
			'is_active' => $model->IsActive(),
41
			'is_system' => $model->IsSystem(),
42
		];
43
	}
44
45
	/**
46
	 */
47
	public function getFields() {
48
		return ['id', 'owner_id', 'name', 'is_guest', 'is_default', 'is_active', 'is_system', 'created_at', 'updated_at'];
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
			'user' => User::getSerializer()->getType(null)
63
		];
64
	}
65
66
	/**
67
	 */
68
	public function getSortFields() {
69
		return ['id', 'owner_id', 'name', 'is_guest', 'is_default', 'is_active', 'is_system'];
70
	}
71
72
	/**
73
	 * @param mixed $model
74
	 */
75
	public function getType($model) {
76
		return 'core/group';
77
	}
78
79
	/**
80
	 * @param mixed $model
81
	 * @param mixed $data
82
	 */
83
	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...
84
		// attributes
85
		$attribs = isset($data['attributes']) ? $data['attributes'] : [];
86
87
		$model = HydrateUtils::hydrate($attribs, $model, ['id', 'owner_id', 'name', 'is_guest', 'is_default', 'is_active', 'is_system']);
88
89
		// relationships
90
		$this->hydrateRelationships($model, $data);
91
92
		return $model;
93
	}
94
95
	/**
96
	 * @param mixed $model
97
	 * @param mixed $data
98
	 */
99
	public function removeUsers($model, $data) {
100
		foreach ($data as $item) {
101
			$user = UserQuery::create()->findOneById($item['id']);
102
			if ($user !== null) {
103
				$model->removeUser($user);
104
			}
105
		}
106
	}
107
108
	/**
109
	 * @param mixed $model
110
	 * @param mixed $data
111
	 */
112
	public function setUsers($model, $data) {
113
		UserGroupQuery::create()->filterByUser($model)->delete();
114
		$this->addUsers($model, $data);
115
	}
116
117
	/**
118
	 * @param mixed $model
119
	 * @param mixed $related
120
	 */
121
	public function user($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...
122
		$relationship = new Relationship(new Collection($model->getUsers(), User::getSerializer()));
123
		return $this->addRelationshipSelfLink($relationship, $model, $related);
124
	}
125
}
126