Completed
Push — master ( 3d52a5...b51608 )
by Dmitry
04:01
created

ServerResource::getMinimumQuantity()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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