Completed
Push — master ( 6f46dc...2c5f1b )
by Peter
08:02 queued 11s
created

ConfigureTrait::configure()   F

Complexity

Conditions 18
Paths 1153

Size

Total Lines 79
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 25
CRAP Score 25.5557

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 79
ccs 25
cts 35
cp 0.7143
rs 2.1826
cc 18
eloc 35
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 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 3
			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 (isset($config['pagination']))
98
		{
99 2
			$this->setPagination($config['pagination']);
0 ignored issues
show
Bug introduced by
It seems like setPagination() 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...
100 2
			unset($config['pagination']);
101
		}
102
103 4
		if (!empty($model) && !$criteria->getSelect())
104
		{
105 4
			$fields = array_keys(ManganMeta::create($model)->fields());
106 4
			$selected = array_fill_keys($fields, true);
107 4
			$criteria->setSelect($selected);
108
		}
109
		
110 4
		foreach ($config as $key => $value)
111
		{
112
			$this->$key = $value;
113
		}
114 4
	}
115
116
	/**
117
	 * Configure limits, sorting for fetching data
118
	 * @return CriteriaInterface
119
	 */
120 4
	protected function configureFetch()
121
	{
122
		// Setup required objects
123 4
		$sort = $this->getSort();
124 4
		$criteria = $this->getCriteria();
125 4
		$pagination = $this->getPagination();
126
127
		// Apply limits if required
128 4
		if ($pagination !== false && $criteria instanceof LimitableInterface)
129
		{
130 4
			$pagination->setCount($this->getTotalItemCount());
131 4
			$pagination->apply($criteria);
132
		}
133
134
		// Apply sort if required
135 4
		if ($sort->isSorted())
136
		{
137
			$criteria->setSort($sort);
138
		}
139 4
		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
	 * Returns the total number of data items.
157
	 * When {@link pagination} is set false, this returns the same value as {@link itemCount}.
158
	 * @return integer total number of possible data items.
159
	 */
160
	abstract public function getTotalItemCount();
161
162
	/**
163
	 * @return CriteriaInterface
164
	 */
165
	abstract public function getCriteria();
166
167
	/**
168
	 * @return AnnotatedInterface
169
	 */
170
	abstract public function getModel();
171
172
	/**
173
	 * @param $model AnnotatedInterface
174
	 * @return static
175
	 */
176
	abstract public function setModel(AnnotatedInterface $model);
177
}
178