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

ActivitySerializerTrait   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 131
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 14
c 1
b 0
f 0
lcom 1
cbo 5
dl 0
loc 131
rs 10

13 Methods

Rating   Name   Duplication   Size   Complexity  
A actor() 0 8 1
A getAttributes() 0 9 1
A getFields() 0 3 1
A getId() 0 3 1
A getRelationships() 0 7 1
A getSortFields() 0 3 1
A getType() 0 3 1
A hydrate() 0 11 2
A object() 0 8 1
A setActor() 0 3 1
A setObject() 0 3 1
A setTarget() 0 3 1
A target() 0 8 1
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\User;
7
use Tobscure\JsonApi\Resource;
8
use keeko\core\model\ActivityObject;
9
10
/**
11
 */
12
trait ActivitySerializerTrait {
13
14
	/**
15
	 * @param mixed $model
16
	 * @param mixed $related
17
	 */
18
	public function actor($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...
19
		$serializer = User::getSerializer();
20
		$relationship = new Relationship(new Resource($model->getActor(), $serializer));
21
		$relationship->setLinks([
22
			'related' => '%apiurl%' . $serializer->getType(null) . '/' . $serializer->getId($model)
23
		]);
24
		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...
25
	}
26
27
	/**
28
	 * @param mixed $model
29
	 * @param array $fields
30
	 */
31
	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...
32
		return [
33
			'id' => $model->Id(),
34
			'actor_id' => $model->ActorId(),
35
			'verb' => $model->Verb(),
36
			'object_id' => $model->ObjectId(),
37
			'target_id' => $model->TargetId(),
38
		];
39
	}
40
41
	/**
42
	 */
43
	public function getFields() {
44
		return ['id', 'actor_id', 'verb', 'object_id', 'target_id'];
45
	}
46
47
	/**
48
	 * @param mixed $model
49
	 */
50
	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...
51
		return $model->getId();
52
	}
53
54
	/**
55
	 */
56
	public function getRelationships() {
57
		return [
58
			'actor' => User::getSerializer()->getType(null),
59
			'object' => ActivityObject::getSerializer()->getType(null),
60
			'target' => ActivityObject::getSerializer()->getType(null)
61
		];
62
	}
63
64
	/**
65
	 */
66
	public function getSortFields() {
67
		return ['id', 'actor_id', 'verb', 'object_id', 'target_id'];
68
	}
69
70
	/**
71
	 * @param mixed $model
72
	 */
73
	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...
74
		return 'core/activity';
75
	}
76
77
	/**
78
	 * @param mixed $model
79
	 * @param mixed $data
80
	 */
81
	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...
82
		// attributes
83
		$attribs = isset($data['attributes']) ? $data['attributes'] : [];
84
85
		$model = HydrateUtils::hydrate($attribs, $model, ['id', 'actor_id', 'verb', 'object_id', 'target_id']);
86
87
		// relationships
88
		$this->hydrateRelationships($model, $data);
0 ignored issues
show
Bug introduced by
The method hydrateRelationships() does not exist on keeko\core\serializer\base\ActivitySerializerTrait. 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...
89
90
		return $model;
91
	}
92
93
	/**
94
	 * @param mixed $model
95
	 * @param mixed $related
96
	 */
97
	public function object($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...
98
		$serializer = ActivityObject::getSerializer();
99
		$relationship = new Relationship(new Resource($model->getObject(), $serializer));
100
		$relationship->setLinks([
101
			'related' => '%apiurl%' . $serializer->getType(null) . '/' . $serializer->getId($model)
102
		]);
103
		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...
104
	}
105
106
	/**
107
	 * @param mixed $model
108
	 * @param mixed $data
109
	 */
110
	public function setActor($model, $data) {
111
		$model->setActorId($data['id']);
112
	}
113
114
	/**
115
	 * @param mixed $model
116
	 * @param mixed $data
117
	 */
118
	public function setObject($model, $data) {
119
		$model->setObjectId($data['id']);
120
	}
121
122
	/**
123
	 * @param mixed $model
124
	 * @param mixed $data
125
	 */
126
	public function setTarget($model, $data) {
127
		$model->setTargetId($data['id']);
128
	}
129
130
	/**
131
	 * @param mixed $model
132
	 * @param mixed $related
133
	 */
134
	public function target($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...
135
		$serializer = ActivityObject::getSerializer();
136
		$relationship = new Relationship(new Resource($model->getTarget(), $serializer));
137
		$relationship->setLinks([
138
			'related' => '%apiurl%' . $serializer->getType(null) . '/' . $serializer->getId($model)
139
		]);
140
		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...
141
	}
142
}
143