ConfigureTrait::configure()   F
last analyzed

Complexity

Conditions 18
Paths 1153

Size

Total Lines 79

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 25
CRAP Score 25.5557

Importance

Changes 0
Metric Value
dl 0
loc 79
ccs 25
cts 35
cp 0.7143
rs 0.7
c 0
b 0
f 0
cc 18
nc 1153
nop 2
crap 25.5557

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 6
	protected function configure($modelClass, $config)
37
	{
38 6
		if (!empty($modelClass))
39
		{
40 6
			if (is_string($modelClass))
41
			{
42
				$this->setModel(new $modelClass);
43
			}
44 6
			elseif (is_object($modelClass))
45
			{
46 6
				$this->setModel($modelClass);
47
			}
48
			else
49
			{
50
				throw new ManganException('Invalid model type for ' . static::class);
51
			}
52
		}
53
54 6
		$model = $this->getModel();
55
56
		// Set criteria from model
57 6
		$criteria = $this->getCriteria();
58 6
		if (!empty($model) && $criteria instanceof MergeableInterface)
59
		{
60
			// NOTE: WithCriteria and CriteriaAware have just slightly different method names
61 6
			if ($model instanceof WithCriteriaInterface)
62
			{
63 3
				$criteria->mergeWith($model->getDbCriteria());
64
			}
65 3
			elseif ($model instanceof CriteriaAwareInterface)
66
			{
67
				$criteria->mergeWith($model->getCriteria());
68
			}
69
		}
70
71
		// Merge criteria from configuration
72 6
		if (isset($config['criteria']))
73
		{
74
			$criteria->mergeWith($config['criteria']);
75
			unset($config['criteria']);
76
		}
77
78
		// Merge limit from configuration
79 6
		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 6
		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 6
		if (isset($config['pagination']))
98
		{
99 2
			$this->setPagination($config['pagination']);
100 2
			unset($config['pagination']);
101
		}
102
103 6
		if (!empty($model) && !$criteria->getSelect())
104
		{
105 6
			$fields = array_keys(ManganMeta::create($model)->fields());
106 6
			$selected = array_fill_keys($fields, true);
107 6
			$criteria->setSelect($selected);
108
		}
109
		
110 6
		foreach ($config as $key => $value)
111
		{
112
			$this->$key = $value;
113
		}
114 6
	}
115
116
	/**
117
	 * Configure limits, sorting for fetching data
118
	 * @return CriteriaInterface
119
	 */
120 6
	protected function configureFetch()
121
	{
122
		// Setup required objects
123 6
		$sort = $this->getSort();
124 6
		$criteria = $this->getCriteria();
125 6
		$pagination = $this->getPagination();
126
127
		// Apply limits if required
128 6
		if ($pagination !== false && $criteria instanceof LimitableInterface)
129
		{
130 6
			$pagination->setCount($this->getTotalItemCount());
131 6
			$pagination->apply($criteria);
132
		}
133
134
		// Apply sort if required
135 6
		if ($sort->isSorted())
136
		{
137
			$criteria->setSort($sort);
138
		}
139 6
		return $criteria;
140
	}
141
142
	/**
143
	 * Returns the sort object.
144
	 * @return SortInterface the sorting object. If this is false, it means the sorting is disabled.
145
	 */
146
	abstract public function getSort();
147
148
	/**
149
	 * Returns the pagination object.
150
	 * @param string $className the pagination object class name, use this param to override default pagination class.
151
	 * @return PaginationInterface|Pagination|false the pagination object. If this is false, it means the pagination is disabled.
152
	 */
153
	abstract public function getPagination($className = Pagination::class);
154
155
	/**
156
	 * Set pagination
157
	 * @param boolean|array|PaginationInterface $pagination
158
	 * @return static
159
	 */
160
	abstract public function setPagination($pagination);
161
162
	/**
163
	 * Returns the total number of data items.
164
	 * When {@link pagination} is set false, this returns the same value as {@link itemCount}.
165
	 * @return integer total number of possible data items.
166
	 */
167
	abstract public function getTotalItemCount();
168
169
	/**
170
	 * @return CriteriaInterface
171
	 */
172
	abstract public function getCriteria();
173
174
	/**
175
	 * @return AnnotatedInterface
176
	 */
177
	abstract public function getModel();
178
179
	/**
180
	 * @param $model AnnotatedInterface
181
	 * @return static
182
	 */
183
	abstract public function setModel(AnnotatedInterface $model);
184
}
185