Passed
Push — master ( 1f93d4...067214 )
by Richard
03:37 queued 17s
created

FieldFactory::arrayField()   C

Complexity

Conditions 17
Paths 17

Size

Total Lines 75
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 17
eloc 33
c 1
b 0
f 0
nc 17
nop 3
dl 0
loc 75
rs 5.2166

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
namespace Riclep\Storyblok;
4
5
use Illuminate\Support\Str;
6
use Riclep\Storyblok\Fields\Asset;
7
use Riclep\Storyblok\Fields\Image;
8
use Riclep\Storyblok\Fields\MultiAsset;
9
use Riclep\Storyblok\Fields\RichText;
10
use Riclep\Storyblok\Fields\Table;
11
12
class FieldFactory
13
{
14
	public function build($block, $field, $key) {
15
		// does the Block assign any $_casts? This is key (field) => value (class)
16
		if (array_key_exists($key, $block->getCasts())) {
17
			$casts = $block->getCasts();
18
			return new $casts[$key]($field, $block);
19
		}
20
21
		// find Fields specific to this Block matching: BlockNameFieldName
22
		if ($class = $block->getChildClassName('Field', $block->component() . '_' . $key)) {
23
			return new $class($field, $block);
24
		}
25
26
		// auto-match Field classes
27
		if ($class = $block->getChildClassName('Field', $key)) {
28
			return new $class($field, $block);
29
		}
30
31
		// single item relations
32
		if (Str::isUuid($field) && ($block->_autoResolveRelations || in_array($key, $block->_resolveRelations))) {
33
			return $block->getRelation(new RequestStory(), $field);
34
		}
35
36
		// complex fields
37
		if (is_array($field) && !empty($field)) {
38
39
40
			return $this->arrayField($block, $field, $key);
41
42
			//return $block->arrayFieldTypes($field, $key);
43
		}
44
45
		// legacy image fields
46
		if ($this->isLegacyImageField($field)) {
47
			return new Image($field, $block);
48
		}
49
50
		// strings or anything else - do nothing
51
		return $field;
52
	}
53
54
	public function arrayField($block, $field, $key) {
55
		// match link fields
56
		if (array_key_exists('linktype', $field)) {
57
			$class = 'Riclep\Storyblok\Fields\\' . Str::studly($field['linktype']) . 'Link';
58
59
			return new $class($field, $block);
60
		}
61
62
		// match rich-text fields
63
		if (array_key_exists('type', $field) && $field['type'] === 'doc') {
64
			return new RichText($field, $block);
65
		}
66
67
		// match asset fields - detecting raster images
68
		if (array_key_exists('fieldtype', $field) && $field['fieldtype'] === 'asset') {
69
70
			// legacy image fields
71
			if($this->isLegacyImageField($field['filename'])) {
72
				return new Image($field, $block);
73
			}
74
75
			return new Asset($field, $block);
76
		}
77
78
		// match table fields
79
		if (array_key_exists('fieldtype', $field) && $field['fieldtype'] === 'table') {
80
			return new Table($field, $block);
81
		}
82
83
		if (array_key_exists(0, $field)) {
84
			// it’s an array of relations - request them if we’re auto or manual resolving
85
			if (Str::isUuid($field[0])) {
86
				if ($block->_autoResolveRelations || in_array($key, $block->_resolveRelations)) {
87
					return collect($field)->transform(function ($relation) use ($block) {
88
						return $block->getRelation(new RequestStory(), $relation);
89
					});
90
				}
91
			}
92
93
			// has child items - single option, multi option and Blocks fields
94
			if (is_array($field[0])) {
95
				// resolved relationships - entire story is returned, we just want the content and a few meta items
96
				if (array_key_exists('content', $field[0])) {
97
					return collect($field)->transform(function ($relation) use ($block) {
98
						$class = $block->getChildClassName('Block', $relation['content']['component']);
99
						$relationClass = new $class($relation['content'], $block);
100
101
						$relationClass->addMeta([
102
							'name' => $relation['name'],
103
							'published_at' => $relation['published_at'],
104
							'full_slug' => $relation['full_slug'],
105
						]);
106
107
						return $relationClass;
108
					});
109
				}
110
111
				// this field holds blocks!
112
				if (array_key_exists('component', $field[0])) {
113
					return collect($field)->transform(function ($childBlock) use ($block) {
114
						$class = $block->getChildClassName('Block', $childBlock['component']);
115
116
						return new $class($childBlock, $block);
117
					});
118
				}
119
120
				// multi assets
121
				if (array_key_exists('filename', $field[0])) {
122
					return new MultiAsset($field, $block);
123
				}
124
			}
125
		}
126
127
		// just return the array
128
		return $field;
129
	}
130
131
132
133
	/**
134
	 * Check if given string is an legacy image field
135
	 *
136
	 * @param  $filename
137
	 * @return boolean
138
	 */
139
	public function isLegacyImageField($filename){
140
		$allowed_extentions = ['.jpg', '.jpeg', '.png', '.gif', '.webp'];
141
142
		return is_string($filename) && Str::of( $filename )->lower()->endsWith( $allowed_extentions );
143
	}
144
}