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

Context   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 73.08%

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 0
dl 0
loc 63
ccs 19
cts 26
cp 0.7308
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A reset() 0 9 2
A apply() 0 11 3
A getPerson() 0 4 2
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 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