Completed
Push — master ( 0b407e...427d35 )
by Peter
07:12
created

RelatedDecorator::write()   C

Complexity

Conditions 10
Paths 35

Size

Total Lines 62
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 30
CRAP Score 10.0033

Importance

Changes 0
Metric Value
dl 0
loc 62
ccs 30
cts 31
cp 0.9677
rs 6.4192
c 0
b 0
f 0
cc 10
eloc 33
nc 35
nop 4
crap 10.0033

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 InvalidArgumentException;
17
use Maslosoft\Mangan\Criteria;
18
use Maslosoft\Mangan\EntityManager;
19
use Maslosoft\Mangan\Finder;
20
use Maslosoft\Mangan\Helpers\PkManager;
21
use Maslosoft\Mangan\Interfaces\Decorators\Property\DecoratorInterface;
22
use Maslosoft\Mangan\Interfaces\Transformators\TransformatorInterface;
23
use Maslosoft\Mangan\Meta\ManganMeta;
24
use Maslosoft\Mangan\Sort;
25
26
/**
27
 * RelatedRecorator
28
 *
29
 * @author Piotr Maselkowski <pmaselkowski at gmail.com>
30
 */
31
class RelatedDecorator implements DecoratorInterface
32
{
33
34 5
	public function read($model, $name, &$dbValue, $transformatorClass = TransformatorInterface::class)
35
	{
36 5
		$fieldMeta = ManganMeta::create($model)->field($name);
37 5
		$relMeta = $fieldMeta->related;
38 5
		$relModel = new $relMeta->class;
39 5
		$criteria = new Criteria(null, $relModel);
40 5
		if (empty($relMeta->join) && empty($relMeta->condition))
41
		{
42
			throw new InvalidArgumentException(sprintf('Parameter `join` or `condition` is required for Related annotation, model `%s`, field `%s`', get_class($model), $name));
43
		}
44 5
		if (!empty($relMeta->join))
45
		{
46 3
			foreach ($relMeta->join as $source => $rel)
47
			{
48 3
				assert($model->$source !== null);
49 3
				$criteria->addCond($rel, '==', $model->$source);
50
			}
51
		}
52 5
		if (!empty($relMeta->condition))
53
		{
54 2
			foreach ($relMeta->condition as $field => $value)
55
			{
56 2
				$criteria->addCond($field, '==', $value);
57
			}
58
		}
59 5
		$criteria->setSort(new Sort($relMeta->sort));
60 5
		if ($relMeta->single)
61
		{
62 3
			$model->$name = (new Finder($relModel))->find($criteria);
63
		}
64
		else
65
		{
66 2
			$model->$name = (new Finder($relModel))->findAll($criteria);
67
		}
68 5
	}
69
70 5
	public function write($model, $name, &$dbValues, $transformatorClass = TransformatorInterface::class)
71
	{
72 5
		if (!empty($model->$name))
73
		{
74
			// Store empty field to trigger decorator read
75 4
			$dbValues[$name] = null;
76
77 4
			$fieldMeta = ManganMeta::create($model)->field($name);
78 4
			$relMeta = $fieldMeta->related;
79 4
			if ($relMeta->single)
80
			{
81
				$models = [
82 2
					$model->$name
83
				];
84
			}
85
			else
86
			{
87 2
				$models = $model->$name;
88
			}
89 4
			$order = 0;
90 4
			foreach ($models as $relModel)
91
			{
92 4
				$fields = [];
93 4
				if (!empty($relMeta->join))
94
				{
95 3
					foreach ($relMeta->join as $source => $rel)
96
					{
97 3
						$fields[] = $rel;
98 3
						assert(isset($model->$source));
99 3
						$relModel->$rel = $model->$source;
100
					}
101
				}
102 4
				if (!empty($relMeta->condition))
103
				{
104 1
					foreach ($relMeta->condition as $field => $value)
105
					{
106 1
						$fields[] = $field;
107 1
						$relModel->$field = $value;
108
					}
109
				}
110 4
				if (!empty($relMeta->orderField))
111
				{
112 1
					$fields[] = $relMeta->orderField;
113 1
					$fields = array_unique($fields);
114 1
					$relModel->order = $order;
115 1
					$order++;
116
				}
117 4
				$em = new EntityManager($relModel);
118 4
				if ($relMeta->updatable)
119
				{
120
					// Update whole model
121 4
					$em->upsert();
122
				}
123
				else
124
				{
125
					// Update only relation info
126
					$criteria = PkManager::prepareFromModel($relModel);
127 4
					$em->updateOne($criteria, $fields);
128
				}
129
			}
130
		}
131 5
	}
132
133
}
134