Completed
Push — master ( 944dfb...9c93d5 )
by Thomas
10:01 queued 37s
created

ActivitySerializerTrait::object()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 1
eloc 6
nc 1
nop 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
	 * @return Relationship
17
	 */
18
	public function actor($model) {
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, 'actor');
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) {
32
		return [
33
			'id' => $model->getId(),
34
			'actor_id' => $model->getActorId(),
35
			'verb' => $model->getVerb(),
36
			'object_id' => $model->getObjectId(),
37
			'target_id' => $model->getTargetId(),
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
	 * @return string
50
	 */
51
	public function getId($model) {
52
		return $model->getId();
53
	}
54
55
	/**
56
	 */
57
	public function getRelationships() {
58
		return [
59
			'actor' => User::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
	 * @return string
73
	 */
74
	public function getType($model) {
75
		return 'core/activity';
76
	}
77
78
	/**
79
	 * @param mixed $model
80
	 * @param mixed $data
81
	 * @return mixed The model
82
	 */
83
	public function hydrate($model, $data) {
84
		// attributes
85
		$attribs = isset($data['attributes']) ? $data['attributes'] : [];
86
87
		$model = HydrateUtils::hydrate($attribs, $model, ['id', 'actor_id', 'verb', 'object_id', 'target_id']);
88
89
		// relationships
90
		$this->hydrateRelationships($model, $data);
91
92
		return $model;
93
	}
94
95
	/**
96
	 * @param mixed $model
97
	 * @return Relationship
98
	 */
99
	public function target($model) {
100
		$serializer = ActivityObject::getSerializer();
101
		$relationship = new Relationship(new Resource($model->getTarget(), $serializer));
102
		$relationship->setLinks([
103
			'related' => '%apiurl%' . $serializer->getType(null) . '/' . $serializer->getId($model)
104
		]);
105
		return $this->addRelationshipSelfLink($relationship, $model, 'target');
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...
106
	}
107
108
	/**
109
	 * @param mixed $model
110
	 * @param mixed $data
111
	 */
112
	abstract protected function hydrateRelationships($model, $data);
0 ignored issues
show
Documentation introduced by
For interfaces and abstract methods it is generally a good practice to add a @return annotation even if it is just @return void or @return null, so that implementors know what to do in the overridden method.

For interface and abstract methods, it is impossible to infer the return type from the immediate code. In these cases, it is generally advisible to explicitly annotate these methods with a @return doc comment to communicate to implementors of these methods what they are expected to return.

Loading history...
113
}
114