Completed
Push — master ( 4182a1...6a0ca3 )
by Peter
05:51
created

Factory::create()   C

Complexity

Conditions 9
Paths 26

Size

Total Lines 63
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 28
CRAP Score 9.0033

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 63
ccs 28
cts 29
cp 0.9655
rs 6.6149
cc 9
eloc 34
nc 26
nop 3
crap 9.0033

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 https://maslosoft.com/mangan/
12
 */
13
14
namespace Maslosoft\Mangan\Helpers\Sanitizer;
15
16
use Maslosoft\Addendum\Utilities\ClassChecker;
17
use Maslosoft\Gazebo\PluginFactory;
18
use Maslosoft\Mangan\Exceptions\ManganException;
19
use Maslosoft\Mangan\Mangan;
20
use Maslosoft\Mangan\Meta\DocumentPropertyMeta;
21
use Maslosoft\Mangan\Meta\DocumentTypeMeta;
22
use Maslosoft\Mangan\Sanitizers\ArraySanitizer;
23
use Maslosoft\Mangan\Sanitizers\BooleanSanitizer;
24
use Maslosoft\Mangan\Sanitizers\DoubleSanitizer;
25
use Maslosoft\Mangan\Sanitizers\IntegerSanitizer;
26
use Maslosoft\Mangan\Sanitizers\PassThrough;
27
use Maslosoft\Mangan\Sanitizers\StringSanitizer;
28
29
/**
30
 * Factory
31
 *
32
 * @author Piotr Maselkowski <pmaselkowski at gmail.com>
33
 */
34
class Factory
35
{
36
	private static $c = [];
37 82
	public static function create(DocumentPropertyMeta $meta, DocumentTypeMeta $modelMeta, $transformatorClass)
38
	{
39 82
		$key = $modelMeta->name . $modelMeta->connectionId . $meta->name . $transformatorClass;
40
41 82
		if(isset(self::$c[$key]))
42
		{
43
			return self::$c[$key];
44
		}
45
46 82
		$sanitizerClass = self::_resolve($meta, $modelMeta);
47 82
		if(false === $sanitizerClass)
48
		{
49 37
			self::$c[$key] = false;
50 37
			return false;
51
		}
52
53
54
		// Remap sanitizer if needed
55 82
		$mapConfig = [];
56 82
		$map = Mangan::fly($modelMeta->connectionId)->sanitizersMap;
57 82
		if (isset($map[$transformatorClass]) && isset($map[$transformatorClass][$sanitizerClass]))
58
		{
59 12
			$mapConfig = $map[$transformatorClass][$sanitizerClass];
60 12
			if (is_string($mapConfig))
61
			{
62 12
				$mapClass = $mapConfig;
63
				$mapConfig = [
64 12
					'class' => $mapClass
65
				];
66
			}
67
		}
68 82
		if (is_array($meta->sanitizer))
69
		{
70 65
			$sanitizerConfig = $meta->sanitizer;
71 65
			$sanitizerConfig['class'] = $sanitizerClass;
72
		}
73
		else
74
		{
75 70
			$sanitizerConfig = [];
76 70
			$sanitizerConfig['class'] = $sanitizerClass;
77
		}
78 82
		if (!empty($mapConfig))
79
		{
80 12
			$sanitizerConfig = array_merge($sanitizerConfig, $mapConfig);
81
		}
82
83
		// Sanitize as array
84 82
		if ($meta->sanitizeArray)
85
		{
86
			$sanitizerConfig = [
87 2
				'class' => ArraySanitizer::class,
88 2
				'sanitizer' => $sanitizerConfig
89
			];
90
		}
91
		$config = [
92
			$transformatorClass => [
93 82
				$sanitizerConfig
94
			]
95
		];
96 82
		$sanitizer = PluginFactory::fly($modelMeta->connectionId)->instance($config, $transformatorClass)[0];
97 82
		self::$c[$key] = $sanitizer;
98 82
		return $sanitizer;
99
	}
100
101 82
	private static function _resolve(DocumentPropertyMeta $meta, DocumentTypeMeta $modelMeta)
102
	{
103
		// Sanitizer is explicitly set
104 82
		if (!empty($meta->sanitizer))
105
		{
106 65
			if (is_array($meta->sanitizer))
107
			{
108 65
				$name = $meta->sanitizer['class'];
109
			}
110
			else
111
			{
112
				$name = $meta->sanitizer;
113
			}
114
			// If short name is used add default namespace
115 65
			if (strstr($name, '\\') === false)
116
			{
117 12
				$className = sprintf('%s\%s', PassThrough::Ns, $name);
118
			}
119
			else
120
			{
121 57
				$className = $name;
122
			}
123 65
			if (!ClassChecker::exists($className))
124
			{
125
				throw new ManganException(sprintf("Sanitizer class `%s` not found for field `%s` in model `%s`", $className, $meta->name, $modelMeta->name));
126
			}
127 65
			return $className;
128
		}
129
130
		// Guess sanitizer
131 73
		switch (gettype($meta->default))
132
		{
133 73
			case 'boolean':
134 32
				return BooleanSanitizer::class;
135 72
			case 'double':
136 8
				return DoubleSanitizer::class;
137 72
			case 'integer':
138 27
				return IntegerSanitizer::class;
139 70
			case 'string':
140 67
				return StringSanitizer::class;
141
		}
142
143
		// None
144 37
		return false;
145
	}
146
147
}
148