Passed
Push — master ( c698e9...11e57f )
by Richard
04:33 queued 12s
created

FieldFactory::arrayField()   D

Complexity

Conditions 18
Paths 18

Size

Total Lines 81
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 18
eloc 36
c 2
b 0
f 0
nc 18
nop 3
dl 0
loc 81
rs 4.8666

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
			return $this->arrayField($block, $field, $key);
39
		}
40
41
		// legacy image fields
42
		if ($this->isLegacyImageField($field)) {
43
			return new Image($field, $block);
44
		}
45
46
		// strings or anything else - do nothing
47
		return $field;
48
	}
49
50
	public function arrayField($block, $field, $key) {
51
		// match link fields
52
		if (array_key_exists('linktype', $field)) {
53
			$class = 'Riclep\Storyblok\Fields\\' . Str::studly($field['linktype']) . 'Link';
54
55
			return new $class($field, $block);
56
		}
57
58
		// match rich-text fields
59
		if (array_key_exists('type', $field) && $field['type'] === 'doc') {
60
			return new RichText($field, $block);
61
		}
62
63
		// match asset fields - detecting raster images
64
		if (array_key_exists('fieldtype', $field) && $field['fieldtype'] === 'asset') {
65
66
			// legacy image fields
67
			if($this->isLegacyImageField($field['filename'])) {
68
				return new Image($field, $block);
69
			}
70
71
			return new Asset($field, $block);
72
		}
73
74
		// match table fields
75
		if (array_key_exists('fieldtype', $field) && $field['fieldtype'] === 'table') {
76
			return new Table($field, $block);
77
		}
78
79
		if (array_key_exists(0, $field)) {
80
			// it’s an array of relations - request them if we’re auto or manual resolving
81
			if (Str::isUuid($field[0])) {
82
				if ($block->_autoResolveRelations || in_array($key, $block->_resolveRelations)) {
83
					$relations = collect($field)->transform(function ($relation) use ($block) {
84
						return $block->getRelation(new RequestStory(), $relation);
85
					});
86
87
					if ($block->_filterRelations) {
88
						$relations = $relations->filter();
89
					}
90
91
					return $relations;
92
				}
93
			}
94
95
			// has child items - single option, multi option and Blocks fields
96
			if (is_array($field[0])) {
97
				// resolved relationships - entire story is returned, we just want the content and a few meta items
98
				if (array_key_exists('content', $field[0])) {
99
					return collect($field)->transform(function ($relation) use ($block) {
100
						$class = $block->getChildClassName('Block', $relation['content']['component']);
101
						$relationClass = new $class($relation['content'], $block);
102
103
						$relationClass->addMeta([
104
							'name' => $relation['name'],
105
							'published_at' => $relation['published_at'],
106
							'full_slug' => $relation['full_slug'],
107
						]);
108
109
						return $relationClass;
110
					});
111
				}
112
113
				// this field holds blocks!
114
				if (array_key_exists('component', $field[0])) {
115
					return collect($field)->transform(function ($childBlock) use ($block) {
116
						$class = $block->getChildClassName('Block', $childBlock['component']);
117
118
						return new $class($childBlock, $block);
119
					});
120
				}
121
122
				// multi assets
123
				if (array_key_exists('filename', $field[0])) {
124
					return new MultiAsset($field, $block);
125
				}
126
			}
127
		}
128
129
		// just return the array
130
		return $field;
131
	}
132
133
134
135
	/**
136
	 * Check if given string is an legacy image field
137
	 *
138
	 * @param  $filename
139
	 * @return boolean
140
	 */
141
	public function isLegacyImageField($filename){
142
		$allowed_extentions = ['.jpg', '.jpeg', '.png', '.gif', '.webp'];
143
144
		return is_string($filename) && Str::of( $filename )->lower()->endsWith( $allowed_extentions );
145
	}
146
}