Completed
Push — master ( f9ee1a...296051 )
by Dmitry
03:24
created

Context::execute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 0
cts 6
cp 0
rs 9.9332
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 2
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