SearchProvider::stop()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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