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

Config   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 2
dl 0
loc 51
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A parse() 0 4 1
B parsePermission() 0 18 8
A getOption() 0 4 1
A __toString() 0 4 1
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