Completed
Push — master ( c65b2e...91a3af )
by Peter
05:35
created

SearchProvider::setSort()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 1
nc 1
nop 1
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\Manganel;
10
11
use Maslosoft\Mangan\Interfaces\DataProviderInterface;
12
use Maslosoft\Mangan\Traits\DataProvider\ConfigureTrait;
13
use Maslosoft\Mangan\Traits\DataProvider\CriteriaTrait;
14
use Maslosoft\Mangan\Traits\DataProvider\DataTrait;
15
use Maslosoft\Mangan\Traits\DataProvider\PaginationTrait;
16
use Maslosoft\Mangan\Traits\ModelAwareTrait;
17
use Maslosoft\Mangan\Traits\SortAwareTrait;
18
19
/**
20
 * SearchProvider
21
 *
22
 * @method SearchCriteria getCriteria()
23
 * @author Piotr Maselkowski <pmaselkowski at gmail.com>
24
 */
25
class SearchProvider implements DataProviderInterface
26
{
27
28
	use ConfigureTrait,
29
	  CriteriaTrait,
30
	  DataTrait,
31
	  ModelAwareTrait,
32
	  PaginationTrait,
33
	  SortAwareTrait;
34
35
	const CriteriaClass = SearchCriteria::class;
36
37
	/**
38
	 * Total items count cache
39
	 * @var int
40
	 */
41
	private $totalItemCount = null;
42
43 2
	public function __construct($modelClass, $config = [])
44
	{
45 2
		$this->configure($modelClass, $config);
46 2
	}
47
48 2
	protected function fetchData()
49
	{
50 2
		$criteria = $this->configureFetch();
51
52 2
		$qb = new QueryBuilder($this->getModel());
53 2
		$qb->setCriteria($criteria);
54 2
		$rawResults = $qb->search($criteria->getSearch());
55 2
		$results = [];
56 2
		foreach ($rawResults as $data)
57
		{
58 2
			$results[] = SearchArray::toModel($data['_source']);
59
		}
60 2
		return $results;
61
	}
62
63 2
	public function getItemCount($refresh = false)
64
	{
65 2
		return count($this->getData($refresh));
66
	}
67
68 2
	public function getTotalItemCount()
69
	{
70 2
		if ($this->totalItemCount === null)
71
		{
72 2
			$qb = new QueryBuilder($this->getModel());
73 2
			$this->totalItemCount = $qb->count($this->getCriteria()->getSearch());
74
		}
75 2
		return $this->totalItemCount;
76
	}
77
78
}
79