PageSettingsCollection   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 10
c 1
b 0
f 0
dl 0
loc 24
ccs 11
cts 11
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __call() 0 8 2
A getSettingValue() 0 12 4
1
<?php
2
3
namespace Thinkone\NovaPageSettings\Model;
4
5
use Illuminate\Database\Eloquent\Collection;
6
use Illuminate\Database\Eloquent\Model;
7
use Illuminate\Support\Arr;
8
use Illuminate\Support\Str;
9
10
/**
11
 * @extends Collection<int, Model>
12
 */
13
class PageSettingsCollection extends Collection
14
{
15 2
    public function getSettingValue(string $key, string $type = 'string', $default = null, ...$parameters)
16
    {
17
        /** @var PageSetting $setting */
18 2
        if ($setting = $this->firstWhere('key', $key)) {
19 2
            if (is_object($setting) && method_exists($setting, ($method = 'value'.Str::ucfirst($type)))) {
20 1
                return $setting->$method(...$parameters);
21
            }
22
23 1
            return Arr::get($setting, 'value');
24
        }
25
26 1
        return $default;
27
    }
28
29 2
    public function __call($method, $parameters)
30
    {
31 2
        $suffix = 'SettingValue';
32 2
        if (Str::endsWith($method, $suffix)) {
33 1
            return $this->getSettingValue(array_shift($parameters), Str::beforeLast($method, $suffix), array_shift($parameters), ...$parameters);
34
        }
35
36 1
        return parent::__call($method, $parameters);
37
    }
38
}
39