Plan   A
last analyzed

Complexity

Total Complexity 20

Size/Duplication

Total Lines 173
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 7

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 20
lcom 2
cbo 7
dl 0
loc 173
ccs 0
cts 67
cp 0
rs 10
c 0
b 0
f 0

14 Methods

Rating   Name   Duplication   Size   Complexity  
A rules() 0 14 1
A attributeLabels() 0 11 1
A getPlanAttributes() 0 13 4
A getPrices() 0 8 2
A getPriceHistory() 0 4 1
A getDesiredPriceClass() 0 8 2
A getSales() 0 4 1
A getTypeOptions() 0 4 1
A getStateOptions() 0 4 1
A isDeleted() 0 4 1
A supportsIndividualPricesCreation() 0 8 1
A supportsSharedPricesCreation() 0 8 1
A find() 0 6 1
A isKnownType() 0 4 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-2019, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hipanel\modules\finance\models;
12
13
use hipanel\base\Model;
14
use hipanel\base\ModelTrait;
15
use hipanel\models\Ref;
16
use hipanel\modules\finance\models\query\PlanQuery;
17
use Yii;
18
19
/**
20
 * Class Plan.
21
 *
22
 * @property string $id
23
 * @property string $name
24
 * @property string $type
25
 * @property string $currency
26
 * @property string $state
27
 * @property int $currency_id
28
 * @property bool $is_grouping
29
 * @property bool $your_tariff
30
 *
31
 * @property PriceHistory[] $priceHistory
32
 * @property Sale[] $sales
33
 * @property Price[]|CertificatePrice[] $prices
34
 * @property-read string[] typeOptions
35
 *
36
 * @author Dmytro Naumenko <[email protected]>
37
 */
38
class Plan extends Model
39
{
40
    public const TYPE_SERVER = 'server';
41
    public const TYPE_PCDN = 'pcdn';
42
    public const TYPE_VCDN = 'vcdn';
43
    public const TYPE_TEMPLATE = 'template';
44
    public const TYPE_CERTIFICATE = 'certificate';
45
    public const TYPE_DOMAIN = 'domain';
46
    public const TYPE_SWITCH = 'switch';
47
    public const TYPE_AVDS = 'avds';
48
    public const TYPE_OVDS = 'ovds';
49
    public const TYPE_SVDS = 'svds';
50
    public const TYPE_CLIENT = 'client';
51
    public const TYPE_HARDWARE = 'hardware';
52
    public const TYPE_ANYCASTCDN = 'anycastcdn';
53
    public const TYPE_REFERRAL = 'referral';
54
    public const TYPE_VPS = 'vps';
55
    public const TYPE_SNAPSHOT = 'snapshot';
56
    public const TYPE_VOLUME = 'volume';
57
    public const TYPE_STORAGE = 'storage';
58
    public const TYPE_PRIVATE_CLOUD_BACKUP = 'private_cloud_backup';
59
    public const TYPE_PRIVATE_CLOUD = 'private_cloud';
60
61
    protected $knownTypes = [
62
        self::TYPE_SERVER               => self::TYPE_SERVER,
63
        self::TYPE_PCDN                 => self::TYPE_PCDN,
64
        self::TYPE_VCDN                 => self::TYPE_VCDN,
65
        self::TYPE_TEMPLATE             => self::TYPE_TEMPLATE,
66
        self::TYPE_CERTIFICATE          => self::TYPE_CERTIFICATE,
67
        self::TYPE_DOMAIN               => self::TYPE_DOMAIN,
68
        self::TYPE_SWITCH               => self::TYPE_SWITCH,
69
        self::TYPE_AVDS                 => self::TYPE_AVDS,
70
        self::TYPE_OVDS                 => self::TYPE_OVDS,
71
        self::TYPE_SVDS                 => self::TYPE_SVDS,
72
        self::TYPE_CLIENT               => self::TYPE_CLIENT,
73
        self::TYPE_HARDWARE             => self::TYPE_HARDWARE,
74
        self::TYPE_ANYCASTCDN           => self::TYPE_ANYCASTCDN,
75
        self::TYPE_VPS                  => self::TYPE_VPS,
76
        self::TYPE_SNAPSHOT             => self::TYPE_SNAPSHOT,
77
        self::TYPE_VOLUME               => self::TYPE_VOLUME,
78
        self::TYPE_STORAGE              => self::TYPE_STORAGE,
79
        self::TYPE_PRIVATE_CLOUD_BACKUP => self::TYPE_PRIVATE_CLOUD_BACKUP,
80
        self::TYPE_PRIVATE_CLOUD        => self::TYPE_PRIVATE_CLOUD,
81
        self::TYPE_REFERRAL             => self::TYPE_REFERRAL,
82
    ];
83
84
    use ModelTrait;
85
86
    /**
87
     * @var string
88
     */
89
    public $monthly;
90
91
    public $servers = [];
92
93
    public function rules()
94
    {
95
        return array_merge(parent::rules(), [
96
            [['id', 'type_id', 'state_id', 'client_id', 'currency_id'], 'integer'],
97
            [['type', 'state', 'client', 'name', 'plan', 'note', 'currency', 'is_grouping'], 'string'],
98
99
            [['type', 'name', 'currency'], 'required', 'on' => ['create', 'update']],
100
            [['id'], 'required', 'on' => ['update', 'delete', 'set-note']],
101
            [['id'], 'required', 'on' => ['delete', 'restore']],
102
            [['id', 'server_ids'], 'safe', 'on' => ['copy']],
103
            [['your_tariff'], 'boolean'],
104
            [['custom_data', 'data'], 'safe', 'on' => ['create', 'update']],
105
        ]);
106
    }
107
108
    public function attributeLabels()
109
    {
110
        return array_merge(parent::attributeLabels(), [
111
            'client' => Yii::t('hipanel', 'Seller'),
112
            'name' => Yii::t('hipanel:finance', 'Name'),
113
            'server_ids' => Yii::t('hipanel.finance.plan', 'Servers'),
114
            'monthly' => Yii::t('hipanel.finance.plan', 'Monthly'),
115
            'is_grouping' => Yii::t('hipanel.finance.plan', 'Grouping'),
116
            'currency' => Yii::t('hipanel:finance', 'Currency'),
117
        ]);
118
    }
119
120
    public function getPlanAttributes(): array
121
    {
122
        $attributes = $this->custom_data['attributes'] ?? [];
0 ignored issues
show
Documentation introduced by
The property custom_data does not exist on object<hipanel\modules\finance\models\Plan>. 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...
123
        $models = [];
124
        foreach ($attributes as $name => $value) {
125
            $model = new PlanAttribute(compact('name', 'value'));
126
            if (!$model->isEmpty()) {
127
                $models[] = $model;
128
            }
129
        }
130
131
        return empty($models) ? [] : $models;
132
    }
133
134
    public function getPrices()
135
    {
136
        if ($this->type === Plan::TYPE_CERTIFICATE) {
137
            return $this->hasMany(CertificatePrice::class, ['plan_id' => 'id'])->inverseOf('plan');
138
        }
139
140
        return $this->hasMany(Price::class, ['plan_id' => 'id'])->indexBy('id')->inverseOf('plan');
141
    }
142
143
    public function getPriceHistory()
144
    {
145
        return $this->hasMany(PriceHistory::class, ['tariff_id' => 'id']);
146
    }
147
148
    public function getDesiredPriceClass()
149
    {
150
        if ($this->type === Plan::TYPE_CERTIFICATE) {
151
            return CertificatePrice::class;
152
        }
153
154
        return Price::class;
155
    }
156
157
    public function getSales()
158
    {
159
        return $this->hasMany(Sale::class, ['tariff_id' => 'id']);
160
    }
161
162
    public function getTypeOptions()
163
    {
164
        return Ref::getList('type,tariff');
165
    }
166
167
    public function getStateOptions()
168
    {
169
        return Ref::getList('state,tariff');
170
    }
171
172
    public function isDeleted(): bool
173
    {
174
        return $this->state === 'deleted';
175
    }
176
177
    public function supportsIndividualPricesCreation(): bool
178
    {
179
        return ! in_array($this->type, [
180
            // Types listed here does not support individual prices creation
181
182
183
        ], true);
184
    }
185
186
    public function supportsSharedPricesCreation(): bool
187
    {
188
        return ! in_array($this->type, [
189
            self::TYPE_TEMPLATE,
190
            self::TYPE_CERTIFICATE,
191
            self::TYPE_DOMAIN,
192
        ], true);
193
    }
194
195
    /**
196
     * {@inheritdoc}
197
     * @return PlanQuery
198
     */
199
    public static function find($options = [])
200
    {
201
        return new PlanQuery(get_called_class(), [
202
            'options' => $options,
203
        ]);
204
    }
205
206
    public function isKnownType(string $type = null): bool
207
    {
208
        return isset($this->knownTypes[$type ?: $this->type]);
209
    }
210
}
211