Completed
Push — master ( 37f66f...c69285 )
by Andrii
12:09
created

ServerResource::tableName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
ccs 0
cts 4
cp 0
crap 2
rs 10
c 0
b 0
f 0
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-2017, 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\server\AbstractServerResourceDecorator;
15
use hipanel\modules\finance\models\decorators\server\ServerResourceDecoratorFactory;
16
use Yii;
17
18
/**
19
 * Class DomainResource.
20
 */
21
class ServerResource extends Resource
22
{
23
    use ModelTrait;
24
25
    public static function tableName()
26
    {
27
        return 'resource';
28
    }
29
30
    const MODEL_TYPE_CPU = 'cpu';
31
    const MODEL_TYPE_RAM = 'ram';
32
    const MODEL_TYPE_HDD = 'hdd';
33
    const MODEL_TYPE_CHASSIS = 'chassis';
34
35
    const TYPE_ISP5 = 'isp5';
36
    const TYPE_ISP = 'isp';
37
    const TYPE_SUPPORT_TIME = 'support_time';
38
    const TYPE_IP_NUMBER = 'ip_num';
39
    const TYPE_SERVER_TRAF_MAX = 'server_traf_max';
40
    const TYPE_SERVER_TRAF95_MAX = 'server_traf95_max';
41
    const TYPE_BACKUP_DU = 'backup_du';
42
43
    public function rules()
44
    {
45
        $rules = parent::rules();
46
        $rules[] = [['model_type', 'partno', 'r_object_id'], 'safe'];
47
        $rules['create-required'] = [
48
            ['object_id'],
49
            'required',
50
            'on' => ['create', 'update'],
51
            'when' => function ($model) {
52
                return $model->isHardwareTypeCorrect();
53
            },
54
        ];
55
        unset($rules['create-required-price']);
56
57
        return $rules;
58
    }
59
60
    /**
61
     * @return array
62
     */
63
    public function getHardwareTypes()
64
    {
65
        return [
66
            static::MODEL_TYPE_CHASSIS => Yii::t('hipanel:finance:tariff', 'Chassis'),
67
            static::MODEL_TYPE_CPU => Yii::t('hipanel:finance:tariff', 'CPU'),
68
            static::MODEL_TYPE_RAM => Yii::t('hipanel:finance:tariff', 'RAM'),
69
            static::MODEL_TYPE_HDD => Yii::t('hipanel:finance:tariff', 'HDD'),
70
        ];
71
    }
72
73
    public function isHardwareTypeCorrect()
74
    {
75
        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...
76
    }
77
78
    public function getTypes()
79
    {
80
        return [
81
            static::TYPE_ISP5 => Yii::t('hipanel:finance:tariff', 'ISP Manager 5'),
82
            static::TYPE_ISP => Yii::t('hipanel:finance:tariff', 'ISP Manager'),
83
            static::TYPE_SUPPORT_TIME => Yii::t('hipanel:finance:tariff', 'Support time'),
84
            static::TYPE_IP_NUMBER => Yii::t('hipanel:finance:tariff', 'IP addresses count'),
85
            static::TYPE_SERVER_TRAF_MAX => Yii::t('hipanel:finance:tariff', 'Server traffic'),
86
            static::TYPE_SERVER_TRAF95_MAX => Yii::t('hipanel:finance:tariff', '95 percentile traffic'),
87
            static::TYPE_BACKUP_DU => Yii::t('hipanel:finance:tariff', 'Backup disk usage'),
88
        ];
89
    }
90
91
    /**
92
     * @return AbstractServerResourceDecorator
93
     */
94
    public function decorator()
95
    {
96
        if (empty($this->decorator)) {
97
            $this->decorator = ServerResourceDecoratorFactory::createFromResource($this);
98
        }
99
100
        return $this->decorator;
101
    }
102
103
    public function realObjectId()
104
    {
105
        if (!$this->isPeriodic()) {
106
            return $this->object_id;
0 ignored issues
show
Documentation introduced by
The property object_id 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...
107
        }
108
109
        if (!$this->tariff->is_personal && isset($this->r_object_id)) {
0 ignored issues
show
Documentation introduced by
The property is_personal does not exist on object<hipanel\modules\finance\models\Tariff>. 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...
110
            return $this->r_object_id;
0 ignored issues
show
Documentation introduced by
The property r_object_id 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...
111
        }
112
113
        return $this->object_id;
0 ignored issues
show
Documentation introduced by
The property object_id 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...
114
    }
115
}
116