Completed
Push — master ( b864d9...3fa98b )
by Dmitry
07:42 queued 03:17
created

Plan::find()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
namespace hipanel\modules\finance\models;
4
5
use hipanel\models\Ref;
6
use hipanel\modules\finance\models\query\PlanQuery;
7
use Yii;
8
9
/**
10
 * Class Plan
11
 *
12
 * @property string $id
13
 * @property string $name
14
 * @property string $type
15
 * @property string $currency
16
 * @property int $currency_id
17
 *
18
 * @property Sale[] $sales
19
 * @property Price[]|CertificatePrice[] $prices
20
 * @property-read string[] typeOptions
21
 *
22
 * @author Dmytro Naumenko <[email protected]>
23
 */
24
class Plan extends \hipanel\base\Model
25
{
26
    const TYPE_SERVER = 'server';
27
    const TYPE_PCDN = 'pcdn';
28
    const TYPE_VCDN = 'vcdn';
29
    const TYPE_TEMPLATE = 'template';
30
    const TYPE_CERTIFICATE = 'certificate';
31
32
    use \hipanel\base\ModelTrait;
33
34
    public function rules()
35
    {
36
        return array_merge(parent::rules(), [
37
            [['id', 'type_id', 'state_id', 'client_id', 'currency_id'], 'integer'],
38
            [['type', 'state', 'client', 'name', 'note', 'currency'], 'string'],
39
40
            [['type', 'name', 'currency'], 'required', 'on' => ['create', 'update']],
41
            [['id'], 'required', 'on' => ['update', 'delete', 'set-note']],
42
            [['id'], 'required', 'on' => ['delete', 'restore']],
43
            [['id', 'server_ids'], 'safe', 'on' => ['copy']],
44
        ]);
45
    }
46
47
    public function attributeLabels()
48
    {
49
        return array_merge(parent::attributeLabels(), [
50
            'name' => Yii::t('hipanel:finance', 'Name'),
51
            'server_ids' => Yii::t('hipanel.finance.plan', 'Servers'),
52
        ]);
53
    }
54
55
    public function getPrices()
56
    {
57
        if ($this->type === Plan::TYPE_CERTIFICATE) {
58
            return $this->hasMany(CertificatePrice::class, ['plan_id' => 'id'])->inverseOf('plan');
59
        }
60
        return $this->hasMany(Price::class, ['plan_id' => 'id'])->indexBy('id')->inverseOf('plan');
61
    }
62
63
    public function getDesiredPriceClass()
64
    {
65
        if ($this->type === Plan::TYPE_CERTIFICATE) {
66
            return CertificatePrice::class;
67
        }
68
69
        return Price::class;
70
    }
71
72
    public function getSales()
73
    {
74
        return $this->hasMany(Sale::class, ['tariff_id' => 'id']);
75
    }
76
77
    public function getTypeOptions()
78
    {
79
        return Ref::getList('type,tariff');
80
    }
81
82
    public function getStateOptions()
83
    {
84
        return Ref::getList('state,tariff');
85
    }
86
87
    public function isDeleted(): bool
88
    {
89
        return $this->state === 'deleted';
0 ignored issues
show
Documentation introduced by
The property state 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...
90
    }
91
92
    /**
93
     * {@inheritdoc}
94
     * @return PlanQuery
95
     */
96
    public static function find($options = [])
97
    {
98
        return new PlanQuery(get_called_class(), [
99
            'options' => $options,
100
        ]);
101
    }
102
}
103