Completed
Push — master ( 160801...bd59d4 )
by Andreas
27:08
created

privilege::get_privilege()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2.0078

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 2
nop 0
dl 0
loc 11
ccs 7
cts 8
cp 0.875
crap 2.0078
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright CONTENT CONTROL GmbH, http://www.contentcontrol-berlin.de
4
 */
5
6
namespace midcom\datamanager\storage;
7
8
use midcom_core_dbaobject;
9
use midcom_core_privilege;
10
11
/**
12
 * Experimental storage class
13
 */
14
class privilege extends delayed
15
{
16 5
    public function __construct(midcom_core_dbaobject $object, array $config)
17
    {
18 5
        if (!array_key_exists('classname', $config['type_config'])) {
19 2
            $config['type_config']['classname'] = '';
20
        }
21 5
        parent::__construct($object, $config);
22 5
    }
23
24
    /**
25
     * {@inheritdoc}
26
     */
27 5
    public function load()
28
    {
29 5
        $source = $this->get_privilege();
30 5
        return $source->value;
31
    }
32
33
    /**
34
     * {@inheritdoc}
35
     */
36
    public function save()
37
    {
38
        $privilege = $this->get_privilege();
39
        $privilege->value = $this->value;
40
        $privilege->store();
41
    }
42
43
    /**
44
     * @return \midcom_core_privilege
45
     */
46 5
    private function get_privilege() : midcom_core_privilege
47
    {
48 5
        $privilege = $this->get_privilege_object()->get_privilege(
49 5
            $this->config['type_config']['privilege_name'],
50 5
            $this->config['type_config']['assignee'],
51 5
            $this->config['type_config']['classname']);
52
53 5
        if ($privilege === false) {
0 ignored issues
show
introduced by
The condition $privilege === false is always false.
Loading history...
54
            throw new \midcom_error_forbidden('Failed to load privilege');
55
        }
56 5
        return $privilege;
57
    }
58
59
    /**
60
     * @return midcom_core_dbaobject
61
     */
62 5
    private function get_privilege_object() : midcom_core_dbaobject
63
    {
64 5
        if (!empty($this->config['type_config']['privilege_object'])) {
65 2
            return $this->config['type_config']['privilege_object'];
66
        }
67 5
        return $this->object;
68
    }
69
}