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

GroupSerializer::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 keeko\core\model\UserQuery;
7
use Tobscure\JsonApi\Relationship;
8
use Tobscure\JsonApi\Resource;
9
use keeko\core\model\User;
10
use Tobscure\JsonApi\Collection;
11
use keeko\core\model\UserGroupQuery;
12
13
class GroupSerializer extends AbstractSerializer {
14
15
	public function getType($model) {
16
		return 'group/groups';
17
	}
18
	
19
	public function getId($model) {
20
		return $model->getId();
21
	}
22
	
23
	public function getAttributes($model, array $fields = null) {
24
		return [
25
			'name' => $model->getName(),
26
			'is_guest' => $model->getIsGuest(),
27
			'is_default' => $model->getIsDefault(),
28
			'is_active' => $model->getIsActive(),
29
			'is_system' => $model->getIsSystem()
30
		];
31
	}
32
	
33
	public function getFields() {
34
		return ['name', 'is_guest', 'is_default', 'is_active', 'is_system'];
35
	}
36
	
37
	public function getSortFields() {
38
		return ['name'];
39
	}
40
41
	public function users($model, $related) {
42
		$relationship = new Relationship(new Collection($model->getUsers(), User::getSerializer()));
43
		return $this->addRelationshipSelfLink($relationship, $model, $related);
44
	}
45
46
	public function owner($model) {
47
		$user = UserQuery::create()->findOneById($model->getOwnerId());
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 9 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
48
		$serializer = User::getSerializer();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
49
		$relationship = new Relationship(new Resource($user, $serializer));
50
		$relationship->setLinks([
51
			'related' => '%apiurl%' . $serializer->getType() . '/' . $serializer->getId($user)
0 ignored issues
show
Bug introduced by
The call to getType() misses a required argument $model.

This check looks for function calls that miss required arguments.

Loading history...
52
		]);
53
		return $relationship;
54
	}
55
56 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...
57
		// attributes
58
		$attribs = isset($data['attributes']) ? $data['attributes'] : [];
59
		
60
		$model = HydrateUtils::hydrate($attribs, $model, ['login_name', 'password' => function ($v) {
61
			return password_hash($v, PASSWORD_BCRYPT);
62
		}, 'given_name', 'family_name', 'display_name', 'email', 'birthday', 'sex']);
63
		
64
		// relationships
65
		$this->hydrateRelationships($model, $data);
66
67
		return $model;
68
	}
69
	
70
	public function getRelationships() {
71
		return [
72
			'users' => 'user/users',
73
			'owner' => 'user/users'
74
		];
75
	}
76
	
77
	public function setUsers($model, $data) {
78
		UserGroupQuery::create()->filterByGroup($model)->deleteAll();
79
		$this->addUsers($model, $data);
80
	}
81
	
82 View Code Duplication
	public function addUsers($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...
83
		foreach ($data as $item) {
84
			$group = UserQuery::create()->findOneById($item['id']);
85
			if ($group !== null) {
86
				$model->addUser($group);
87
			}
88
		}
89
	}
90
	
91 View Code Duplication
	public function removeUsers($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...
92
		foreach ($data as $item) {
93
			$group = UserQuery::create()->findOneById($item['id']);
94
			if ($group !== null) {
95
				$model->removeUser($group);
96
			}
97
		}
98
	}
99
	
100
	public function setOwner($model, $data) {
101
		$model->setOwnerId($data['id']);
102
	}
103
}