Tariff::getGeneralType()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 12
ccs 0
cts 11
cp 0
rs 9.8666
cc 4
nc 4
nop 0
crap 20
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\modules\finance\models\query\TariffQuery;
14
use hipanel\modules\finance\models\stubs\ServerResourceStub;
15
use Yii;
16
17
/**
18
 * Class Tariff.
19
 * @property resource[]|DomainResource[]|ServerResource[] $resources
20
 */
21
class Tariff extends \hipanel\base\Model implements CalculableModelInterface
22
{
23
    use \hipanel\base\ModelTrait;
24
25
    const TYPE_DOMAIN = 'domain';
26
    const TYPE_XEN = 'svds';
27
    const TYPE_OPENVZ = 'ovds';
28
    const TYPE_SERVER = 'server';
29
    const TYPE_CERT = 'certificate';
30
31
    /**
32
     * {@inheritdoc}
33
     */
34
    public function rules()
35
    {
36
        return [
37
            [['id'], 'filter', 'filter' => function ($value) { return explode(',', $value); }, 'on' => ['search']],
38
            [['id'], 'each', 'rule' => ['trim', 'integer'], 'on' => ['search']],
39
            [['client_id', 'seller_id', 'id', 'parent_id'], 'integer', 'on' => ['create', 'update']],
40
            [['client', 'seller', 'bill', 'name'], 'safe'],
41
            [['domain', 'server'], 'safe'],
42
            [['tariff', 'tariff_id'], 'safe'],
43
            [['type_id', 'state_id'], 'integer'],
44
            [['type', 'state', 'currency'], 'safe'],
45
            [['used'], 'integer'],
46
            [['note', 'label'], 'safe'],
47
            [['is_personal'], 'boolean'],
48
            [['id'], 'required', 'on' => ['delete', 'set-note']],
49
            [['note'], 'string', 'on' => ['set-note']],
50
        ];
51
    }
52
53
    public function getResources()
54
    {
55
        if ($this->getGeneralType() === 'domain') {
56
            return $this->hasMany(DomainResource::class, ['tariff_id' => 'id'])->inverseOf('tariff');
57
        } elseif ($this->getGeneralType() === 'server') {
58
            return $this->hasMany(ServerResource::class, ['tariff_id' => 'id'])->inverseOf('tariff');
59
        } elseif ($this->getGeneralType() === 'certificate') {
60
            return $this->hasMany(CertificateResource::class, ['tariff_id' => 'id'])->inverseOf('tariff');
61
        }
62
63
        return $this->hasMany(Resource::class, ['tariff_id' => 'id'])->inverseOf('tariff');
64
    }
65
66
    /**
67
     * @param $type
68
     * @param bool $stubWhenNotFound whether to return [[ServerResourceStub]] when
69
     * the tariff does not have a relevant resource
70
     * @return DomainResource|ServerResource|ServerResourceStub|resource
71
     */
72
    public function getResourceByType($type, $stubWhenNotFound = true)
73
    {
74
        foreach ($this->resources as $resource) {
75
            if ($resource->type === $type) {
76
                return $resource;
77
            }
78
        }
79
80
        return $stubWhenNotFound ? $this->getStubResource($type) : null;
81
    }
82
83
    /**
84
     * @param string $type
85
     * @return ServerResourceStub
86
     */
87
    public function getStubResource($type)
88
    {
89
        return new ServerResourceStub([
90
            'tariff' => $this,
91
            'type' => $type,
92
        ]);
93
    }
94
95
    /**
96
     * {@inheritdoc}
97
     */
98
    public function attributeLabels()
99
    {
100
        return $this->mergeAttributeLabels([
101
            'used' => Yii::t('hipanel:finance:tariff', 'Used'),
102
        ]);
103
    }
104
105
    /**
106
     * {@inheritdoc}
107
     * @return TariffQuery
108
     */
109
    public static function find($options = [])
110
    {
111
        return new TariffQuery(get_called_class(), [
112
            'options' => $options,
113
        ]);
114
    }
115
116
    public function getGeneralType()
117
    {
118
        if ($this->type === static::TYPE_DOMAIN) {
0 ignored issues
show
Documentation introduced by
The property type does not exist on object<hipanel\modules\finance\models\Tariff>. 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...
119
            return 'domain';
120
        } elseif ($this->type === static::TYPE_CERT) {
0 ignored issues
show
Documentation introduced by
The property type does not exist on object<hipanel\modules\finance\models\Tariff>. 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...
121
            return 'certificate';
122
        } elseif (in_array($this->type, [static::TYPE_OPENVZ, static::TYPE_XEN, static::TYPE_SERVER], true)) {
0 ignored issues
show
Documentation introduced by
The property type does not exist on object<hipanel\modules\finance\models\Tariff>. 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
            return 'server';
124
        }
125
126
        return null;
127
    }
128
129
    /**
130
     * Method creates and returns corresponding Calculation model.
131
     *
132
     * @return Calculation
133
     */
134
    public function getCalculationModel()
135
    {
136
        return new Calculation([
137
            'calculation_id' => $this->id,
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<hipanel\modules\finance\models\Tariff>. 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...
138
            'tariff_id' => $this->id,
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<hipanel\modules\finance\models\Tariff>. 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...
139
            'object' => $this->getGeneralType(),
140
        ]);
141
    }
142
}
143