Completed
Push — master ( 11ee78...8b3cbf )
by Thomas
04:46
created

UserSerializer::getFields()   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 0
1
<?php
2
namespace keeko\core\model\serializer;
3
4
use keeko\core\model\serializer\AbstractSerializer;
5
use keeko\core\utils\HydrateUtils;
6
use Tobscure\JsonApi\Relationship;
7
use keeko\core\model\Group;
8
use Tobscure\JsonApi\Collection;
9
use keeko\core\model\UserGroupQuery;
10
use keeko\core\model\GroupQuery;
11
12
class UserSerializer extends AbstractSerializer {
13
14
	public function getType($model) {
15
		return 'user/users';
16
	}
17
	
18
	public function getId($model) {
19
		return $model->getId();
20
	}
21
	
22
	public function getAttributes($model, array $fields = null) {
23
		return [
24
			'login_name' => $model->getLoginName(),
25
			'given_name' => $model->getGivenName(),
26
			'family_name' => $model->getFamilyName(),
27
			'display_name' => $model->getDisplayName(),
28
			'email' => $model->getEmail(),
29
			'birthday' => $model->getBirthday(\DateTime::ISO8601),
30
			'sex' => $model->getSex(),
31
			'created_at' => $model->getCreatedAt(\DateTime::ISO8601),
32
			'updated_at' => $model->getUpdatedAt(\DateTime::ISO8601)
33
		];
34
	}
35
	
36
	public function getFields() {
37
		return ['login_name', 'given_name', 'family_name', 'display_name', 'email', 'birthday', 'sex', 'created_at', 'updated_at'];
38
	}
39
	
40
	public function getSortFields() {
41
		return ['login_name', 'given_name', 'family_name', 'display_name', 'email', 'birthday'];
42
	}
43
	
44
	public function groups($model, $related) {
45
		$relationship = new Relationship(new Collection($model->getGroups(), Group::getSerializer()));
46
		return $this->addRelationshipSelfLink($relationship, $model, $related);
47
	}
48
	
49 View Code Duplication
	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...
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
50
		// attributes
51
		$attribs = isset($data['attributes']) ? $data['attributes'] : [];
52
		
53
		$user = HydrateUtils::hydrate($attribs, $model, ['login_name', 'password' => function ($v) {
54
			return password_hash($v, PASSWORD_BCRYPT);
55
		}, 'given_name', 'family_name', 'display_name', 'email', 'birthday', 'sex']);
56
57
		// relationships
58
		$this->hydrateRelationships($model, $data);
59
60
		return $user;
61
	}
62
	
63
	public function getRelationships() {
64
		return ['group' => 'group/groups'];
65
	}
66
67
	public function setGroups($model, $data) {
68
		UserGroupQuery::create()->filterByUser($model)->deleteAll();
69
		$this->addGroups($model, $data);
70
	}
71
	
72 View Code Duplication
	public function addGroups($model, $data) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
73
		foreach ($data as $item) {
74
			$group = GroupQuery::create()->findOneById($item['id']);
75
			if ($group !== null) {
76
				$model->addGroup($group);
77
			}
78
		}
79
	}
80
	
81 View Code Duplication
	public function removeGroups($model, $data) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
82
		foreach ($data as $item) {
83
			$group = GroupQuery::create()->findOneById($item['id']);
84
			if ($group !== null) {
85
				$model->removeGroup($group);
86
			}
87
		}
88
	}
89
}