Completed
Push — master ( a7ae23...b30b3b )
by Thomas
09:53
created

UserSerializerTrait::removeGroups()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 3
eloc 5
nc 3
nop 2
1
<?php
2
namespace keeko\core\serializer\base;
3
4
use keeko\framework\utils\HydrateUtils;
5
use Tobscure\JsonApi\Relationship;
6
use keeko\core\model\Group;
7
use keeko\core\model\GroupQuery;
8
use Tobscure\JsonApi\Collection;
9
use keeko\core\model\UserGroupQuery;
10
11
/**
12
 */
13
trait UserSerializerTrait {
14
15
	/**
16
	 * @param mixed $model
17
	 * @param mixed $data
18
	 */
19
	public function addGroups($model, $data) {
20
		foreach ($data as $item) {
21
			$group = GroupQuery::create()->findOneById($item['id']);
22
			if ($group !== null) {
23
				$model->addGroup($group);
24
			}
25
		}
26
	}
27
28
	/**
29
	 * @param mixed $model
30
	 * @param array $fields
31
	 */
32
	public function getAttributes($model, array $fields = null) {
1 ignored issue
show
Unused Code introduced by
The parameter $fields is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
33
		return [
34
			'id' => $model->Id(),
35
			'login_name' => $model->LoginName(),
36
			'password' => $model->Password(),
37
			'given_name' => $model->GivenName(),
38
			'family_name' => $model->FamilyName(),
39
			'display_name' => $model->DisplayName(),
40
			'email' => $model->Email(),
41
			'birthday' => $model->Birthday(\DateTime::ISO8601),
42
			'sex' => $model->Sex(),
43
		];
44
	}
45
46
	/**
47
	 */
48
	public function getFields() {
49
		return ['id', 'login_name', 'given_name', 'family_name', 'display_name', 'email', 'birthday', 'sex', 'created_at', 'updated_at'];
50
	}
51
52
	/**
53
	 * @param mixed $model
54
	 */
55
	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...
56
		return $model->getId();
57
	}
58
59
	/**
60
	 */
61
	public function getRelationships() {
62
		return [
63
			'group' => Group::getSerializer()->getType(null)
64
		];
65
	}
66
67
	/**
68
	 */
69
	public function getSortFields() {
70
		return ['id', 'login_name', 'password', 'given_name', 'family_name', 'display_name', 'email', 'birthday', 'sex'];
71
	}
72
73
	/**
74
	 * @param mixed $model
75
	 */
76
	public function getType($model) {
1 ignored issue
show
Unused Code introduced by
The parameter $model is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
77
		return 'core/user';
78
	}
79
80
	/**
81
	 * @param mixed $model
82
	 * @param mixed $related
83
	 */
84
	public function group($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...
85
		$relationship = new Relationship(new Collection($model->getGroups(), Group::getSerializer()));
86
		return $this->addRelationshipSelfLink($relationship, $model, $related);
0 ignored issues
show
Bug introduced by
It seems like addRelationshipSelfLink() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
87
	}
88
89
	/**
90
	 * @param mixed $model
91
	 * @param mixed $data
92
	 */
93
	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...
94
		// attributes
95
		$attribs = isset($data['attributes']) ? $data['attributes'] : [];
96
97
		$model = HydrateUtils::hydrate($attribs, $model, ['id', 'login_name', 'password' => function($v) {
98
			return password_hash($v, PASSWORD_BCRYPT);
99
		}, 'given_name', 'family_name', 'display_name', 'email', 'birthday', 'sex']);
100
101
		// relationships
102
		$this->hydrateRelationships($model, $data);
0 ignored issues
show
Bug introduced by
The method hydrateRelationships() does not exist on keeko\core\serializer\base\UserSerializerTrait. Did you maybe mean hydrate()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
103
104
		return $model;
105
	}
106
107
	/**
108
	 * @param mixed $model
109
	 * @param mixed $data
110
	 */
111
	public function removeGroups($model, $data) {
112
		foreach ($data as $item) {
113
			$group = GroupQuery::create()->findOneById($item['id']);
114
			if ($group !== null) {
115
				$model->removeGroup($group);
116
			}
117
		}
118
	}
119
120
	/**
121
	 * @param mixed $model
122
	 * @param mixed $data
123
	 */
124
	public function setGroups($model, $data) {
125
		UserGroupQuery::create()->filterByGroup($model)->delete();
126
		$this->addGroups($model, $data);
127
	}
128
}
129