Completed
Push — master ( 0622f1...018a2f )
by Dmitry
04:34
created

ServerResource::getAvailableTypes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 12
ccs 0
cts 12
cp 0
rs 9.4285
cc 1
eloc 9
nc 1
nop 0
crap 2

1 Method

Rating   Name   Duplication   Size   Complexity  
A ServerResource::isHardwareTypeCorrect() 0 4 1
1
<?php
2
3
/*
4
 * Finance module for HiPanel
5
 *
6
 * @link      https://github.com/hiqdev/hipanel-module-finance
7
 * @package   hipanel-module-finance
8
 * @license   BSD-3-Clause
9
 * @copyright Copyright (c) 2015-2016, HiQDev (http://hiqdev.com/)
10
 */
11
12
namespace hipanel\modules\finance\models;
13
14
use hipanel\base\ModelTrait;
15
use hipanel\modules\finance\models\decorators\AbstractResourceDecorator;
16
use hipanel\modules\finance\models\decorators\server\AbstractServerResourceDecorator;
17
use hipanel\modules\finance\models\decorators\server\BackupResourceDecorator;
18
use hipanel\modules\finance\models\decorators\server\ServerResourceDecoratorFactory;
19
use Yii;
20
21
/**
22
 * Class DomainResource
23
 * @package hipanel\modules\finance\models
24
 */
25
class ServerResource extends Resource
26
{
27
    use ModelTrait;
28
29
    public static function index()
30
    {
31
        return 'resources';
32
    }
33
34
    public static function type()
35
    {
36
        return 'resource';
37
    }
38
39
    const MODEL_TYPE_CPU = 'cpu';
40
    const MODEL_TYPE_RAM = 'ram';
41
    const MODEL_TYPE_HDD = 'hdd';
42
    const MODEL_TYPE_CHASSIS = 'chassis';
43
44
    const TYPE_ISP5 = 'isp5';
45
    const TYPE_ISP = 'isp';
46
    const TYPE_SUPPORT_TIME = 'support_time';
47
    const TYPE_IP_NUMBER = 'ip_num';
48
    const TYPE_SERVER_TRAF_MAX = 'server_traf_max';
49
    const TYPE_SERVER_TRAF95_MAX = 'server_traf95_max';
50
    const TYPE_BACKUP_DU = 'backup_du';
51
52
    public function rules()
53
    {
54
        $rules = parent::rules();
55
        $rules[] = [['model_type', 'partno'], 'safe'];
56
        $rules['create-required'] = [
57
            ['object_id'],
58
            'required',
59
            'on' => ['create', 'update'],
60
            'when' => function ($model) {
61
                return $model->isHardwareTypeCorrect();
62
            }
63
        ];
64
        unset($rules['create-required-price']);
65
66
67
        return $rules;
68
    }
69
70
    /**
71
     * @return array
72
     */
73
    public function getHardwareTypes()
74
    {
75
        return [
76
            static::MODEL_TYPE_CHASSIS => Yii::t('hipanel/finance/tariff', 'Chassis'),
77
            static::MODEL_TYPE_CPU => Yii::t('hipanel/finance/tariff', 'CPU'),
78
            static::MODEL_TYPE_RAM => Yii::t('hipanel/finance/tariff', 'RAM'),
79
            static::MODEL_TYPE_HDD => Yii::t('hipanel/finance/tariff', 'HDD'),
80
        ];
81
    }
82
83
    public function isHardwareTypeCorrect()
84
    {
85
        return isset($this->getHardwareTypes()[$this->model_type]);
0 ignored issues
show
Documentation introduced by
The property model_type does not exist on object<hipanel\modules\f...\models\ServerResource>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
86
    }
87
88
89
    public function getTypes()
90
    {
91
        return [
92
            static::TYPE_ISP5 => Yii::t('hipanel/finance/tariff', 'ISP Manager 5'),
93
            static::TYPE_ISP => Yii::t('hipanel/finance/tariff', 'ISP Manager'),
94
            static::TYPE_SUPPORT_TIME => Yii::t('hipanel/finance/tariff', 'Support time'),
95
            static::TYPE_IP_NUMBER => Yii::t('hipanel/finance/tariff', 'IP addresses count'),
96
            static::TYPE_SERVER_TRAF_MAX => Yii::t('hipanel/finance/tariff', 'Server traffic'),
97
            static::TYPE_SERVER_TRAF95_MAX => Yii::t('hipanel/finance/tariff', '95 percentile traffic'),
98
            static::TYPE_BACKUP_DU => Yii::t('hipanel/finance/tariff', 'Backup disk usage'),
99
        ];
100
    }
101
102
    /**
103
     * @return AbstractResourceDecorator
104
     */
105
    public function decorator()
106
    {
107
        if (empty($this->decorator)) {
108
            $this->decorator = ServerResourceDecoratorFactory::createFromResource($this);
109
        }
110
111
        return $this->decorator;
112
    }
113
}
114