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

SearchFinder   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 9

Test Coverage

Coverage 71.43%

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 9
dl 0
loc 66
ccs 15
cts 21
cp 0.7143
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 30 3
A create() 0 4 1
A createModel() 0 7 1
1
<?php
2
3
namespace Maslosoft\Manganel;
4
5
use Maslosoft\Addendum\Interfaces\AnnotatedInterface;
6
use Maslosoft\Mangan\Abstracts\AbstractFinder;
7
use Maslosoft\Mangan\Interfaces\FinderInterface;
8
use Maslosoft\Mangan\Profillers\NullProfiler;
9
use Maslosoft\Mangan\Traits\Finder\FinderHelpers;
10
use Maslosoft\Manganel\Adapters\Finder\ElasticSearchAdapter;
11
use Maslosoft\Manganel\Interfaces\ModelsAwareInterface;
12
use Maslosoft\Manganel\Traits\UniqueModelsAwareTrait;
13
14
/**
15
 * SearchFinder
16
 *
17
 * @author Piotr Maselkowski <pmaselkowski at gmail.com>
18
 */
19
class SearchFinder extends AbstractFinder implements FinderInterface, ModelsAwareInterface
20
{
21
22
	use FinderHelpers,
23
	  UniqueModelsAwareTrait;
24
25
	/**
26
	 * Constructor
27
	 *
28
	 * @param object|object[] $models Model or array of model instances
29
	 * @param IndexManager $im
30
	 * @param Manganel $manganel
31
	 */
32 4
	public function __construct($models, $im = null, $manganel = null)
1 ignored issue
show
Unused Code introduced by
The parameter $im is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
33
	{
34 4
		if (is_array($models))
35
		{
36
			$model = current($model);
0 ignored issues
show
Bug introduced by
The variable $model seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
37
		}
38
		else
39
		{
40
			// Ensure array and that model is set for further usage
41 4
			$model = $models;
42 4
			$models = [$models];
43
		}
44 4
		if (null === $manganel)
45
		{
46 4
			$manganel = Manganel::create($model);
0 ignored issues
show
Unused Code introduced by
$manganel is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
47
		}
48 4
		assert($model instanceof AnnotatedInterface);
49
50 4
		$this->setModel($model);
51 4
		$this->setModels($models);
52 4
		$this->setScopeManager(new MultiScopeManager($model, $models));
53 4
		$this->setAdapter(new ElasticSearchAdapter($models));
54
55
		/**
56
		 * TODO Use profiler here if any available
57
		 */
58 4
		$this->setProfiler(new NullProfiler);
59 4
		$this->setFinderEvents(new Helpers\MultiFinderEvents());
60 4
		$this->withCursor(false);
61 4
	}
62
63
	/**
64
	 * Create search finder instance.
65
	 *
66
	 * @param AnnotatedInterface $model
67
	 * @param IndexManager $im
68
	 * @param Manganel $manganel
69
	 * @return FinderInterface
70
	 */
71
	public static function create(AnnotatedInterface $model, $im = null, Manganel $manganel = null)
72
	{
73
		return new static($model, $im, $manganel);
74
	}
75
76
	protected function createModel($data)
77
	{
78
		// Do not use second param for multi model
79
		// compatibility
80
		assert(array_key_exists('_class', $data), 'Stored document must have `_class` field');
81
		return SearchArray::toModel($data);
82
	}
83
84
}
85