Completed
Pull Request — master (#254)
by Anton
11:18
created

Table   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 71
ccs 2
cts 2
cp 1
rs 10
wmc 7
lcom 1
cbo 3

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getRootCategories() 0 4 1
A buildTree() 0 5 1
A buildTreeByAlias() 0 6 1
A generateTree() 0 18 4
1
<?php
2
/**
3
 * @copyright Bluz PHP Team
4
 * @link https://github.com/bluzphp/skeleton
5
 */
6
7
/**
8
 * @namespace
9
 */
10
namespace Application\Categories;
11
12
use Bluz\Proxy\Db;
13
14
/**
15
 * Class Table
16
 *
17
 * @package Application\Categories
18
 *
19
 * @method  static Row findRow($primaryKey)
20
 * @method  static Row findRowWhere($whereList)
21
 */
22
class Table extends \Bluz\Db\Table
23
{
24
    /**
25
     * Table
26
     *
27
     * @var string
28
     */
29
    protected $table = 'categories';
30
31
    /**
32
     * Primary key(s)
33
     * @var array
34
     */
35
    protected $primary = array('id');
36
37
    /**
38
     * Get root categories
39
     * @return array
40
     */
41 3
    public function getRootCategories()
42
    {
43 3
        return $this->select()->where('parentId IS NULL')->orderBy('name', 'ASC')->execute();
44
    }
45
    
46
    /**
47
     * Build tree by root id
48
     * @param int $id
49
     * @return Row
50
     */
51
    public function buildTree($id = null)
52
    {
53
        $tree = $this->generateTree($id);
54
        return $tree[$id];
55
    }
56
57
    /**
58
     * Build tree by root alias
59
     * @param string $alias
60
     * @return Row
61
     */
62
    public function buildTreeByAlias($alias)
63
    {
64
        $current = $this->findRow(['alias' => $alias]);
65
66
        return $this->buildTree($current['id']);
67
    }
68
69
    /**
70
     * Get all categories in tree by rootId
71
     * @param integer $rootId
72
     * @return Row[]
73
     */
74
    protected function generateTree($rootId)
75
    {
76
        /** @var Row[] $categories */
77
        $categories = Db::fetchGroup(
78
            'SELECT id, categories.* FROM categories WHERE rootId = :id or id = :id ORDER BY `name`',
79
            ['id' => $rootId],
80
            $this->rowClass
0 ignored issues
show
Unused Code introduced by
The call to Db::fetchGroup() has too many arguments starting with $this->rowClass.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
81
        );
82
        $categories = array_map('reset', $categories);
83
84
85
        foreach ($categories as $category) {
86
            if ($category->parentId && $category->id != $category->parentId) {
87
                $categories[$category->parentId]->addChild($category);
88
            }
89
        }
90
        return $categories;
91
    }
92
}
93