Completed
Push — master ( d1d4a9...91184f )
by Peter
12:35
created

Finder::createModel()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3.0261

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 6
cts 7
cp 0.8571
rs 9.6
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 3.0261
1
<?php
2
3
/**
4
 * This software package is licensed under AGPL or Commercial license.
5
 *
6
 * @package   maslosoft/mangan
7
 * @licence   AGPL or Commercial
8
 * @copyright Copyright (c) Piotr Masełkowski <[email protected]>
9
 * @copyright Copyright (c) Maslosoft
10
 * @copyright Copyright (c) Others as mentioned in code
11
 * @link      https://maslosoft.com/mangan/
12
 */
13
14
namespace Maslosoft\Mangan;
15
16
use Maslosoft\Addendum\Interfaces\AnnotatedInterface;
17
use Maslosoft\Mangan\Abstracts\AbstractFinder;
18
use Maslosoft\Mangan\Adapters\Finder\MongoAdapter;
19
use Maslosoft\Mangan\Exceptions\ManganException;
20
use Maslosoft\Mangan\Helpers\FinderEvents;
21
use Maslosoft\Mangan\Interfaces\EntityManagerInterface;
22
use Maslosoft\Mangan\Interfaces\FinderInterface;
23
use Maslosoft\Mangan\Meta\ManganMeta;
24
use Maslosoft\Mangan\Traits\Finder\CreateModel;
25
use Maslosoft\Mangan\Traits\Finder\FinderHelpers;
26
use Maslosoft\Mangan\Transformers\RawArray;
27
28
/**
29
 * Basic Finder implementation
30
 *
31
 * @author Piotr Maselkowski <pmaselkowski at gmail.com>
32
 */
33
class Finder extends AbstractFinder
34
{
35
36
	use CreateModel,
37
		FinderHelpers;
38
39
	/**
40
	 * Constructor
41
	 *
42
	 * @param object                 $model Model instance
43
	 * @param EntityManagerInterface $em
44
	 * @param Mangan                 $mangan
45
	 */
46 99
	public function __construct($model, $em = null, $mangan = null)
47
	{
48 99
		if (null === $mangan)
49
		{
50 98
			$mangan = Mangan::fromModel($model);
51
		}
52 99
		$this->setModel($model);
53 99
		$this->setScopeManager(new ScopeManager($model));
54 99
		$this->setAdapter(new MongoAdapter($model, $mangan, $em));
55
56 99
		$this->setProfiler($mangan->getProfiler());
57 99
		$this->setFinderEvents(new FinderEvents);
58 99
		$this->withCursor($mangan->useCursor);
59 99
	}
60
61
	/**
62
	 * Create model related finder.
63
	 * This will create customized finder if defined in model with Finder annotation.
64
	 * If no custom finder is defined this will return default Finder.
65
	 *
66
	 * @param AnnotatedInterface     $model
67
	 * @param EntityManagerInterface $em
68
	 * @param Mangan                 $mangan
69
	 * @return FinderInterface
70
	 */
71 13
	public static function create(AnnotatedInterface $model, $em = null, Mangan $mangan = null)
72
	{
73 13
		$finderClass = ManganMeta::create($model)->type()->finder ?: static::class;
74 13
		return new $finderClass($model, $em, $mangan);
75
	}
76
77
}
78