Completed
Push — master ( 15c9e2...d1079a )
by wen
03:11
created

Config::parsePermission()   B

Complexity

Conditions 8
Paths 7

Size

Total Lines 18
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 7.7777
c 0
b 0
f 0
cc 8
eloc 13
nc 7
nop 0
1
<?php
2
3
namespace Sco\Admin\Config;
4
5
use Sco\Attributes\HasAttributesTrait;
6
7
abstract class Config implements ConfigInterface
8
{
9
    use HasAttributesTrait;
10
11
    protected $defaultPermissions = [
12
        'view'   => true,
13
        'create' => true,
14
        'update' => true,
15
        'delete' => true,
16
    ];
17
18
    public function __construct(array $attributes)
19
    {
20
        $this->setAttribute($attributes);
21
        $this->parse();
22
    }
23
24
    protected function parse()
25
    {
26
        $this->parsePermission();
27
    }
28
29
    protected function parsePermission()
30
    {
31
        $attribute = collect();
32
        $permissions  = $this->getAttribute('permissions');
33
        if (is_array($permissions)) {
34
            $option = array_merge($this->defaultPermissions, $permissions);
35
            foreach ($option as $key => $item) {
36
                $val = $item instanceof \Closure ? $item() : $item;
37
                $attribute->put($key, $val ? true : false);
38
            }
39
        } else {
40
            $val = $permissions instanceof \Closure ? $permissions() : $permissions;
41
            foreach ($this->defaultPermissions as $key => $item) {
42
                $attribute->put($key, $val ? true : false);
43
            }
44
        }
45
        $this->setAttribute('permissions', $attribute->toArray());
46
    }
47
48
    public function getOption($key, $default = null)
49
    {
50
        return $this->getAttribute($key, $default);
51
    }
52
53
    public function __toString()
54
    {
55
        return $this->toJson();
0 ignored issues
show
Bug introduced by
The method toJson() does not seem to exist on object<Sco\Admin\Config\Config>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
56
    }
57
}
58