Completed
Push — master ( 0dfb02...aa2861 )
by Peter
13:57
created

SearchProvider::getTotalItemCount()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 23
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 3.0052

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 23
ccs 11
cts 12
cp 0.9167
rs 9.0856
c 1
b 0
f 0
cc 3
eloc 12
nc 4
nop 0
crap 3.0052
1
<?php
2
3
/**
4
 * This software package is licensed under `AGPL, Commercial` license[s].
5
 *
6
 * @package maslosoft/manganel
7
 * @license AGPL, Commercial
8
 *
9
 * @copyright Copyright (c) Peter Maselkowski <[email protected]>
10
 * @link http://maslosoft.com/manganel/
11
 */
12
13
namespace Maslosoft\Manganel;
14
15
use Maslosoft\Mangan\Interfaces\DataProviderInterface;
16
use Maslosoft\Mangan\Traits\DataProvider\ConfigureTrait;
17
use Maslosoft\Mangan\Traits\DataProvider\CriteriaTrait;
18
use Maslosoft\Mangan\Traits\DataProvider\DataTrait;
19
use Maslosoft\Mangan\Traits\DataProvider\PaginationTrait;
20
use Maslosoft\Mangan\Traits\ModelAwareTrait;
21
use Maslosoft\Mangan\Traits\SortAwareTrait;
22
23
/**
24
 * SearchProvider
25
 *
26
 * @method SearchCriteria getCriteria()
27
 * @author Piotr Maselkowski <pmaselkowski at gmail.com>
28
 */
29
class SearchProvider implements DataProviderInterface
30
{
31
32
	use ConfigureTrait,
33
	  DataTrait,
34
	  ModelAwareTrait,
35
	  PaginationTrait,
36
	  SortAwareTrait,
37
	  CriteriaTrait
38
	{
39
		CriteriaTrait::setCriteria as traitSetCriteria;
40
	}
41
42
	const CriteriaClass = SearchCriteria::class;
43
44
	/**
45
	 * Total items count cache
46
	 * @var int
47
	 */
48
	private $totalItemCount = null;
49
50
	/**
51
	 * Finder instance
52
	 * @var SearchFinder
53
	 */
54
	private $finder = null;
55
	private $isStopped = false;
56
57 11
	public function __construct($modelClass = null, $config = [])
58
	{
59 11
		if (is_array($modelClass))
60
		{
61 2
			$models = $modelClass;
62
		}
63
		else
64
		{
65 9
			$models = [$modelClass];
66
		}
67 11
		foreach ($models as $modelClass)
68
		{
69 11
			$this->configure($modelClass, $config);
70
		}
71 11
		$this->finder = new SearchFinder($models);
72 11
		$criteria = $this->getCriteria();
73 11
		assert($criteria instanceof SearchCriteria);
74 11
		$criteria->setModels($models);
75 11
	}
76
77
	public function stop($doStop = true)
78
	{
79
		$this->isStopped = $doStop;
80
	}
81
82 11
	protected function fetchData()
83
	{
84 11
		if ($this->isStopped)
85
		{
86
			return [];
87
		}
88 11
		$criteria = $this->configureFetch();
89
90 11
		$models = $criteria->getModels();
91
92 11
		if (!empty($models))
93
		{
94 11
			$this->finder->setModels($models);
95
		}
96 11
		return $this->finder->findAll($criteria);
97
	}
98
99 11
	public function setCriteria($criteria)
100
	{
101 11
		assert($criteria instanceof SearchCriteria);
102 11
		$criteria->setModels($this->finder->getModels());
103 11
		return $this->traitSetCriteria($criteria);
104
	}
105
106 9
	public function getItemCount($refresh = false)
107
	{
108 9
		return count($this->getData($refresh));
109
	}
110
111 11
	public function getTotalItemCount()
112
	{
113 11
		if ($this->isStopped)
114
		{
115
			$this->totalItemCount = 0;
116
		}
117 11
		if ($this->totalItemCount === null)
118
		{
119 11
			$qb = new QueryBuilder($this->getModel());
120
121
			/**
122
			 * TODO Must count with criteria too!
123
			 * And multi model
124
			 */
125 11
			$criteria = new SearchCriteria($this->getCriteria());
126 11
			$criteria->setModels($this->finder->getModels());
127 11
			$criteria->setLimit(false);
128 11
			$criteria->setOffset(false);
129 11
			$qb->setCriteria($criteria);
130 11
			$this->totalItemCount = $qb->count($this->getCriteria()->getSearch());
131
		}
132 11
		return $this->totalItemCount;
133
	}
134
135
}
136