Completed
Push — master ( 102c37...c32e8f )
by Peter
06:31
created

ConfigureTrait   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 139
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 73.81%

Importance

Changes 0
Metric Value
wmc 21
c 0
b 0
f 0
lcom 1
cbo 4
dl 0
loc 139
ccs 31
cts 42
cp 0.7381
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A configureFetch() 0 21 4
getSort() 0 1 ?
getPagination() 0 1 ?
getTotalItemCount() 0 1 ?
getCriteria() 0 1 ?
getModel() 0 1 ?
setModel() 0 1 ?
F configure() 0 73 17
1
<?php
2
3
/**
4
 * This software package is licensed under AGPL or Commercial license.
5
 *
6
 * @package maslosoft/mangan
7
 * @licence AGPL or Commercial
8
 * @copyright Copyright (c) Piotr Masełkowski <[email protected]>
9
 * @copyright Copyright (c) Maslosoft
10
 * @copyright Copyright (c) Others as mentioned in code
11
 * @link https://maslosoft.com/mangan/
12
 */
13
14
namespace Maslosoft\Mangan\Traits\DataProvider;
15
16
use Maslosoft\Addendum\Interfaces\AnnotatedInterface;
17
use Maslosoft\Mangan\Exceptions\ManganException;
18
use Maslosoft\Mangan\Interfaces\Criteria\LimitableInterface;
19
use Maslosoft\Mangan\Interfaces\Criteria\MergeableInterface;
20
use Maslosoft\Mangan\Interfaces\CriteriaAwareInterface;
21
use Maslosoft\Mangan\Interfaces\CriteriaInterface;
22
use Maslosoft\Mangan\Interfaces\PaginationInterface;
23
use Maslosoft\Mangan\Interfaces\SortInterface;
24
use Maslosoft\Mangan\Interfaces\WithCriteriaInterface;
25
use Maslosoft\Mangan\Meta\ManganMeta;
26
use Maslosoft\Mangan\Pagination;
27
28
/**
29
 * ConfigureTrait
30
 *
31
 * @author Piotr Maselkowski <pmaselkowski at gmail.com>
32
 */
33
trait ConfigureTrait
34
{
35
36 4
	protected function configure($modelClass, $config)
37
	{
38 4
		if (!empty($modelClass))
39
		{
40 4
			if (is_string($modelClass))
41
			{
42
				$this->setModel(new $modelClass);
43
			}
44 4
			elseif (is_object($modelClass))
45
			{
46 4
				$this->setModel($modelClass);
47
			}
48
			else
49
			{
50
				throw new ManganException('Invalid model type for ' . static::class);
51
			}
52
		}
53
54 4
		$model = $this->getModel();
55
56
		// Set criteria from model
57 4
		$criteria = $this->getCriteria();
58 4
		if (!empty($model) && $criteria instanceof MergeableInterface)
59
		{
60
			// NOTE: WithCriteria and CriteriaAware have just slightly different method names
61 4
			if ($model instanceof WithCriteriaInterface)
62
			{
63 1
				$criteria->mergeWith($model->getDbCriteria());
64
			}
65
			elseif ($model instanceof CriteriaAwareInterface)
66
			{
67
				$criteria->mergeWith($model->getCriteria());
68
			}
69
		}
70
71
		// Merge criteria from configuration
72 4
		if (isset($config['criteria']))
73
		{
74
			$criteria->mergeWith($config['criteria']);
75
			unset($config['criteria']);
76
		}
77
78
		// Merge limit from configuration
79 4
		if (isset($config['limit']) && $config['limit'] > 0)
80
		{
81 1
			$criteria->setLimit($config['limit']);
82 1
			unset($config['limit']);
83
		}
84
85
		// Merge sorting from configuration
86 4
		if (isset($config['sort']))
87
		{
88
			// Apply default sorting if criteria does not have sort configured
89
			$sort = $criteria->getSort();
90
			if (isset($config['sort']['defaultOrder']) && empty($sort))
91
			{
92
				$criteria->setSort($config['sort']['defaultOrder']);
93
			}
94
			unset($config['sort']);
95
		}
96
97 4
		if (!empty($model) && !$criteria->getSelect())
98
		{
99 4
			$fields = array_keys(ManganMeta::create($model)->fields());
100 4
			$selected = array_fill_keys($fields, true);
101 4
			$criteria->setSelect($selected);
102
		}
103
104 4
		foreach ($config as $key => $value)
105
		{
106 2
			$this->$key = $value;
107
		}
108 4
	}
109
110
	/**
111
	 * Configure limits, sorting for fetching data
112
	 * @return CriteriaInterface
113
	 */
114 4
	protected function configureFetch()
115
	{
116
		// Setup required objects
117 4
		$sort = $this->getSort();
118 4
		$criteria = $this->getCriteria();
119 4
		$pagination = $this->getPagination();
120
121
		// Apply limits if required
122 4
		if ($pagination !== false && $criteria instanceof LimitableInterface)
123
		{
124 4
			$pagination->setCount($this->getTotalItemCount());
125 4
			$pagination->apply($criteria);
126
		}
127
128
		// Apply sort if required
129 4
		if ($sort->isSorted())
130
		{
131
			$criteria->setSort($sort);
132
		}
133 4
		return $criteria;
134
	}
135
136
	/**
137
	 * Returns the sort object.
138
	 * @return SortInterface the sorting object. If this is false, it means the sorting is disabled.
139
	 */
140
	abstract public function getSort();
141
142
	/**
143
	 * Returns the pagination object.
144
	 * @param string $className the pagination object class name, use this param to override default pagination class.
145
	 * @return PaginationInterface|Pagination|false the pagination object. If this is false, it means the pagination is disabled.
146
	 */
147
	abstract public function getPagination($className = Pagination::class);
148
149
	/**
150
	 * Returns the total number of data items.
151
	 * When {@link pagination} is set false, this returns the same value as {@link itemCount}.
152
	 * @return integer total number of possible data items.
153
	 */
154
	abstract public function getTotalItemCount();
155
156
	/**
157
	 * @return CriteriaInterface
158
	 */
159
	abstract public function getCriteria();
160
161
	/**
162
	 * @return AnnotatedInterface
163
	 */
164
	abstract public function getModel();
165
166
	/**
167
	 * @param $model AnnotatedInterface
168
	 * @return static
169
	 */
170
	abstract public function setModel(AnnotatedInterface $model);
171
}
172