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

LocalizationSerializerTrait::script()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

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 1
eloc 6
nc 1
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\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 keeko\core\model\LanguageVariantQuery;
12
use Tobscure\JsonApi\Collection;
13
use keeko\core\model\LocalizationVariantQuery;
14
15
/**
16
 */
17
trait LocalizationSerializerTrait {
18
19
	/**
20
	 * @param mixed $model
21
	 * @param mixed $data
22
	 */
23
	public function addLanguageVariants($model, $data) {
24
		foreach ($data as $item) {
25
			$languageVariant = LanguageVariantQuery::create()->findOneById($item['id']);
26
			if ($languageVariant !== null) {
27
				$model->addLanguageVariant($languageVariant);
28
			}
29
		}
30
	}
31
32
	/**
33
	 * @param mixed $model
34
	 * @param mixed $related
35
	 */
36
	public function extLang($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...
37
		$serializer = Language::getSerializer();
38
		$relationship = new Relationship(new Resource($model->getExtLang(), $serializer));
39
		$relationship->setLinks([
40
			'related' => '%apiurl%' . $serializer->getType(null) . '/' . $serializer->getId($model)
41
		]);
42
		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...
43
	}
44
45
	/**
46
	 * @param mixed $model
47
	 * @param array $fields
48
	 */
49
	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...
50
		return [
51
			'id' => $model->Id(),
52
			'parent_id' => $model->ParentId(),
53
			'name' => $model->Name(),
54
			'locale' => $model->Locale(),
55
			'language_id' => $model->LanguageId(),
56
			'ext_language_id' => $model->ExtLanguageId(),
57
			'region' => $model->Region(),
58
			'script_id' => $model->ScriptId(),
59
			'is_default' => $model->IsDefault(),
60
		];
61
	}
62
63
	/**
64
	 */
65
	public function getFields() {
66
		return ['id', 'parent_id', 'name', 'locale', 'language_id', 'ext_language_id', 'region', 'script_id', 'is_default'];
67
	}
68
69
	/**
70
	 * @param mixed $model
71
	 */
72
	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...
73
		return $model->getId();
74
	}
75
76
	/**
77
	 */
78
	public function getRelationships() {
79
		return [
80
			'localization' => Localization::getSerializer()->getType(null),
81
			'language' => Language::getSerializer()->getType(null),
82
			'ext-lang' => Language::getSerializer()->getType(null),
83
			'script' => LanguageScript::getSerializer()->getType(null),
84
			'language-variant' => LanguageVariant::getSerializer()->getType(null)
85
		];
86
	}
87
88
	/**
89
	 */
90
	public function getSortFields() {
91
		return ['id', 'parent_id', 'name', 'locale', 'language_id', 'ext_language_id', 'region', 'script_id', 'is_default'];
92
	}
93
94
	/**
95
	 * @param mixed $model
96
	 */
97
	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...
98
		return 'core/localization';
99
	}
100
101
	/**
102
	 * @param mixed $model
103
	 * @param mixed $data
104
	 */
105
	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...
106
		// attributes
107
		$attribs = isset($data['attributes']) ? $data['attributes'] : [];
108
109
		$model = HydrateUtils::hydrate($attribs, $model, ['id', 'parent_id', 'name', 'locale', 'language_id', 'ext_language_id', 'region', 'script_id', 'is_default']);
110
111
		// relationships
112
		$this->hydrateRelationships($model, $data);
0 ignored issues
show
Bug introduced by
The method hydrateRelationships() does not exist on keeko\core\serializer\ba...lizationSerializerTrait. 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...
113
114
		return $model;
115
	}
116
117
	/**
118
	 * @param mixed $model
119
	 * @param mixed $related
120
	 */
121
	public function language($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...
122
		$serializer = Language::getSerializer();
123
		$relationship = new Relationship(new Resource($model->getLanguage(), $serializer));
124
		$relationship->setLinks([
125
			'related' => '%apiurl%' . $serializer->getType(null) . '/' . $serializer->getId($model)
126
		]);
127
		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...
128
	}
129
130
	/**
131
	 * @param mixed $model
132
	 * @param mixed $related
133
	 */
134
	public function languageVariant($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
		$relationship = new Relationship(new Collection($model->getLanguageVariants(), LanguageVariant::getSerializer()));
136
		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...
137
	}
138
139
	/**
140
	 * @param mixed $model
141
	 * @param mixed $related
142
	 */
143
	public function localization($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...
144
		$serializer = Localization::getSerializer();
145
		$relationship = new Relationship(new Resource($model->getLocalization(), $serializer));
146
		$relationship->setLinks([
147
			'related' => '%apiurl%' . $serializer->getType(null) . '/' . $serializer->getId($model)
148
		]);
149
		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...
150
	}
151
152
	/**
153
	 * @param mixed $model
154
	 * @param mixed $data
155
	 */
156
	public function removeLanguageVariants($model, $data) {
157
		foreach ($data as $item) {
158
			$languageVariant = LanguageVariantQuery::create()->findOneById($item['id']);
159
			if ($languageVariant !== null) {
160
				$model->removeLanguageVariant($languageVariant);
161
			}
162
		}
163
	}
164
165
	/**
166
	 * @param mixed $model
167
	 * @param mixed $related
168
	 */
169
	public function script($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...
170
		$serializer = LanguageScript::getSerializer();
171
		$relationship = new Relationship(new Resource($model->getScript(), $serializer));
172
		$relationship->setLinks([
173
			'related' => '%apiurl%' . $serializer->getType(null) . '/' . $serializer->getId($model)
174
		]);
175
		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...
176
	}
177
178
	/**
179
	 * @param mixed $model
180
	 * @param mixed $data
181
	 */
182
	public function setExtLang($model, $data) {
183
		$model->setExtLangId($data['id']);
184
	}
185
186
	/**
187
	 * @param mixed $model
188
	 * @param mixed $data
189
	 */
190
	public function setLanguage($model, $data) {
191
		$model->setLanguageId($data['id']);
192
	}
193
194
	/**
195
	 * @param mixed $model
196
	 * @param mixed $data
197
	 */
198
	public function setLanguageVariants($model, $data) {
199
		LocalizationVariantQuery::create()->filterByLanguageVariant($model)->delete();
200
		$this->addLanguageVariants($model, $data);
201
	}
202
203
	/**
204
	 * @param mixed $model
205
	 * @param mixed $data
206
	 */
207
	public function setLocalization($model, $data) {
208
		$model->setLocalizationId($data['id']);
209
	}
210
211
	/**
212
	 * @param mixed $model
213
	 * @param mixed $data
214
	 */
215
	public function setScript($model, $data) {
216
		$model->setScriptId($data['id']);
217
	}
218
}
219