Completed
Push — master ( b30b3b...5895ce )
by Thomas
09:27
created

RegionAreaDomainTrait   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
B paginate() 0 27 3
A read() 0 14 2
applyFilter() 0 1 ?
getServiceContainer() 0 1 ?
1
<?php
2
namespace keeko\core\domain\base;
3
4
use keeko\core\model\RegionArea;
5
use keeko\core\model\RegionAreaQuery;
6
use keeko\framework\service\ServiceContainer;
7
use keeko\framework\domain\payload\Found;
8
use keeko\framework\domain\payload\NotFound;
9
use Tobscure\JsonApi\Parameters;
10
use keeko\framework\utils\NameUtils;
11
12
/**
13
 */
14
trait RegionAreaDomainTrait {
15
16
	/**
17
	 * @param Parameters $params
18
	 */
19
	public function paginate(Parameters $params) {
20
		$sysPrefs = $this->getServiceContainer()->getPreferenceLoader()->getSystemPreferences();
21
		$defaultSize = $sysPrefs->getPaginationSize();
22
		$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...
23
		$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...
24
25
		$query = RegionAreaQuery::create();
26
27
		// sorting
28
		$sort = $params->getSort(RegionArea::getSerializer()->getSortFields());
29
		foreach ($sort as $field => $order) {
30
			$method = 'orderBy' . NameUtils::toStudlyCase($field);
31
			$query->$method($order);
32
		}
33
34
		// filtering
35
		$filter = $params->getFilter();
36
		if (!empty($filter)) {
37
			$this->applyFilter($query, $filter);
0 ignored issues
show
Unused Code introduced by
The call to RegionAreaDomainTrait::applyFilter() has too many arguments starting with $filter.

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...
38
		}
39
40
		// paginate
41
		$regionArea = $query->paginate($page, $size);
42
43
		// run response
44
		return new Found(['model' => $regionArea]);
45
	}
46
47
	/**
48
	 * @param mixed $id
49
	 */
50
	public function read($id) {
51
		// read
52
		$regionArea = RegionAreaQuery::create()->findOneById($id);
53
54
		// check existence
55
		if ($regionArea === null) {
56
			$payload = new NotFound(['message' => 'RegionArea not found.']);
57
		} else {
58
			$payload = new Found(['model' => $regionArea]);
59
		}
60
61
		// run response
62
		return $payload;
63
	}
64
65
	/**
66
	 * Implement this functionality at keeko\core\domain\RegionAreaDomain
67
	 * 
68
	 * @param RegionAreaQuery $query
69
	 */
70
	abstract protected function applyFilter(RegionAreaQuery $query);
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...
71
72
	/**
73
	 * Returns the service container
74
	 * 
75
	 * @return ServiceContainer
76
	 */
77
	abstract protected function getServiceContainer();
78
}
79