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

SearchFinder::__construct()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 27
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 3.0021

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 27
ccs 15
cts 16
cp 0.9375
rs 8.8571
cc 3
eloc 16
nc 4
nop 3
crap 3.0021
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\Traits\Finder\FinderHelpers;
9
use Maslosoft\Manganel\Adapters\Finder\ElasticSearchAdapter;
10
use Maslosoft\Manganel\Interfaces\ModelsAwareInterface;
11
use Maslosoft\Manganel\Traits\UniqueModelsAwareTrait;
12
13
/**
14
 * SearchFinder
15
 *
16
 * @author Piotr Maselkowski <pmaselkowski at gmail.com>
17
 */
18
class SearchFinder extends AbstractFinder implements FinderInterface, ModelsAwareInterface
19
{
20
21
	use FinderHelpers,
22
	  UniqueModelsAwareTrait;
23
24
	/**
25
	 * Constructor
26
	 *
27
	 * @param object|object[] $models Model or array of model instances
28
	 * @param IndexManager $im
29
	 * @param Manganel $manganel
30
	 */
31 28
	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...
32
	{
33 28
		if (is_array($models))
34
		{
35
			$model = current($models);
36
		}
37
		else
38
		{
39
			// Ensure array and that model is set for further usage
40 28
			$model = $models;
41 28
			$models = [$models];
42
		}
43 28
		if (null === $manganel)
44
		{
45 28
			$manganel = Manganel::create($model);
46
		}
47 28
		assert($model instanceof AnnotatedInterface);
48
49 28
		$this->setModel($model);
50 28
		$this->setModels($models);
51 28
		$this->setScopeManager(new MultiScopeManager($model, $models));
52 28
		$this->setAdapter(new ElasticSearchAdapter($models));
53
54 28
		$this->setProfiler($manganel->getProfiler());
55 28
		$this->setFinderEvents(new Helpers\MultiFinderEvents());
56 28
		$this->withCursor(false);
57 28
	}
58
59
	/**
60
	 * Create search finder instance.
61
	 *
62
	 * @param AnnotatedInterface $model
63
	 * @param IndexManager $im
64
	 * @param Manganel $manganel
65
	 * @return FinderInterface
66
	 */
67
	public static function create(AnnotatedInterface $model, $im = null, Manganel $manganel = null)
68
	{
69
		return new static($model, $im, $manganel);
70
	}
71
72 19
	protected function createModel($data)
73
	{
74
		// Do not use second param for multi model
75
		// compatibility
76 19
		assert(array_key_exists('_class', $data), 'Stored document must have `_class` field');
77 19
		return SearchArray::toModel($data);
78
	}
79
80
}
81