Completed
Push — master ( f708f1...55490a )
by Dmitry
10s
created

TariffProfile   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 3
dl 0
loc 75
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A tableName() 0 4 1
A rules() 0 33 3
A beforeValidate() 0 11 3
A attributeLabels() 0 12 1
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\models\Ref;
14
use Yii;
15
16
/**
17
 * Class Tariff.
18
 * @property resource[]|DomainResource[]|ServerResource[] $resources
19
 */
20
class TariffProfile extends \hipanel\base\Model
21
{
22
    use \hipanel\base\ModelTrait;
23
24
    public static function tableName()
25
    {
26
        return 'tariffprofile';
27
    }
28
29
    /**
30
     * {@inheritdoc}
31
     */
32
    public function rules()
33
    {
34
        $main = [
35
            [['name'], 'safe'],
36
            [
37
                ['name'],
38
                'required',
39
                'on' => ['update', 'create'],
40
                'when' => function ($model) {
41
                    return (int) $model->id !== (int) $model->client_id;
42
                },
43
            ],
44
            [['id'], 'integer'],
45
            [['id'], 'required', 'on' => ['update', 'delete']],
46
            [['tariff_names'], 'safe'],
47
            [['seller_id', 'client_id'], 'integer'],
48
            [['seller', 'client'], 'safe'],
49
            [['tariffs'], 'safe'],
50
        ];
51
52
        $tariffTypes = [Tariff::TYPE_DOMAIN, Tariff::TYPE_CERT, Tariff::TYPE_XEN, Tariff::TYPE_OPENVZ, Tariff::TYPE_SERVER];
53
        foreach ($tariffTypes as $type) {
54
            if (in_array($type, [Tariff::TYPE_DOMAIN, Tariff::TYPE_CERT], true)) {
55
                $main[] = [[$type], 'integer'];
56
            } else {
57
                $main[] = [[$type], 'filter', 'filter' => function($value) { return explode(",", $value); }];
58
                $main[] = [[$type], 'each', 'rule' => ['trim'], 'on' => ['update', 'create']];
59
                $main[] = [[$type], 'each', 'rule' => ['integer'], 'on' => ['update', 'create']];
60
            }
61
        }
62
63
        return $main;
64
    }
65
66
    public function beforeValidate()
67
    {
68
        foreach ([Tariff::TYPE_XEN, Tariff::TYPE_OPENVZ, Tariff::TYPE_SERVER] as $attribute) {
69
            if (empty($this->$attribute)) {
70
                continue ;
71
            }
72
            $this->$attribute = reset($this->$attribute);
73
        }
74
75
        return parent::beforeValidate();
76
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81
    public function attributeLabels()
82
    {
83
        return $this->mergeAttributeLabels([
84
            'name' => Yii::t('hipanel.finance.tariffprofile', 'Name'),
85
            'tariff_names' => Yii::t('hipanel.finance.tariffprofile', 'Tariffs'),
86
            'domain' => Yii::t('hipanel.finance.tariffprofile', 'Domain tariff'),
87
            'certificate' => Yii::t('hipanel.finance.tariffprofile', 'Certificate tariff'),
88
            'svds' => Yii::t('hipanel.finance.tariffprofile', 'XEN tariffs'),
89
            'ovds' => Yii::t('hipanel.finance.tariffprofile', 'Open-VZ tariffs'),
90
            'server' => Yii::t('hipanel.finance.tariffprofile', 'Server tariffs'),
91
        ]);
92
    }
93
94
}
95