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

DomainResource::from()   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
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 2
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 Yii;
15
16
/**
17
 * Class DomainResource.
18
 */
19
class DomainResource extends Resource
20
{
21
    use ModelTrait;
22
23
    public static function tableName()
24
    {
25
        return 'resource';
26
    }
27
28
    const TYPE_DOMAIN_REGISTRATION = 'dregistration';
29
    const TYPE_DOMAIN_TRANSFER = 'dtransfer';
30
    const TYPE_DOMAIN_RENEWAL = 'drenewal';
31
    const TYPE_DOMAIN_DELETE_AGP = 'ddelete_agp';
32
    const TYPE_DOMAIN_RESTORE_EXPIRED = 'drestore_expired';
33
    const TYPE_DOMAIN_RESTORE_DELETED = 'drestore_deleted';
34
35
    const TYPE_PREMIUM_DNS = 'premium_dns';
36
37
    public function rules()
38
    {
39
        $rules = parent::rules();
40
        $rules['create-required'] = [
41
            ['object_id'],
42
            'required',
43
            'on' => ['create', 'update'],
44
            'when' => function ($model) {
45
                return $model->isTypeCorrect();
46
            },
47
        ];
48
        $rules['create-required-price'] = [['price'], 'required', 'on' => ['create', 'update']];
49
        $rules[] = [['zone'], 'safe'];
50
51
        return $rules;
52
    }
53
54
    /**
55
     * @return array
56
     */
57
    public function getTypes()
58
    {
59
        return [
60
            static::TYPE_DOMAIN_REGISTRATION => Yii::t('hipanel:finance:tariff', 'Registration'),
61
            static::TYPE_DOMAIN_TRANSFER => Yii::t('hipanel:finance:tariff', 'Transfer'),
62
            static::TYPE_DOMAIN_RENEWAL => Yii::t('hipanel:finance:tariff', 'Renewal'),
63
            static::TYPE_DOMAIN_DELETE_AGP => Yii::t('hipanel:finance:tariff', 'Delete in AGP'),
64
            static::TYPE_DOMAIN_RESTORE_EXPIRED => Yii::t('hipanel:finance:tariff', 'Restoring expired'),
65
            static::TYPE_DOMAIN_RESTORE_DELETED => Yii::t('hipanel:finance:tariff', 'Restoring deleted'),
66
        ];
67
    }
68
69
    public function getServiceTypes()
70
    {
71
        return [
72
            static::TYPE_PREMIUM_DNS => Yii::t('hipanel:finance:tariff', 'Premium DNS'),
73
        ];
74
    }
75
76
    public function isTypeCorrect()
77
    {
78
        return isset($this->getTypes()[$this->type]);
0 ignored issues
show
Documentation introduced by
The property type does not exist on object<hipanel\modules\f...\models\DomainResource>. 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...
79
    }
80
}
81