Context   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 59.38%

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 0
dl 0
loc 74
ccs 19
cts 32
cp 0.5938
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A execute() 0 10 1
A reset() 0 9 2
A apply() 0 11 3
A getPerson() 0 4 3
B toArray() 0 22 7
1
<?php
2
3
namespace Basis;
4
5
use Carbon\Carbon;
6
7
class Context
8
{
9
    public $access;
10
    public $channel;
11
    public $session;
12
13
    public $company;
14
    public $person;
15
    public $module;
16
17
    public $parent;
18
    public $event;
19
20
    public function execute($context, $callback)
21
    {
22
        $origin = $this->toArray();
23
24
        $this->reset($context);
25
        $result = call_user_func($callback);
26
        $this->reset($origin);
27
28
        return $result;
29
    }
30
31 4
    public function reset($context = []) : self
32
    {
33 4
        foreach ($this as $k => $_) {
0 ignored issues
show
Bug introduced by
The expression $this of type this<Basis\Context> is not traversable.
Loading history...
34 4
            $this->$k = null;
35
        }
36 4
        $this->apply($context);
37
38 4
        return $this;
39
    }
40
41 4
    public function apply($data) : self
42
    {
43 4
        foreach ($data as $k => $v) {
44 3
            if ($k == 'parent') {
45
                $v = (object) $v;
46
            }
47 3
            $this->$k = $v;
48
        }
49
50 4
        return $this;
51
    }
52
53 2
    public function getPerson()
54
    {
55 2
        return $this->parent && $this->parent->person ? $this->parent->person : $this->person;
56
    }
57
58 3
    public function toArray(): array
59
    {
60 3
        $result = [];
61 3
        foreach (get_object_vars($this) as $k => $v) {
62 3
            if (!is_null($v)) {
63 2
                if ($k == 'parent') {
64
                    $result[$k] = [];
65
                    foreach ($v as $kk => $vv) {
66
                        if ($vv) {
67
                            $result[$k][$kk] = $vv;
68
                        }
69
                    }
70
                    if (!count($result[$k])) {
71
                        unset($result[$k]);
72
                    }
73
                } else {
74 2
                    $result[$k] = $v;
75
                }
76
            }
77
        }
78 3
        return $result;
79
    }
80
}
81