Completed
Push — master ( 507c4a...51e00b )
by Peter
07:40
created

Finder   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 10

Test Coverage

Coverage 95%

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 10
dl 0
loc 65
ccs 19
cts 20
cp 0.95
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 14 2
A create() 0 5 2
A createModel() 0 20 3
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\FinderHelpers;
25
use Maslosoft\Mangan\Transformers\RawArray;
26
27
/**
28
 * Basic Finder implementation
29
 *
30
 * @author Piotr Maselkowski <pmaselkowski at gmail.com>
31
 */
32
class Finder extends AbstractFinder
33
{
34
35
	use FinderHelpers;
36
37
	/**
38
	 * Constructor
39
	 *
40
	 * @param object $model Model instance
41
	 * @param EntityManagerInterface $em
42
	 * @param Mangan $mangan
43
	 */
44 95
	public function __construct($model, $em = null, $mangan = null)
45
	{
46 95
		if (null === $mangan)
47
		{
48 94
			$mangan = Mangan::fromModel($model);
49
		}
50 95
		$this->setModel($model);
51 95
		$this->setScopeManager(new ScopeManager($model));
52 95
		$this->setAdapter(new MongoAdapter($model, $mangan, $em));
53
54 95
		$this->setProfiler($mangan->getProfiler());
55 95
		$this->setFinderEvents(new FinderEvents);
56 95
		$this->withCursor($mangan->useCursor);
57 95
	}
58
59
	/**
60
	 * Create model related finder.
61
	 * This will create customized finder if defined in model with Finder annotation.
62
	 * If no custom finder is defined this will return default Finder.
63
	 *
64
	 * @param AnnotatedInterface $model
65
	 * @param EntityManagerInterface $em
66
	 * @param Mangan $mangan
67
	 * @return FinderInterface
68
	 */
69 12
	public static function create(AnnotatedInterface $model, $em = null, Mangan $mangan = null)
70
	{
71 12
		$finderClass = ManganMeta::create($model)->type()->finder ?: static::class;
72 12
		return new $finderClass($model, $em, $mangan);
73
	}
74
75 79
	protected function createModel($data)
76
	{
77 79
		if (!empty($data['$err']))
78
		{
79
			throw new ManganException(sprintf("There is an error in query: %s", $data['$err']));
80
		}
81
82
		// By default create instances of same
83
		// type as provided model
84 79
		$model = $this->getModel();
85
86
		// For non homogeneous collections class
87
		// need to be taken from data, not defined
88
		// by model
89 79
		if(ManganMeta::create($model)->type()->homogenous === false)
90
		{
91 3
			$model = null;
92
		}
93 79
		return RawArray::toModel($data, $model);
94
	}
95
96
}
97