Completed
Push — master ( 7d6c96...5af7de )
by Peter
05:54
created

SearchProvider   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 9

Test Coverage

Coverage 95.24%

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 6
lcom 2
cbo 9
dl 0
loc 68
ccs 20
cts 21
cp 0.9524
rs 10
c 2
b 1
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A fetchData() 0 13 2
A getItemCount() 0 4 1
A getTotalItemCount() 0 17 2
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
58 9
	public function __construct($modelClass = null, $config = [])
59
	{
60 9
		$this->configure($modelClass, $config);
61 9
		$this->finder = new SearchFinder($this->getModel());
62 9
	}
63
64 9
	protected function fetchData()
65
	{
66
67 9
		$criteria = $this->configureFetch();
68
69 9
		$models = $criteria->getModels();
70
71 9
		if (!empty($models))
72
		{
73
			$this->finder->setModels($models);
74
		}
75 9
		return $this->finder->findAll($criteria);
76
	}
77
78 7
	public function getItemCount($refresh = false)
79
	{
80 7
		return count($this->getData($refresh));
81
	}
82
83 9
	public function getTotalItemCount()
84
	{
85 9
		if ($this->totalItemCount === null)
86
		{
87 9
			$qb = new QueryBuilder($this->getModel());
88
			/**
89
			 * TODO Must count with criteria too!
90
			 * And multi model
91
			 */
92 9
			$criteria = new SearchCriteria($this->getCriteria());
93 9
			$criteria->setLimit(false);
94 9
			$criteria->setOffset(false);
95 9
			$qb->setCriteria($criteria);
96 9
			$this->totalItemCount = $qb->count($this->getCriteria()->getSearch());
97
		}
98 9
		return $this->totalItemCount;
99
	}
100
101
}
102