Passed
Push — master ( 0eb049...b6cfd3 )
by Yuichi
01:50
created

TreeStructureTrait::addCount()   B

Complexity

Conditions 8
Paths 7

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 8

Importance

Changes 0
Metric Value
cc 8
nc 7
nop 4
dl 0
loc 17
ccs 11
cts 11
cp 1
crap 8
rs 8.4444
c 0
b 0
f 0
1
<?php
2
namespace Sonar\Common;
3
use Exception;
4
5
trait TreeStructureTrait
6
{
7
    protected $tree;
8
9 3
    public function getTree()
10
    {
11 3
        return $this->tree;
12
    }
13 1
    public function initTree()
14
    {
15 1
        $this->tree = [];
16 1
    }
17 9
    public function addTreeStructure($data)
18
    {
19 9
        $tree = isset($this->tree) ? $this->tree : [];
20
21 9
        foreach ($data as $rec) {
22 9
            if (is_object($rec) === false) {
23 1
                throw new Exception('data record must be object.');
24
            }
25 8
            if (isset($rec->id) === false) {
26 2
                throw new Exception('data record object does not have property => "id".');
27
            }
28 7
            if (isset($rec->parent_id) === false || ! $rec->parent_id) {
29 7
                $rec->children = [];
30 7
                $tree[$rec->id] = $rec;
31
            } else {
32 6
                $this->addTree($tree, $rec);
33
            }
34
        }
35 7
        $this->tree = $tree;
36 7
    }
37 6
    protected function addTree(&$tree, $data)
38
    {
39 6
        foreach ($tree as $id => &$rec) {
40 6
            if ($data->parent_id == $id) {
41 6
                $data->children = [];
42 6
                $rec->children[$data->id] = $data;
43 6
                return;
44 6
            } elseif (count($rec->children)) {
45 6
                $this->addTree($rec->children, $data);
46
            }
47 6
            unset($rec);
48
        }
49 6
    }
50 5
    public function mergeCount($count_data,$is_merge=false)
51
    {
52 5
        $tree = isset($this->tree) ? $this->tree : [];
53 5
        foreach ( $count_data as $count ) {
54 5
            if (isset($count->id) === false) {
55 2
                throw new Exception('count data record object does not have property => "id".');
56
            }
57 3
            if (isset($count->count) === false) {
58 1
                throw new Exception('count data record object does not have property => "count".');
59
            }
60 2
            $this->addCount($this->tree,$count->id,$count->count,$is_merge);
61
62
        }
63 2
        $this->tree = $tree;
64 2
    }
65 2
    protected function addCount(&$tree,$id,$count,$is_merge)
66
    {
67 2
        foreach ( $tree as $rec ) {
68 2
            if ( $rec->id == $id ) {
69 2
                if( isset($rec->count) ) {
70 1
                    $rec->count += $count;
71
                } else {
72 2
                    $rec->count = $count;
73
                } 
74 2
                if ( isset($rec->parent_id) && $rec->parent_id && $is_merge ) {
75 2
                    $this->addCount($this->tree,$rec->parent_id,$count,$is_merge);
76
                }
77 2
            } elseif ( count($rec->children) ) {
78 2
                $this->addCount($rec->children,$id,$count,$is_merge);
79
            }
80
        }
81 2
    }
82
}
83