Completed
Push — master ( 3f30e7...37b632 )
by Dmitry
08:21
created

Context::toArray()   B

Complexity

Conditions 7
Paths 9

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 11.8162

Importance

Changes 0
Metric Value
dl 0
loc 22
ccs 7
cts 13
cp 0.5385
rs 8.6346
c 0
b 0
f 0
cc 7
nc 9
nop 0
crap 11.8162
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 4
    public function reset($context = []) : self
21
    {
22 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...
23 4
            $this->$k = null;
24
        }
25 4
        $this->apply($context);
26
27 4
        return $this;
28
    }
29
30 4
    public function apply($data) : self
31
    {
32 4
        foreach ($data as $k => $v) {
33 3
            if ($k == 'parent') {
34
                $v = (object) $v;
35
            }
36 3
            $this->$k = $v;
37
        }
38
39 4
        return $this;
40
    }
41
42 2
    public function getPerson()
43
    {
44 2
        return $this->parent ? $this->parent->person : $this->person;
45
    }
46
47 3
    public function toArray(): array
48
    {
49 3
        $result = [];
50 3
        foreach (get_object_vars($this) as $k => $v) {
51 3
            if (!is_null($v)) {
52 2
                if ($k == 'parent') {
53
                    $result[$k] = [];
54
                    foreach ($v as $kk => $vv) {
55
                        if ($vv) {
56
                            $result[$k][$kk] = $vv;
57
                        }
58
                    }
59
                    if (!count($result[$k])) {
60
                        unset($result[$k]);
61
                    }
62
                } else {
63 2
                    $result[$k] = $v;
64
                }
65
            }
66
        }
67 3
        return $result;
68
    }
69
}
70