Completed
Pull Request — master (#254)
by Anton
48:46 queued 42:49
created

Table::getAllCategories()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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