Passed
Push — master ( 806331...6aab10 )
by Richard
04:40 queued 18s
created

ProcessesBlocks::processStoryblokKeys()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 4
rs 10
1
<?php
2
3
4
namespace Riclep\Storyblok\Traits;
5
6
7
use App\Storyblok\DefaultBlock;
0 ignored issues
show
Bug introduced by
The type App\Storyblok\DefaultBlock was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use Illuminate\Support\Facades\Config;
9
use Illuminate\Support\Str;
10
11
trait ProcessesBlocks
12
{
13
	private function processBlock($block, $key) {
14
		if (is_array($block) && !array_key_exists('component', $block)) {
15
			$block['component'] = $this->getComponentType($block, $key);
16
		}
17
18
		$blockType = $this->getBlockType($block, $key);
19
20
		if ($blockType) {
21
			return $blockType;
22
		}
23
24
		return $block ?: false;
25
	}
26
27
28
	private function getBlockType($block, $key) {
29
		if (is_int($key) && $this->isUuid($block)) {
30
			$child = $this->childStory($block);
0 ignored issues
show
Bug introduced by
It seems like childStory() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

30
			/** @scrutinizer ignore-call */ 
31
   $child = $this->childStory($block);
Loading history...
31
			$blockClass = $this->getBlockClass($child['content']['component']);
32
33
			return new $blockClass($child, $key);
34
		}
35
36
		if (!in_array($key, ['id', 'uuid', 'group_id']) && $this->isUuid($block)) {
37
			$child = $this->childStory($block);
38
			$blockClass = $this->getBlockClass($child['content']['component']);
39
40
			return new $blockClass($child, $key);
41
		}
42
43
		if (is_array($block)) {
44
			return $this->arrayBlock($block, $key);
45
		}
46
47
		return false;
48
	}
49
50
	/**
51
	 * Works out the component type - this can come from either the component specified in
52
	 * the response from Storyblok or, in the case of plugins, the plugin name is used
53
	 */
54
	private function getComponentType($block, $key) {
55
		if (array_key_exists('plugin', $block)) {
56
			return $block['plugin'];
57
		}
58
59
		if (array_key_exists('component', $block) && !is_null($block['component'])) {
60
			return $block['component'];
61
		}
62
63
		return $key;
64
	}
65
66
	private function arrayBlock($block, $key) {
67
		$blockClass = $this->getBlockClass($block['component']);
68
69
		// or return the default block
70
		return new $blockClass($block, $key);
71
	}
72
73
	private function getBlockClass($component) {
74
75
		//dump($this, $component, '------------------------');
76
77
		if (class_exists(config('storyblok.component_class_namespace') . Str::studly($component) . 'Block')) {
78
			return config('storyblok.component_class_namespace') . Str::studly($component) . 'Block';
79
		}
80
81
		return config('storyblok.component_class_namespace') . 'DefaultBlock';
82
	}
83
84
	/**
85
	 * Check if a given string is a valid UUID
86
	 *
87
	 * @param   string  $uuid   The string to check
88
	 * @return  boolean
89
	 */
90
	private function isUuid( $uuid ) {
91
		return !(!is_string($uuid) || (preg_match('/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/', $uuid) !== 1));
92
	}
93
94
	private function processStoryblokKeys($block) {
95
		$this->_uid = $block['_uid'] ?? null;
0 ignored issues
show
Bug Best Practice introduced by
The property _uid does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
96
		$this->component = $block['component'] ?? null;
0 ignored issues
show
Bug Best Practice introduced by
The property component does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
97
		$this->content = collect(array_diff_key($block, array_flip(['_editable', '_uid', 'component', 'plugin'])));
0 ignored issues
show
Bug Best Practice introduced by
The property content does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
98
	}
99
}