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

GroupSerializer   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 83
Duplicated Lines 34.94 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 16
c 1
b 0
f 1
lcom 1
cbo 7
dl 29
loc 83
rs 10

11 Methods

Rating   Name   Duplication   Size   Complexity  
A getType() 0 3 1
A getId() 0 3 1
A getAttributes() 0 9 1
A users() 0 4 1
A owner() 0 9 1
A hydrate() 13 13 2
A getRelationships() 0 6 1
A setUsers() 0 4 1
A addUsers() 8 8 3
A removeUsers() 8 8 3
A setOwner() 0 3 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 users($model, $related) {
34
		$relationship = new Relationship(new Collection($model->getUsers(), User::getSerializer()));
35
		return $this->addRelationshipSelfLink($relationship, $model, $related);
36
	}
37
38
	public function owner($model) {
39
		$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...
40
		$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...
41
		$relationship = new Relationship(new Resource($user, $serializer));
42
		$relationship->setLinks([
43
			'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...
44
		]);
45
		return $relationship;
46
	}
47
48 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...
49
		// attributes
50
		$attribs = isset($data['attributes']) ? $data['attributes'] : [];
51
		
52
		$model = HydrateUtils::hydrate($attribs, $model, ['login_name', 'password' => function ($v) {
53
			return password_hash($v, PASSWORD_BCRYPT);
54
		}, 'given_name', 'family_name', 'display_name', 'email', 'birthday', 'sex']);
55
		
56
		// relationships
57
		$this->hydrateRelationships($model, $data);
58
59
		return $model;
60
	}
61
	
62
	public function getRelationships() {
63
		return [
64
			'users' => 'user/users',
65
			'owner' => 'user/users'
66
		];
67
	}
68
	
69
	public function setUsers($model, $data) {
70
		UserGroupQuery::create()->filterByGroup($model)->deleteAll();
71
		$this->addUsers($model, $data);
72
	}
73
	
74 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...
75
		foreach ($data as $item) {
76
			$group = UserQuery::create()->findOneById($item['id']);
77
			if ($group !== null) {
78
				$model->addUser($group);
79
			}
80
		}
81
	}
82
	
83 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...
84
		foreach ($data as $item) {
85
			$group = UserQuery::create()->findOneById($item['id']);
86
			if ($group !== null) {
87
				$model->removeUser($group);
88
			}
89
		}
90
	}
91
	
92
	public function setOwner($model, $data) {
93
		$model->setOwnerId($data['id']);
94
	}
95
}