Completed
Push — master ( c6b5de...db50e7 )
by Dmitry
04:45
created

src/models/ServerResource.php (6 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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\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_WIN_LICENSE = 'win_license';
47
    const TYPE_SERVER_DU = 'server_du';
48
    const TYPE_MONTHLY = 'monthly';
49
50
    public function rules()
51
    {
52
        $rules = parent::rules();
53
        $rules[] = [['model_type', 'partno', 'r_object_id'], 'safe'];
54
        $rules['create-required'] = [
55
            ['object_id'],
56
            'required',
57
            'on' => ['create', 'update'],
58
            'when' => function ($model) {
59
                return $model->isHardwareTypeCorrect();
60
            },
61
        ];
62
        unset($rules['create-required-price']);
63
64
        return $rules;
65
    }
66
67
    /**
68
     * @return array
69
     */
70
    public function getHardwareTypes()
71
    {
72
        return [
73
            static::MODEL_TYPE_CHASSIS => Yii::t('hipanel:finance:tariff', 'Chassis'),
74
            static::MODEL_TYPE_CPU => Yii::t('hipanel:finance:tariff', 'CPU'),
75
            static::MODEL_TYPE_RAM => Yii::t('hipanel:finance:tariff', 'RAM'),
76
            static::MODEL_TYPE_HDD => Yii::t('hipanel:finance:tariff', 'HDD'),
77
        ];
78
    }
79
80
    public function isHardwareTypeCorrect()
81
    {
82
        return isset($this->getHardwareTypes()[$this->model_type]);
0 ignored issues
show
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...
83
    }
84
85
    public function getTypes()
86
    {
87
        /** @var ServerResourceTypesProviderInterface $provider */
88
        $provider = Yii::createObject(ServerResourceTypesProviderInterface::class);
89
90
        return $provider->getTypes();
91
    }
92
93
    public function getMinimumQuantity()
94
    {
95
        $types = [
96
            static::TYPE_MONTHLY => 0,
97
        ];
98
99
        return isset($types[$this->type]) ? $types[$this->type] : 0.01;
0 ignored issues
show
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...
100
    }
101
102
    /**
103
     * @return AbstractServerResourceDecorator
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
    public function realObjectId()
115
    {
116
        if (!$this->isPeriodic()) {
117
            return $this->object_id;
0 ignored issues
show
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...
118
        }
119
120
        if (!$this->tariff->is_personal && isset($this->r_object_id)) {
0 ignored issues
show
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...
121
            return $this->r_object_id;
0 ignored issues
show
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...
122
        }
123
124
        return $this->object_id;
0 ignored issues
show
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...
125
    }
126
}
127