Test Failed
Push — master ( 45bc78...882099 )
by Alexey
05:03
created

Catalog::calcItemsCount()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 9
nc 6
nop 1
dl 0
loc 13
rs 9.2
c 0
b 0
f 0
1
<?php
2
/**
3
 * INJI
4
 *
5
 * @author Alexey Krupskiy <[email protected]>
6
 * @link http://inji.ru/
7
 * @copyright 2017 Alexey Krupskiy
8
 * @license https://github.com/injitools/Inji/blob/master/LICENSE
9
 */
10
11
namespace Ecommerce;
12
13
/**
14
 * @property int $id
15
 * @property string $name
16
 * @property int $weight
17
 * @property int $parent_id
18
 * @property int $icon_file_id
19
 * @property int $items_count
20
 * @property \Ecommerce\Catalog $parent
21
 * @property \Ecommerce\Catalog[] $childs
22
 * @property \Ecommerce\Catalog\Category[] $categories
23
 * @property \Files\File $icon
24
 */
25
class Catalog extends \Model {
26
    static $cols = [
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $cols.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
27
        'name' => ['type' => 'text'],
28
        'weight' => ['type' => 'number'],
29
        'parent_id' => ['type' => 'select', 'source' => 'relation', 'relation' => 'parent', 'extraValues' => ['0' => 'Нет родителя']],
30
        'icon_file_id' => ['type' => 'image'],
31
        'items_count' => ['type' => 'number'],
32
        'childsMgr' => ['type' => 'dataManager', 'relation' => 'childs'],
33
        'categoriesMgr' => ['type' => 'dataManager', 'relation' => 'categories'],
34
    ];
35
    static $labels = [
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $labels.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
36
        'name' => 'Название',
37
        'weight' => 'Вес соритровки',
38
        'parent_id' => 'Родитель',
39
        'icon_file_id' => 'Иконка',
40
        'childsMgr' => 'Дочерние каталоги',
41
        'categoriesMgr' => 'Категории товаров',
42
    ];
43
    static $dataManagers = [
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $dataManagers.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
44
        'manager' => [
45
            'filters' => ['name', 'parent_id'],
46
            'cols' => ['name', 'icon_file_id', 'childsMgr', 'categoriesMgr'],
47
            'sortMode' => true
48
        ]
49
    ];
50
    static $forms = [
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $forms.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
51
        'manager' => [
52
            'map' => [
53
                ['name', 'icon_file_id'],
54
                ['categoriesMgr'],
55
                ['childsMgr'],
56
57
            ]
58
        ]
59
    ];
60
61
    public function calcItemsCount($save = true) {
62
        $count = 0;
63
        foreach ($this->categories as $category) {
64
            $count += $category->category->items_count;
65
        }
66
        $this->items_count = $count;
67
        if ($save) {
68
            $this->save();
69
            if ($this->parent) {
70
                $this->parent->calcItemsCount();
71
            }
72
        }
73
    }
74
75 View Code Duplication
    static function relations() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
76
        return [
77
            'parent' => [
78
                'model' => 'Ecommerce\Catalog',
79
                'col' => 'parent_id'
80
            ],
81
            'childs' => [
82
                'type' => 'many',
83
                'col' => 'parent_id',
84
                'model' => 'Ecommerce\Catalog'
85
            ],
86
            'categories' => [
87
                'type' => 'many',
88
                'col' => 'catalog_id',
89
                'model' => 'Ecommerce\Catalog\Category'
90
            ],
91
            'icon' => [
92
                'col' => 'icon_file_id',
93
                'model' => 'Files\File'
94
            ]
95
        ];
96
    }
97
}