Completed
Push — multimodel-dp ( 745c1d...bcd65b )
by Peter
03:53
created

SearchProvider::fetchData()   C

Complexity

Conditions 10
Paths 63

Size

Total Lines 54
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 110

Importance

Changes 2
Bugs 1 Features 0
Metric Value
dl 0
loc 54
ccs 0
cts 45
cp 0
rs 6.8372
c 2
b 1
f 0
cc 10
eloc 27
nc 63
nop 0
crap 110

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
	public function __construct($modelClass = null, $config = [])
53
	{
54
		$this->configure($modelClass, $config);
55
	}
56
57
	protected function fetchData()
58
	{
59
60
		$criteria = $this->configureFetch();
61
62
		/**
63
		 * TODO Refactor this into SearchFinder class
64
		 */
65
		$qb = new QueryBuilder();
66
		if ($criteria instanceof SearchCriteria)
67
		{
68
			$models = $criteria->getModels();
69
			if (!empty($models))
70
			{
71
				$qb->add($models);
0 ignored issues
show
Documentation introduced by
$models is of type array, but the function expects a object<Maslosoft\Addendu...ces\AnnotatedInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
72
			}
73
		}
74
		$model = $this->getModel();
75
		if (!empty($model) && !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
		$modelCriteria = null;
81
82
		// This check is required for plain php objects
83
		if ($model instanceof WithCriteriaInterface)
84
		{
85
			$modelCriteria = $model->getDbCriteria();
86
		}
87
88
		$criteria->mergeWith($modelCriteria);
89
		if (!empty($model))
90
		{
91
			$qb->add($model);
92
		}
93
		$qb->setCriteria($criteria);
94
		$rawResults = $qb->search($criteria->getSearch());
95
		$results = [];
96
		foreach ($rawResults as $data)
97
		{
98
			$model = SearchArray::toModel($data['_source']);
99
			if ($model instanceof IndexAwareInterface)
100
			{
101
				$model->setIndex($data['_index']);
102
			}
103
			if ($model instanceof ScoreAwareInterface)
104
			{
105
				$model->setScore($data['_score']);
106
			}
107
			$results[] = $model;
108
		}
109
		return $results;
110
	}
111
112
	public function getItemCount($refresh = false)
113
	{
114
		return count($this->getData($refresh));
115
	}
116
117
	public function getTotalItemCount()
118
	{
119
		if ($this->totalItemCount === null)
120
		{
121
			$qb = new QueryBuilder($this->getModel());
122
			/**
123
			 * TODO Must count with criteria too!
124
			 * And multi model
125
			 */
126
			$criteria = new SearchCriteria($this->getCriteria());
127
			$criteria->setLimit(false);
128
			$criteria->setOffset(false);
129
			$qb->setCriteria($criteria);
130
			$this->totalItemCount = $qb->count($this->getCriteria()->getSearch());
131
		}
132
		return $this->totalItemCount;
133
	}
134
135
}
136