Completed
Push — master ( ef5bc2...840014 )
by Klochok
04:47
created

TariffProfile::getTitle()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 2
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-2019, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hipanel\modules\finance\models;
12
13
use Yii;
14
15
/**
16
 * Class Tariff.
17
 * @property resource[]|DomainResource[]|ServerResource[] $resources
18
 */
19
class TariffProfile extends \hipanel\base\Model
20
{
21
    use \hipanel\base\ModelTrait;
22
23
    public static function tableName()
24
    {
25
        return 'tariffprofile';
26
    }
27
28
    /**
29
     * {@inheritdoc}
30
     */
31
    public function rules()
32
    {
33
        $main = [
34
            [['name'], 'safe'],
35
            [
36
                ['name'],
37
                'required',
38
                'on' => ['update', 'create'],
39
                'when' => function ($model) {
40
                    return !$model->isDefault();
41
                },
42
            ],
43
            [['id'], 'integer'],
44
            [['id'], 'required', 'on' => ['update', 'delete']],
45
            [['tariff_names'], 'safe'],
46
            [['seller_id', 'client_id'], 'integer'],
47
            [['seller', 'client', 'items'], 'safe'],
48
            [['tariffs'], 'safe'],
49
        ];
50
51
        $tariffTypes = [Tariff::TYPE_DOMAIN, Tariff::TYPE_CERT, Tariff::TYPE_XEN, Tariff::TYPE_OPENVZ, Tariff::TYPE_SERVER];
52
        foreach ($tariffTypes as $type) {
53
            if (in_array($type, [Tariff::TYPE_DOMAIN, Tariff::TYPE_CERT], true)) {
54
                $main[] = [[$type], 'integer'];
55
            } else {
56
                $main[] = [[$type], 'filter', 'filter' => function ($value) { return explode(',', $value); }];
57
                $main[] = [[$type], 'each', 'rule' => ['trim'], 'on' => ['update', 'create']];
58
                $main[] = [[$type], 'each', 'rule' => ['integer'], 'on' => ['update', 'create']];
59
            }
60
        }
61
62
        return $main;
63
    }
64
65
    public function beforeValidate()
66
    {
67
        foreach ([Tariff::TYPE_XEN, Tariff::TYPE_OPENVZ, Tariff::TYPE_SERVER] as $attribute) {
68
            if (empty($this->$attribute)) {
69
                continue;
70
            }
71
            $this->$attribute = reset($this->$attribute);
72
        }
73
74
        return parent::beforeValidate();
75
    }
76
77
    /**
78
     * {@inheritdoc}
79
     */
80
    public function attributeLabels()
81
    {
82
        return $this->mergeAttributeLabels([
83
            'name' => Yii::t('hipanel.finance.tariffprofile', 'Name'),
84
            'tariff_names' => Yii::t('hipanel.finance.tariffprofile', 'Tariffs'),
85
            'domain' => Yii::t('hipanel.finance.tariffprofile', 'Domain tariff'),
86
            'certificate' => Yii::t('hipanel.finance.tariffprofile', 'Certificate tariff'),
87
            'svds' => Yii::t('hipanel.finance.tariffprofile', 'XEN tariffs'),
88
            'ovds' => Yii::t('hipanel.finance.tariffprofile', 'Open-VZ tariffs'),
89
            'server' => Yii::t('hipanel.finance.tariffprofile', 'Server tariffs'),
90
        ]);
91
    }
92
93
    /**
94
     * Check is model is default profile
95
     *
96
     * @return bool
97
     */
98
    public function isDefault() : bool
99
    {
100
        return (bool) ((string) $this->id === (string) $this->client_id);
0 ignored issues
show
Documentation introduced by Yurii Myronchuk
The property id does not exist on object<hipanel\modules\f...e\models\TariffProfile>. 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...
Documentation introduced by Yurii Myronchuk
The property client_id does not exist on object<hipanel\modules\f...e\models\TariffProfile>. 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...
101
    }
102
103
    /**
104
     * Human readable title
105
     *
106
     * @return string
107
     */
108
    public function getTitle(): string
109
    {
110
        return $this->isDefault() ? Yii::t('hipanel.finance.tariffprofile', 'Default') : $this->name;
0 ignored issues
show
Documentation introduced by Andrey Klochok
The property name does not exist on object<hipanel\modules\f...e\models\TariffProfile>. 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