PageSettingsCollection::__call()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 10
cc 2
nc 2
nop 2
crap 2
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