Test Failed
Push — master ( 2a8a67...5ddd1f )
by Alexey
04:25
created

Param::realType()   B

Complexity

Conditions 9
Paths 12

Size

Total Lines 19
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 90

Importance

Changes 0
Metric Value
cc 9
eloc 15
nc 12
nop 0
dl 0
loc 19
ccs 0
cts 2
cp 0
crap 90
rs 7.756
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Item param
5
 *
6
 * @author Alexey Krupskiy <[email protected]>
7
 * @link http://inji.ru/
8
 * @copyright 2015 Alexey Krupskiy
9
 * @license https://github.com/injitools/cms-Inji/blob/master/LICENSE
10
 */
11
12
namespace Ecommerce\Item;
13
/**
14
 * @property int $id
15
 * @property int $item_option_id
16
 * @property int $item_id
17
 * @property string $value
18
 * @property string $date_create
19
 *
20
 * @property-read \Ecommerce\Item\Option $option
21
 * @property-read \Ecommerce\Item\Option\Item $optionItem
22
 * @property-read \Ecommerce\Item $item
23
 * @property-read \Files\File $file
24
 */
25
class Param extends \Model {
26
27
    public static $objectName = 'Параметр товара';
28
    public static $labels = [
29
        'item_option_id' => 'Параметр',
30
        'item_id' => 'Товар',
31
        'value' => 'Значение',
32
    ];
33
    public static $cols = [
34
        //Основные параметры
35
        'item_id' => ['type' => 'select', 'source' => 'relation', 'relation' => 'item'],
36
        'item_option_id' => ['type' => 'select', 'source' => 'relation', 'relation' => 'option', 'onChange' => 'reloadForm'],
37
        'value' => ['type' => 'dynamicType', 'typeSource' => 'selfMethod', 'selfMethod' => 'realType'],
38
        //Системные
39
        'date_create' => ['type' => 'dateTime']
40
    ];
41
42
    public static function indexes() {
43
        return [
44
            'ecommerce_itemOptionRelation' => [
45
                'type' => 'INDEX',
46
                'cols' => [
47
                    'item_param_item_id',
48
                    'item_param_item_option_id'
49
                ]
50
            ],
51
            'ecommerce_paramItemIndex' => [
52
                'type' => 'INDEX',
53
                'cols' => [
54
                    'item_param_item_id',
55
                ]
56
            ],
57
            'ecommerce_paramOptionIndex' => [
58
                'type' => 'INDEX',
59
                'cols' => [
60
                    'item_param_item_option_id'
61
                ]
62
            ],
63
        ];
64
    }
65
66
    public function realType() {
67
        $type = 'text';
68
        if (isset($this->item_option_type)) {
69
            $type = $this->item_option_type ? $this->item_option_type : 'text';
0 ignored issues
show
Bug introduced by
The property item_option_type does not seem to exist. Did you mean option?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
70
        } elseif ($this->item_option_id && isset(\Ecommerce\Item\Option::$loaded[$this->item_option_id])) {
71
            $type = \Ecommerce\Item\Option::$loaded[$this->item_option_id]->type ? \Ecommerce\Item\Option::$loaded[$this->item_option_id]->type : 'text';
72
        } elseif ($this->item_option_id && $this->option) {
73
            \Ecommerce\Item\Option::$loaded[$this->item_option_id] = $this->option;
74
            $type = $this->option->type;
75
        }
76
        if ($type == 'select') {
77
            return [
78
                'type' => 'select',
79
                'source' => 'relation',
80
                'relation' => 'option:items',
81
            ];
82
        }
83
        return $type;
84
    }
85
86
    public static $dataManagers = [
87
88
        'manager' => [
89
            'name' => 'Параметры товара',
90
            'cols' => [
91
                'item_option_id',
92
                'item_id',
93
                'value',
94
            ],
95
        ],
96 3
    ];
97 3
    public static $forms = [
98 1
        'manager' => [
99 2
            'map' => [
100 1
                ['item_id', 'item_option_id'],
101
                ['value']
102 1
            ]
103
        ]];
104
105
    /**
106
     * Return final param value
107
     *
108
     * @param string $default
109
     * @return string
110
     */
111
    public function valueText($default = '') {
112
        if ($this->option->type == 'select' && $this->optionItem) {
113
            return $this->optionItem->value;
114
        } elseif ($this->option->type !== 'select' && $this->value) {
115
            return $this->value;
116
        }
117
        return $default;
118
    }
119
120
    public static function relations() {
121
        return [
122
            'file' => [
123
                'model' => 'Files\File',
124
                'col' => 'value'
125
            ],
126
            'option' => [
127
                'model' => 'Ecommerce\Item\Option',
128
                'col' => 'item_option_id'
129
            ],
130
            'item' => [
131
                'model' => 'Ecommerce\Item',
132
                'col' => 'item_id'
133
            ],
134
            'optionItem' => [
135
                'model' => 'Ecommerce\Item\Option\Item',
136
                'col' => 'value'
137
            ]
138
        ];
139
    }
140
}