SearchFinder::create()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 3
crap 2
1
<?php
2
3
/**
4
 * This software package is licensed under `AGPL-3.0-only, proprietary` license[s].
5
 *
6
 * @package maslosoft/manganel
7
 * @license AGPL-3.0-only, proprietary
8
 *
9
 * @copyright Copyright (c) Peter Maselkowski <[email protected]>
10
 * @link https://maslosoft.com/manganel/
11
 */
12
13
namespace Maslosoft\Manganel;
14
15
use Maslosoft\Addendum\Interfaces\AnnotatedInterface;
16
use Maslosoft\Mangan\Abstracts\AbstractFinder;
17
use Maslosoft\Mangan\Interfaces\FinderInterface;
18
use Maslosoft\Mangan\Traits\Finder\FinderHelpers;
19
use Maslosoft\Manganel\Adapters\Finder\ElasticSearchAdapter;
20
use Maslosoft\Manganel\Interfaces\ModelsAwareInterface;
21
use Maslosoft\Manganel\Traits\UniqueModelsAwareTrait;
22
23
/**
24
 * SearchFinder
25
 *
26
 * @author Piotr Maselkowski <pmaselkowski at gmail.com>
27
 */
28
class SearchFinder extends AbstractFinder implements FinderInterface, ModelsAwareInterface
29
{
30
31
	use FinderHelpers,
32
	  UniqueModelsAwareTrait;
33
34
	/**
35
	 * Constructor
36
	 *
37
	 * @param object|object[] $models Model or array of model instances
38
	 * @param IndexManager $im
39
	 * @param Manganel $manganel
40
	 */
41 24
	public function __construct($models, $im = null, $manganel = null)
0 ignored issues
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...
42
	{
43 24
		if (is_array($models))
44
		{
45 20
			$model = current($models);
46
		}
47
		else
48
		{
49
			// Ensure array and that model is set for further usage
50 4
			$model = $models;
51 4
			$models = [$models];
52
		}
53 24
		foreach ($models as $modelIndex => $modelSignature)
54
		{
55 24
			if (is_string($modelSignature))
56
			{
57
				$models[$modelIndex] = new $modelSignature;
58
			}
59
		}
60 24
		if (is_string($model))
61
		{
62
			$model = new $model;
63
		}
64 24
		if (null === $manganel)
65
		{
66 24
			$manganel = Manganel::create($model);
67
		}
68 24
		assert($model instanceof AnnotatedInterface);
69
70 24
		$this->setModel($model);
71 24
		$this->setModels($models);
72 24
		$this->setScopeManager(new MultiScopeManager($model, $models));
73 24
		$this->setAdapter(new ElasticSearchAdapter($models));
74
75 24
		$this->setProfiler($manganel->getProfiler());
76 24
		$this->setFinderEvents(new Helpers\MultiFinderEvents());
77 24
		$this->withCursor(false);
78 24
	}
79
80
	/**
81
	 * Create search finder instance.
82
	 *
83
	 * @param AnnotatedInterface $model
84
	 * @param IndexManager $im
85
	 * @param Manganel $manganel
86
	 * @return FinderInterface
87
	 */
88
	public static function create(AnnotatedInterface $model, $im = null, Manganel $manganel = null)
89
	{
90
		return new static($model, $im, $manganel);
91
	}
92
93 12
	protected function createModel($data)
94
	{
95
		// Do not use second param for multi model
96
		// compatibility
97 12
		assert(array_key_exists('_class', $data), 'Stored document must have `_class` field');
98 12
		return SearchArray::toModel($data);
99
	}
100
101
}
102