Completed
Push — master ( c8f19a...ddbfd2 )
by Peter
72:09 queued 69:12
created

Factory::create()   C

Complexity

Conditions 9
Paths 26

Size

Total Lines 63
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 41
CRAP Score 9.001

Importance

Changes 0
Metric Value
dl 0
loc 63
ccs 41
cts 42
cp 0.9762
rs 6.6149
c 0
b 0
f 0
cc 9
eloc 34
nc 26
nop 3
crap 9.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 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 87
	public static function create(DocumentPropertyMeta $meta, DocumentTypeMeta $modelMeta, $transformatorClass)
38
	{
39 87
		$key = $modelMeta->name . $modelMeta->connectionId . $meta->name . $transformatorClass;
40
41 87
		if(isset(self::$c[$key]))
42 87
		{
43
			return self::$c[$key];
44
		}
45
46 87
		$sanitizerClass = self::_resolve($meta, $modelMeta);
47 87
		if(false === $sanitizerClass)
48 87
		{
49 38
			self::$c[$key] = false;
50 38
			return false;
51
		}
52
53
54
		// Remap sanitizer if needed
55 86
		$mapConfig = [];
56 86
		$map = Mangan::fly($modelMeta->connectionId)->sanitizersMap;
57 86
		if (isset($map[$transformatorClass]) && isset($map[$transformatorClass][$sanitizerClass]))
58 86
		{
59 12
			$mapConfig = $map[$transformatorClass][$sanitizerClass];
60 12
			if (is_string($mapConfig))
61 12
			{
62 12
				$mapClass = $mapConfig;
63
				$mapConfig = [
64
					'class' => $mapClass
65 12
				];
66 12
			}
67 12
		}
68 86
		if (is_array($meta->sanitizer))
69 86
		{
70 68
			$sanitizerConfig = $meta->sanitizer;
71 68
			$sanitizerConfig['class'] = $sanitizerClass;
72 68
		}
73
		else
74
		{
75 74
			$sanitizerConfig = [];
76 74
			$sanitizerConfig['class'] = $sanitizerClass;
77
		}
78 86
		if (!empty($mapConfig))
79 86
		{
80 12
			$sanitizerConfig = array_merge($sanitizerConfig, $mapConfig);
81 12
		}
82
83
		// Sanitize as array
84 86
		if ($meta->sanitizeArray)
85 86
		{
86
			$sanitizerConfig = [
87 2
				'class' => ArraySanitizer::class,
88
				'sanitizer' => $sanitizerConfig
89 2
			];
90 2
		}
91
		$config = [
92
			$transformatorClass => [
93
				$sanitizerConfig
94 86
			]
95 86
		];
96 86
		$sanitizer = PluginFactory::fly($modelMeta->connectionId)->instance($config, $transformatorClass)[0];
97 86
		self::$c[$key] = $sanitizer;
98 86
		return $sanitizer;
99
	}
100
101 87
	private static function _resolve(DocumentPropertyMeta $meta, DocumentTypeMeta $modelMeta)
102
	{
103
		// Sanitizer is explicitly set
104 87
		if (!empty($meta->sanitizer))
105 87
		{
106 68
			if (is_array($meta->sanitizer))
107 68
			{
108 68
				$name = $meta->sanitizer['class'];
109 68
			}
110
			else
111
			{
112
				$name = $meta->sanitizer;
113
			}
114
			// If short name is used add default namespace
115 68
			if (strstr($name, '\\') === false)
116 68
			{
117 12
				$className = sprintf('%s\%s', PassThrough::Ns, $name);
118 12
			}
119
			else
120
			{
121 60
				$className = $name;
122
			}
123 68
			if (!ClassChecker::exists($className))
124 68
			{
125
				throw new ManganException(sprintf("Sanitizer class `%s` not found for field `%s` in model `%s`", $className, $meta->name, $modelMeta->name));
126
			}
127 68
			return $className;
128
		}
129
130
		// Guess sanitizer
131 78
		switch (gettype($meta->default))
132
		{
133 83
			case 'boolean':
134 33
				return BooleanSanitizer::class;
135 77
			case 'double':
136 8
				return DoubleSanitizer::class;
137 82
			case 'integer':
138 27
				return IntegerSanitizer::class;
139 87
			case 'string':
140 71
				return StringSanitizer::class;
141 38
		}
142
143
		// None
144 38
		return false;
145
	}
146
147
}
148