ServerResource   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 111
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 13
lcom 0
cbo 4
dl 0
loc 111
ccs 0
cts 74
cp 0
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A tableName() 0 4 1
A rules() 0 16 1
A getHardwareTypes() 0 9 1
A isHardwareTypeCorrect() 0 4 1
A getTypes() 0 7 1
A getMinimumQuantity() 0 8 2
A decorator() 0 8 2
A realObjectId() 0 12 4
1
<?php
2
/**
3
 * Finance module for HiPanel
4
 *
5
 * @link      https://github.com/hiqdev/hipanel-module-finance
6
 * @package   hipanel-module-finance
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2015-2019, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hipanel\modules\finance\models;
12
13
use hipanel\base\ModelTrait;
14
use hipanel\modules\finance\models\decorators\DecoratedInterface;
15
use hipanel\modules\finance\models\decorators\ResourceDecoratorInterface;
16
use hipanel\modules\finance\models\decorators\server\AbstractServerResourceDecorator;
17
use hipanel\modules\finance\models\decorators\server\ServerResourceDecoratorFactory;
18
use Yii;
19
use yii\base\InvalidConfigException;
20
21
/**
22
 * Class ServerResource.
23
 *
24
 * @property float fee
25
 *
26
 * @author Dmytro Naumenko <[email protected]>
27
 */
28
class ServerResource extends Resource implements DecoratedInterface
29
{
30
    use ModelTrait;
31
32
    public static function tableName()
33
    {
34
        return 'resource';
35
    }
36
37
    const MODEL_TYPE_CPU = 'cpu';
38
39
    const MODEL_TYPE_RAM = 'ram';
40
41
    const MODEL_TYPE_HDD = 'hdd';
42
43
    const MODEL_TYPE_CHASSIS = 'chassis';
44
45
    const TYPE_ISP5 = 'isp5';
46
47
    const TYPE_ISP = 'isp';
48
49
    const TYPE_SUPPORT_TIME = 'support_time';
50
51
    const TYPE_IP_NUMBER = 'ip_num';
52
53
    const TYPE_SERVER_TRAF_MAX = 'server_traf_max';
54
55
    const TYPE_SERVER_TRAF95_MAX = 'server_traf95_max';
56
57
    const TYPE_BACKUP_DU = 'backup_du';
58
59
    const TYPE_WIN_LICENSE = 'win_license';
60
61
    const TYPE_SERVER_DU = 'server_du';
62
63
    const TYPE_MONTHLY = 'monthly';
64
65
    public function rules()
66
    {
67
        $rules = parent::rules();
68
        $rules[] = [['model_type', 'partno', 'r_object_id'], 'safe'];
69
        $rules['create-required'] = [
70
            ['object_id'],
71
            'required',
72
            'on' => ['create', 'update'],
73
            'when' => function ($model) {
74
                return $model->isHardwareTypeCorrect();
75
            },
76
        ];
77
        unset($rules['create-required-price']);
78
79
        return $rules;
80
    }
81
82
    /**
83
     * @return array
84
     */
85
    public function getHardwareTypes()
86
    {
87
        return [
88
            static::MODEL_TYPE_CHASSIS => Yii::t('hipanel:finance:tariff', 'Chassis'),
89
            static::MODEL_TYPE_CPU => Yii::t('hipanel:finance:tariff', 'CPU'),
90
            static::MODEL_TYPE_RAM => Yii::t('hipanel:finance:tariff', 'RAM'),
91
            static::MODEL_TYPE_HDD => Yii::t('hipanel:finance:tariff', 'HDD'),
92
        ];
93
    }
94
95
    public function isHardwareTypeCorrect()
96
    {
97
        return isset($this->getHardwareTypes()[$this->model_type]);
98
    }
99
100
    public function getTypes()
101
    {
102
        /** @var ServerResourceTypesProviderInterface $provider */
103
        $provider = Yii::createObject(ServerResourceTypesProviderInterface::class);
104
105
        return $provider->getTypes();
106
    }
107
108
    public function getMinimumQuantity()
109
    {
110
        $types = [
111
            static::TYPE_MONTHLY => 0,
112
        ];
113
114
        return isset($types[$this->type]) ? $types[$this->type] : 0.01;
115
    }
116
117
    public function decorator(): ResourceDecoratorInterface
118
    {
119
        if (empty($this->decorator)) {
120
            $this->decorator = ServerResourceDecoratorFactory::createFromResource($this);
121
        }
122
123
        return $this->decorator;
124
    }
125
126
    public function realObjectId()
127
    {
128
        if (!$this->isPeriodic()) {
129
            return $this->object_id;
130
        }
131
132
        if (!$this->tariff->is_personal && isset($this->r_object_id)) {
133
            return $this->r_object_id;
134
        }
135
136
        return $this->object_id;
137
    }
138
}
139