Completed
Push — master ( 6a00f8...075576 )
by Peter
07:41
created

ConfigureTrait::configureFetch()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 21
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4.016

Importance

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