Completed
Push — master ( f0b491...11ee78 )
by Thomas
09:37
created

UserSerializer::addGroups()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 5

Duplication

Lines 8
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 8
loc 8
rs 9.4285
cc 3
eloc 5
nc 3
nop 2
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 groups($model, $related) {
37
		$relationship = new Relationship(new Collection($model->getGroups(), Group::getSerializer()));
38
		return $this->addRelationshipSelfLink($relationship, $model, $related);
39
	}
40
	
41 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...
42
		// attributes
43
		$attribs = isset($data['attributes']) ? $data['attributes'] : [];
44
		
45
		$user = HydrateUtils::hydrate($attribs, $model, ['login_name', 'password' => function ($v) {
46
			return password_hash($v, PASSWORD_BCRYPT);
47
		}, 'given_name', 'family_name', 'display_name', 'email', 'birthday', 'sex']);
48
49
		// relationships
50
		$this->hydrateRelationships($model, $data);
51
52
		return $user;
53
	}
54
	
55
	public function getRelationships() {
56
		return ['group' => 'group/groups'];
57
	}
58
59
	public function setGroups($model, $data) {
60
		UserGroupQuery::create()->filterByUser($model)->deleteAll();
61
		$this->addGroups($model, $data);
62
	}
63
	
64 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...
65
		foreach ($data as $item) {
66
			$group = GroupQuery::create()->findOneById($item['id']);
67
			if ($group !== null) {
68
				$model->addGroup($group);
69
			}
70
		}
71
	}
72
	
73 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...
74
		foreach ($data as $item) {
75
			$group = GroupQuery::create()->findOneById($item['id']);
76
			if ($group !== null) {
77
				$model->removeGroup($group);
78
			}
79
		}
80
	}
81
}