Completed
Push — master ( c7a6bb...41fefb )
by Peter
20:43
created

Transformer::toModel()   B

Complexity

Conditions 8
Paths 42

Size

Total Lines 54
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 37
CRAP Score 8.001

Importance

Changes 4
Bugs 1 Features 0
Metric Value
c 4
b 1
f 0
dl 0
loc 54
ccs 37
cts 38
cp 0.9737
rs 7.4119
cc 8
eloc 30
nc 42
nop 3
crap 8.001

How to fix   Long Method   

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 http://maslosoft.com/mangan/
12
 */
13
14
namespace Maslosoft\Mangan\Transformers;
15
16
use Maslosoft\Addendum\Interfaces\AnnotatedInterface;
17
use Maslosoft\Mangan\Exceptions\TransformatorException;
18
use Maslosoft\Mangan\Helpers\Decorator\Decorator;
19
use Maslosoft\Mangan\Helpers\Decorator\ModelDecorator;
20
use Maslosoft\Mangan\Helpers\Finalizer\FinalizingManager;
21
use Maslosoft\Mangan\Helpers\PropertyFilter\Filter;
22
use Maslosoft\Mangan\Helpers\Sanitizer\Sanitizer;
23
use Maslosoft\Mangan\Meta\DocumentPropertyMeta;
24
use Maslosoft\Mangan\Meta\ManganMeta;
25
26
/**
27
 * Transformer
28
 *
29
 * @author Piotr Maselkowski <pmaselkowski at gmail.com>
30
 */
31
abstract class Transformer
32
{
33
34
	/**
35
	 * Returns the given object as an associative array
36
	 * @param AnnotatedInterface|object $model
37
	 * @param string[] $fields Fields to transform
38
	 * @return array an associative array of the contents of this object
39
	 */
40 101
	public static function fromModel(AnnotatedInterface $model, $fields = [])
41
	{
42 101
		$meta = ManganMeta::create($model);
43 101
		$calledClass = get_called_class();
44 101
		$decorator = new Decorator($model, $calledClass);
45 101
		$md = new ModelDecorator($model, $calledClass);
46 101
		$sanitizer = new Sanitizer($model, $calledClass);
47 101
		$filter = new Filter($model, $calledClass);
48 101
		$arr = [];
49 101
		foreach ($meta->fields() as $name => $fieldMeta)
50
		{
51 101
			if (!empty($fields) && !in_array($name, $fields))
52 101
			{
53 23
				continue;
54
			}
55 101
			if (!$filter->fromModel($model, $fieldMeta))
56 101
			{
57 32
				continue;
58
			}
59 101
			$model->$name = $sanitizer->write($name, $model->$name);
60 101
			$decorator->write($name, $arr);
61 101
		}
62 101
		$md->write($arr);
63 101
		return FinalizingManager::fromModel($arr, static::class, $model);
64
	}
65
66
	/**
67
	 * Create document from array
68
	 *
69
	 * @param mixed[] $data
70
	 * @param string|object $className
71
	 * @param AnnotatedInterface $instance
72
	 * @return AnnotatedInterface
73
	 * @throws TransformatorException
74
	 */
75 75
	public static function toModel($data, $className = null, AnnotatedInterface $instance = null)
76
	{
77 75
		$data = (array) $data;
78 75
		if (is_object($className))
79 75
		{
80 59
			$className = get_class($className);
81 59
		}
82 75
		if (!$className)
83 75
		{
84 35
			if (array_key_exists('_class', $data))
85 35
			{
86 35
				$className = $data['_class'];
87 35
			}
88
			else
89
			{
90
				throw new TransformatorException('Could not determine document type');
91
			}
92 35
		}
93
		if ($instance)
94 75
		{
95 3
			$model = $instance;
96 3
		}
97
		else
98
		{
99 72
			$model = new $className;
100
		}
101 75
		$meta = ManganMeta::create($model);
102 75
		$calledClass = get_called_class();
103 75
		$decorator = new Decorator($model, $calledClass);
104 75
		$md = new ModelDecorator($model, $calledClass);
105 75
		$sanitizer = new Sanitizer($model, $calledClass);
106 75
		$filter = new Filter($model, $calledClass);
107 75
		foreach ($meta->fields() as $name => $fieldMeta)
108
		{
109
			/* @var $fieldMeta DocumentPropertyMeta */
110 75
			if (isset($data[$name]))
111 75
			{
112 75
				$value = $data[$name];
113 75
			}
114
			else
115
			{
116 35
				$value = $fieldMeta->default;
117
			}
118 75
			if (!$filter->toModel($model, $fieldMeta))
119 75
			{
120 17
				continue;
121
			}
122 75
			$decorator->read($name, $value);
123 75
			$model->$name = $sanitizer->read($name, $model->$name);
124 75
		}
125 75
		$md->read($data);
126
127 75
		return FinalizingManager::toModel(static::class, $model);
128
	}
129
130
}
131