EmbeddedArrayDecorator   A
last analyzed

Complexity

Total Complexity 22

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 84.09%

Importance

Changes 0
Metric Value
wmc 22
lcom 0
cbo 3
dl 0
loc 104
ccs 37
cts 44
cp 0.8409
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A read() 0 24 3
A write() 0 17 3
C _getInstance() 0 49 16
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\Decorators;
15
16
use Maslosoft\Addendum\Interfaces\AnnotatedInterface;
17
use Maslosoft\Mangan\Helpers\DbRefManager;
18
use Maslosoft\Mangan\Helpers\UnknownDocumentTypePanicker;
19
use Maslosoft\Mangan\Interfaces\Decorators\Property\DecoratorInterface;
20
use Maslosoft\Mangan\Interfaces\Transformators\TransformatorInterface;
21
22
/**
23
 * EmbeddedArrayDecorator
24
 *
25
 * @author Piotr Maselkowski <pmaselkowski at gmail.com>
26
 */
27
class EmbeddedArrayDecorator extends EmbeddedDecorator implements DecoratorInterface
28
{
29
30 9
	public function read($model, $name, &$dbValue, $transformatorClass = TransformatorInterface::class)
31
	{
32 9
		if (is_array($dbValue))
33
		{
34 9
			$docs = [];
35 9
			foreach ($dbValue as $key => $data)
36
			{
37 7
				static::ensureClass($model, $name, $data);
38
				// Set ensured class to $dbValue
39 7
				$instance = $this->_getInstance($model->$name, $dbValue, $data, $model, $name);
40 7
				$embedded = $transformatorClass::toModel($data, $instance, $instance, $model, $name);
41
42
				// Field was transformed from DB Ref
43 7
				$embedded = DbRefManager::maybeCreateInstanceFrom($embedded);
44
45 7
				$docs[] = $embedded;
46
			}
47 9
			$model->$name = $docs;
48
		}
49
		else
50
		{
51
			$model->$name = $dbValue;
52
		}
53 9
	}
54
55 9
	public function write($model, $name, &$dbValue, $transformatorClass = TransformatorInterface::class)
56
	{
57 9
		if (is_array($model->$name))
58
		{
59 9
			$dbValue[$name] = [];
60 9
			$key = 0;
61 9
			foreach ($model->$name as $key => $document)
62
			{
63 7
				$data = $transformatorClass::fromModel($document);
64 7
				$dbValue[$name][] = $data;
65
			}
66
		}
67
		else
68
		{
69
			$dbValue[$name] = $model->$name;
70
		}
71 9
	}
72
73
	/**
74
	 * TODO: This relies on _id
75
	 * @param AnnotatedInterface[] $instances
76
	 * @param mixed[] $dbValue
77
	 * @param mixed[] $data
78
	 * @return AnnotatedInterface|null
79
	 */
80 7
	private function _getInstance($instances, $dbValue, $data, $parent = null, $parentField = '')
81
	{
82 7
		if (!count($instances))
83
		{
84 6
			return null;
85
		}
86 3
		$map = [];
87
		/**
88
		 * TODO Workaround for manganel not having _id
89
		 */
90 3
		if (!isset($data['_id']) && isset($data['id']))
91
		{
92
			$data['_id'] = $data['id'];
93
		}
94 3
		if(!isset($data['_id']) && isset($data['_mongo_id_']))
95
		{
96
			$data['_id'] = $data['_mongo_id_'];
97
		}
98 3
		foreach ($dbValue as $val)
99
		{
100
			/**
101
			 * TODO Workaround for manganel not having _id
102
			 */
103 3
			if (!isset($val['_id']) && isset($val['id']))
104
			{
105
				$val['_id'] = $val['id'];
106
			}
107 3
			if(!isset($val['_id']) && isset($val['_mongo_id_']))
108
			{
109
				$val['_id'] = $val['_mongo_id_'];
110
			}
111 3
			if(empty($val['_class']))
112
			{
113
				UnknownDocumentTypePanicker::tryHandle($val, $parent, $parentField);
114
			}
115 3
			assert(array_key_exists('_id', $val), sprintf('_id not defined for %s', $val['_class']));
116 3
			$id = (string) $val['_id'];
117 3
			$map[$id] = true;
118
		}
119 3
		foreach ($instances as $instance)
120
		{
121 3
			$id = (string) $instance->_id;
122 3
			if (isset($map[$id]) && $data['_id'] == $id && $instance instanceof $data['_class'])
123
			{
124 2
				return $instance;
125
			}
126
		}
127 2
		return null;
128
	}
129
130
}
131