Completed
Push — master ( fe78a2...c84a78 )
by Peter
06:36
created

SearchProvider   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 11

Test Coverage

Coverage 91.89%

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 13
lcom 1
cbo 11
dl 0
loc 94
ccs 34
cts 37
cp 0.9189
rs 10
c 2
b 1
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B fetchData() 0 54 9
A getItemCount() 0 4 1
A getTotalItemCount() 0 9 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 7
	public function __construct($modelClass = null, $config = [])
53
	{
54 7
		$this->configure($modelClass, $config);
55 7
	}
56
57 7
	protected function fetchData()
58
	{
59
60 7
		$criteria = $this->configureFetch();
61
62
		/**
63
		 * TODO Refactor this into SearchFinder class
64
		 */
65 7
		$qb = new QueryBuilder();
66 7
		if ($criteria instanceof SearchCriteria)
67
		{
68 7
			$models = $criteria->getModels();
69 7
			if (!empty($models))
70
			{
71
				$qb->add($models);
72
			}
73
		}
74 7
		$model = $this->getModel();
75 7
		if (!Event::handled($model, FinderInterface::EventBeforeFind))
0 ignored issues
show
Bug Best Practice introduced by
The expression \Maslosoft\Mangan\Events...rface::EventBeforeFind) of type boolean|null is loosely compared to false; this is ambiguous if the boolean can be false. You might want to explicitly use !== null instead.

If an expression can have both false, and null as possible values. It is generally a good practice to always use strict comparison to clearly distinguish between those two values.

$a = canBeFalseAndNull();

// Instead of
if ( ! $a) { }

// Better use one of the explicit versions:
if ($a !== null) { }
if ($a !== false) { }
if ($a !== null && $a !== false) { }
Loading history...
76
		{
77
			return [];
78
		}
79
80 7
		$modelCriteria = null;
81
82
		// This check is required for plain php objects
83 7
		if ($model instanceof WithCriteriaInterface)
84
		{
85
			$modelCriteria = $model->getDbCriteria();
86
		}
87
88 7
		$criteria->mergeWith($modelCriteria);
89 7
		if (!empty($model))
90
		{
91 7
			$qb->add($model);
92
		}
93 7
		$qb->setCriteria($criteria);
94 7
		$rawResults = $qb->search($criteria->getSearch());
95 7
		$results = [];
96 7
		foreach ($rawResults as $data)
97
		{
98 7
			$model = SearchArray::toModel($data['_source']);
99 7
			if ($model instanceof IndexAwareInterface)
100
			{
101 1
				$model->setIndex($data['_index']);
102
			}
103 7
			if ($model instanceof ScoreAwareInterface)
104
			{
105 1
				$model->setScore($data['_score']);
106
			}
107 7
			$results[] = $model;
108
		}
109 7
		return $results;
110
	}
111
112 5
	public function getItemCount($refresh = false)
113
	{
114 5
		return count($this->getData($refresh));
115
	}
116
117 7
	public function getTotalItemCount()
118
	{
119 7
		if ($this->totalItemCount === null)
120
		{
121 7
			$qb = new QueryBuilder($this->getModel());
122 7
			$this->totalItemCount = $qb->count($this->getCriteria()->getSearch());
123
		}
124 7
		return $this->totalItemCount;
125
	}
126
127
}
128