Completed
Push — master ( f579b8...c5e777 )
by Thomas
07:12 queued 02:33
created

ApplicationDomainTrait::paginate()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 27
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 27
rs 8.8571
cc 3
eloc 15
nc 4
nop 1
1
<?php
2
namespace keeko\core\domain\base;
3
4
use keeko\core\model\Application;
5
use keeko\core\model\ApplicationQuery;
6
use keeko\framework\service\ServiceContainer;
7
use keeko\framework\domain\payload\PayloadInterface;
8
use phootwork\collection\Map;
9
use keeko\framework\domain\payload\Found;
10
use keeko\framework\domain\payload\NotFound;
11
use Tobscure\JsonApi\Parameters;
12
use keeko\framework\utils\NameUtils;
13
use keeko\framework\domain\payload\Created;
14
use keeko\framework\domain\payload\Updated;
15
use keeko\framework\domain\payload\NotUpdated;
16
use keeko\framework\domain\payload\NotValid;
17
use keeko\framework\domain\payload\Deleted;
18
use keeko\framework\domain\payload\NotDeleted;
19
20
/**
21
 */
22
trait ApplicationDomainTrait {
23
24
	/**
25
	 * Creates a new Application with the provided data
26
	 * 
27
	 * @param mixed $data
28
	 * @return PayloadInterface
29
	 */
30
	public function create($data) {
31
		// hydrate
32
		$serializer = Application::getSerializer();
33
		$application = $serializer->hydrate(new Application(), $data);
34
35
		// validate
36
		if (!$application->validate()) {
37
			return new NotValid([
38
				'errors' => $application->getValidationFailures()
39
			]);
40
		}
41
42
		$application->save();
43
		return new Created(['model' => $application]);
44
	}
45
46
	/**
47
	 * Deletes a Application with the given id
48
	 * 
49
	 * @param mixed $id
50
	 * @return PayloadInterface
51
	 */
52
	public function delete($id) {
53
		// find
54
		$application = $this->get($id);
55
56
		if ($application === null) {
57
			return new NotFound(['message' => 'Application not found.']);
58
		}
59
60
		// delete
61
		$application->delete();
62
63
		if ($application->isDeleted()) {
64
			return new Deleted(['model' => $application]);
65
		}
66
67
		return new NotDeleted(['message' => 'Could not delete Application']);
68
	}
69
70
	/**
71
	 * Returns a paginated result
72
	 * 
73
	 * @param Parameters $params
74
	 * @return PayloadInterface
75
	 */
76
	public function paginate(Parameters $params) {
77
		$sysPrefs = $this->getServiceContainer()->getPreferenceLoader()->getSystemPreferences();
78
		$defaultSize = $sysPrefs->getPaginationSize();
79
		$page = $params->getPage('number');
0 ignored issues
show
Bug introduced by
The method getPage() cannot be called from this context as it is declared protected in class Tobscure\JsonApi\Parameters.

This check looks for access to methods that are not accessible from the current context.

If you need to make a method accessible to another context you can raise its visibility level in the defining class.

Loading history...
80
		$size = $params->getPage('size', $defaultSize);
0 ignored issues
show
Bug introduced by
The method getPage() cannot be called from this context as it is declared protected in class Tobscure\JsonApi\Parameters.

This check looks for access to methods that are not accessible from the current context.

If you need to make a method accessible to another context you can raise its visibility level in the defining class.

Loading history...
Unused Code introduced by
The call to Parameters::getPage() has too many arguments starting with $defaultSize.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
81
82
		$query = ApplicationQuery::create();
83
84
		// sorting
85
		$sort = $params->getSort(Application::getSerializer()->getSortFields());
86
		foreach ($sort as $field => $order) {
87
			$method = 'orderBy' . NameUtils::toStudlyCase($field);
88
			$query->$method($order);
89
		}
90
91
		// filtering
92
		$filter = $params->getFilter();
93
		if (!empty($filter)) {
94
			$this->applyFilter($query, $filter);
95
		}
96
97
		// paginate
98
		$application = $query->paginate($page, $size);
99
100
		// run response
101
		return new Found(['model' => $application]);
102
	}
103
104
	/**
105
	 * Returns one Application with the given id
106
	 * 
107
	 * @param mixed $id
108
	 * @return PayloadInterface
109
	 */
110
	public function read($id) {
111
		// read
112
		$application = $this->get($id);
113
114
		// check existence
115
		if ($application === null) {
116
			return new NotFound(['message' => 'Application not found.']);
117
		}
118
119
		return new Found(['model' => $application]);
120
	}
121
122
	/**
123
	 * Updates a Application with the given idand the provided data
124
	 * 
125
	 * @param mixed $id
126
	 * @param mixed $data
127
	 * @return PayloadInterface
128
	 */
129
	public function update($id, $data) {
130
		// find
131
		$application = $this->get($id);
132
133
		if ($application === null) {
134
			return new NotFound(['message' => 'Application not found.']);
135
		}
136
137
		// hydrate
138
		$serializer = Application::getSerializer();
139
		$application = $serializer->hydrate($application, $data);
140
141
		// validate
142
		if (!$application->validate()) {
143
			return new NotValid([
144
				'errors' => $application->getValidationFailures()
145
			]);
146
		}
147
148
		$rows = $application->save();
149
		$payload = ['model' => $application];
150
151
		if ($rows === 0) {
152
			return new NotUpdated($payload);
153
		}
154
155
		return new Updated($payload);
156
	}
157
158
	/**
159
	 * Implement this functionality at keeko\core\domain\ApplicationDomain
160
	 * 
161
	 * @param ApplicationQuery $query
162
	 * @param mixed $filter
163
	 */
164
	abstract protected function applyFilter(ApplicationQuery $query, $filter);
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...
165
166
	/**
167
	 * Returns one Application with the given id from cache
168
	 * 
169
	 * @param mixed $id
170
	 * @return Application|null
171
	 */
172
	protected function get($id) {
173
		if ($this->pool === null) {
174
			$this->pool = new Map();
0 ignored issues
show
Bug introduced by
The property pool does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
175
		} else if ($this->pool->has($id)) {
176
			return $this->pool->get($id);
177
		}
178
179
		$application = ApplicationQuery::create()->findOneById($id);
180
		$this->pool->set($id, $application);
181
182
		return $application;
183
	}
184
185
	/**
186
	 * Returns the service container
187
	 * 
188
	 * @return ServiceContainer
189
	 */
190
	abstract protected function getServiceContainer();
191
}
192