Completed
Push — master ( 5af7de...d3c2fe )
by Peter
04:23
created

SearchProvider   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Test Coverage

Coverage 78.56%

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 9
c 2
b 1
f 0
lcom 1
cbo 9
dl 0
loc 81
ccs 22
cts 28
cp 0.7856
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A stop() 0 4 1
A fetchData() 0 16 3
A getItemCount() 0 4 1
A getTotalItemCount() 0 21 3
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\Events\Event;
16
use Maslosoft\Mangan\Interfaces\DataProviderInterface;
17
use Maslosoft\Mangan\Interfaces\FinderInterface;
18
use Maslosoft\Mangan\Interfaces\WithCriteriaInterface;
19
use Maslosoft\Mangan\Traits\DataProvider\ConfigureTrait;
20
use Maslosoft\Mangan\Traits\DataProvider\CriteriaTrait;
21
use Maslosoft\Mangan\Traits\DataProvider\DataTrait;
22
use Maslosoft\Mangan\Traits\DataProvider\PaginationTrait;
23
use Maslosoft\Mangan\Traits\ModelAwareTrait;
24
use Maslosoft\Mangan\Traits\SortAwareTrait;
25
use Maslosoft\Manganel\Interfaces\IndexAwareInterface;
26
use Maslosoft\Manganel\Interfaces\ScoreAwareInterface;
27
28
/**
29
 * SearchProvider
30
 *
31
 * @method SearchCriteria getCriteria()
32
 * @author Piotr Maselkowski <pmaselkowski at gmail.com>
33
 */
34
class SearchProvider implements DataProviderInterface
35
{
36
37
	use ConfigureTrait,
38
	  CriteriaTrait,
39
	  DataTrait,
40
	  ModelAwareTrait,
41
	  PaginationTrait,
42
	  SortAwareTrait;
43
44
	const CriteriaClass = SearchCriteria::class;
45
46
	/**
47
	 * Total items count cache
48
	 * @var int
49
	 */
50
	private $totalItemCount = null;
51
52
	/**
53
	 * Finder instance
54
	 * @var SearchFinder
55
	 */
56
	private $finder = null;
57
	private $isStopped = false;
58
59 9
	public function __construct($modelClass = null, $config = [])
60
	{
61 9
		$this->configure($modelClass, $config);
62 9
		$this->finder = new SearchFinder($this->getModel());
63 9
	}
64
65
	public function stop($doStop = true)
66
	{
67
		$this->isStopped = $doStop;
68
	}
69
70 9
	protected function fetchData()
71
	{
72 9
		if ($this->isStopped)
73
		{
74
			return [];
75
		}
76 9
		$criteria = $this->configureFetch();
77
78 9
		$models = $criteria->getModels();
79
80 9
		if (!empty($models))
81
		{
82
			$this->finder->setModels($models);
83
		}
84 9
		return $this->finder->findAll($criteria);
85
	}
86
87 7
	public function getItemCount($refresh = false)
88
	{
89 7
		return count($this->getData($refresh));
90
	}
91
92 9
	public function getTotalItemCount()
93
	{
94 9
		if ($this->isStopped)
95
		{
96
			$this->totalItemCount = 0;
97
		}
98 9
		if ($this->totalItemCount === null)
99
		{
100 9
			$qb = new QueryBuilder($this->getModel());
101
			/**
102
			 * TODO Must count with criteria too!
103
			 * And multi model
104
			 */
105 9
			$criteria = new SearchCriteria($this->getCriteria());
106 9
			$criteria->setLimit(false);
107 9
			$criteria->setOffset(false);
108 9
			$qb->setCriteria($criteria);
109 9
			$this->totalItemCount = $qb->count($this->getCriteria()->getSearch());
110
		}
111 9
		return $this->totalItemCount;
112
	}
113
114
}
115