Passed
Push — master ( a5fc0a...e954fa )
by Richard
14:40 queued 12s
created

HasSettings   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 23
dl 0
loc 48
rs 10
c 4
b 0
f 0
wmc 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A settings() 0 6 2
A preprocessHasSettings() 0 22 3
A isCommaSeparatedList() 0 8 2
A hasSettings() 0 2 1
1
<?php
2
3
namespace Riclep\Storyblok\Traits;
4
5
use Illuminate\Support\Str;
6
7
trait HasSettings
8
{
9
	protected $_settings;
10
11
	public function preprocessHasSettings($content) {
12
		if (array_key_exists(config('storyblok.settings_field'), $content)) {  // TODO set key in config
13
			$this->_settings = collect($content[config('storyblok.settings_field')])->keyBy(function($setting) {
14
				return Str::slug($setting['component'], '_');
15
			})->map(function ($setting) {
16
				$settings = collect(array_diff_key($setting, array_flip(['_editable', '_uid', 'component'])))
0 ignored issues
show
Bug introduced by
array_diff_key($setting,... '_uid', 'component'))) of type array is incompatible with the type Illuminate\Contracts\Support\Arrayable expected by parameter $value of collect(). ( Ignorable by Annotation )

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

16
				$settings = collect(/** @scrutinizer ignore-type */ array_diff_key($setting, array_flip(['_editable', '_uid', 'component'])))
Loading history...
17
					->map(function($setting) {
18
						if ($this->isCommaSeparatedList($setting)) {
19
							return $this->isCommaSeparatedList($setting);
20
						}
21
22
						return $setting;
23
					});
24
25
				return $settings;
26
			});
27
		}
28
29
		// remove the processed item for future items
30
		unset($content[config('storyblok.settings_field')]);
31
32
		return $content;
33
	}
34
35
	protected function isCommaSeparatedList($string) {
36
		if (!preg_match('/^[\w]+(,[\w]*)+$/', $string)) {
37
			return false;
38
		}
39
40
		return array_map(function($item) {
41
			return (int) trim($item);
42
		}, explode(',', $string));
43
	}
44
45
	public function settings($setting = null) {
46
		if ($setting) {
47
			return $this->_settings[$setting];
48
		}
49
50
		return $this->_settings;
51
	}
52
53
	public function hasSettings() {
54
		return $this->_settings;
55
	}
56
}