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

LocalizationSerializerTrait::getRelationships()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 1
eloc 6
nc 1
nop 0
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\Localization;
7
use Tobscure\JsonApi\Resource;
8
use keeko\core\model\Language;
9
use keeko\core\model\LanguageScript;
10
use keeko\core\model\LanguageVariant;
11
use Tobscure\JsonApi\Collection;
12
13
/**
14
 */
15
trait LocalizationSerializerTrait {
16
17
	/**
18
	 * @param mixed $model
19
	 * @return Relationship
20
	 */
21
	public function extLang($model) {
22
		$serializer = Language::getSerializer();
23
		$relationship = new Relationship(new Resource($model->getExtLang(), $serializer));
24
		$relationship->setLinks([
25
			'related' => '%apiurl%' . $serializer->getType(null) . '/' . $serializer->getId($model)
26
		]);
27
		return $this->addRelationshipSelfLink($relationship, $model, 'ext-lang');
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...
28
	}
29
30
	/**
31
	 * @param mixed $model
32
	 * @param array $fields
33
	 */
34
	public function getAttributes($model, array $fields = null) {
35
		return [
36
			'id' => $model->getId(),
37
			'parent_id' => $model->getParentId(),
38
			'name' => $model->getName(),
39
			'locale' => $model->getLocale(),
40
			'language_id' => $model->getLanguageId(),
41
			'ext_language_id' => $model->getExtLanguageId(),
42
			'region' => $model->getRegion(),
43
			'script_id' => $model->getScriptId(),
44
			'is_default' => $model->getIsDefault(),
45
		];
46
	}
47
48
	/**
49
	 */
50
	public function getFields() {
51
		return ['id', 'parent_id', 'name', 'locale', 'language_id', 'ext_language_id', 'region', 'script_id', 'is_default'];
52
	}
53
54
	/**
55
	 * @param mixed $model
56
	 * @return string
57
	 */
58
	public function getId($model) {
59
		return $model->getId();
60
	}
61
62
	/**
63
	 */
64
	public function getRelationships() {
65
		return [
66
			'parent' => Localization::getSerializer()->getType(null),
67
			'ext-lang' => Language::getSerializer()->getType(null),
68
			'script' => LanguageScript::getSerializer()->getType(null),
69
			'language-variants' => LanguageVariant::getSerializer()->getType(null)
70
		];
71
	}
72
73
	/**
74
	 */
75
	public function getSortFields() {
76
		return ['id', 'parent_id', 'name', 'locale', 'language_id', 'ext_language_id', 'region', 'script_id', 'is_default'];
77
	}
78
79
	/**
80
	 * @param mixed $model
81
	 * @return string
82
	 */
83
	public function getType($model) {
84
		return 'core/localization';
85
	}
86
87
	/**
88
	 * @param mixed $model
89
	 * @param mixed $data
90
	 * @return mixed The model
91
	 */
92
	public function hydrate($model, $data) {
93
		// attributes
94
		$attribs = isset($data['attributes']) ? $data['attributes'] : [];
95
96
		$model = HydrateUtils::hydrate($attribs, $model, ['id', 'parent_id', 'name', 'locale', 'language_id', 'ext_language_id', 'region', 'script_id', 'is_default']);
97
98
		// relationships
99
		$this->hydrateRelationships($model, $data);
100
101
		return $model;
102
	}
103
104
	/**
105
	 * @param mixed $model
106
	 * @return Relationship
107
	 */
108
	public function languageVariants($model) {
109
		$relationship = new Relationship(new Collection($model->getLanguageVariants(), LanguageVariant::getSerializer()));
110
		return $this->addRelationshipSelfLink($relationship, $model, 'language-variants');
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...
111
	}
112
113
	/**
114
	 * @param mixed $model
115
	 * @return Relationship
116
	 */
117
	public function parent($model) {
118
		$serializer = Localization::getSerializer();
119
		$relationship = new Relationship(new Resource($model->getParent(), $serializer));
120
		$relationship->setLinks([
121
			'related' => '%apiurl%' . $serializer->getType(null) . '/' . $serializer->getId($model)
122
		]);
123
		return $this->addRelationshipSelfLink($relationship, $model, 'parent');
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...
124
	}
125
126
	/**
127
	 * @param mixed $model
128
	 * @return Relationship
129
	 */
130
	public function script($model) {
131
		$serializer = LanguageScript::getSerializer();
132
		$relationship = new Relationship(new Resource($model->getScript(), $serializer));
133
		$relationship->setLinks([
134
			'related' => '%apiurl%' . $serializer->getType(null) . '/' . $serializer->getId($model)
135
		]);
136
		return $this->addRelationshipSelfLink($relationship, $model, 'script');
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...
137
	}
138
139
	/**
140
	 * @param mixed $model
141
	 * @param mixed $data
142
	 */
143
	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...
144
}
145