Completed
Push — master ( 5701cd...3770db )
by Dmitry
05:00
created

Tariff::getResourceByType()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 10
Code Lines 5

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 10
ccs 0
cts 9
cp 0
rs 9.2
cc 4
eloc 5
nc 5
nop 2
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-2017, 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
29
    /**
30
     * {@inheritdoc}
31
     */
32
    public function rules()
33
    {
34
        return [
35
            [['client_id', 'seller_id', 'id', 'parent_id'], 'integer'],
36
            [['client', 'seller', 'bill', 'name'], 'safe'],
37
            [['domain', 'server'], 'safe'],
38
            [['tariff', 'tariff_id'], 'safe'],
39
            [['type_id', 'state_id'], 'integer'],
40
            [['type', 'state', 'currency'], 'safe'],
41
            [['used'], 'integer'],
42
            [['note', 'label'], 'safe'],
43
            [['is_personal'], 'boolean'],
44
            [['id'], 'required', 'on' => ['delete']],
45
        ];
46
    }
47
48
    public function getResources()
49
    {
50
        if ($this->type === self::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...
51
            return $this->hasMany(DomainResource::class, ['tariff_id' => 'id'])->inverseOf('tariff');
52
        } elseif (in_array($this->type, [self::TYPE_XEN, self::TYPE_OPENVZ], 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...
53
            return $this->hasMany(ServerResource::class, ['tariff_id' => 'id'])->inverseOf('tariff');
54
        }
55
56
        return $this->hasMany(Resource::class, ['tariff_id' => 'id'])->inverseOf('tariff');
57
    }
58
59
    /**
60
     * @param $type
61
     * @param bool $stubWhenNotFound whether to return [[ServerResourceStub]] when
62
     * the tariff does not have a relevant resource
63
     * @return DomainResource|ServerResource|ServerResourceStub|resource
64
     */
65
    public function getResourceByType($type, $stubWhenNotFound = true)
66
    {
67
        foreach ($this->resources as $resource) {
68
            if ($resource->type === $type) {
69
                return $resource;
70
            }
71
        }
72
73
        return $stubWhenNotFound ? $this->getStubResource($type) : null;
74
    }
75
76
    /**
77
     * @param string $type
78
     * @return ServerResourceStub
79
     */
80
    public function getStubResource($type)
81
    {
82
        return new ServerResourceStub([
83
            'tariff' => $this,
84
            'type' => $type
85
        ]);
86
    }
87
88
    /**
89
     * {@inheritdoc}
90
     */
91
    public function attributeLabels()
92
    {
93
        return $this->mergeAttributeLabels([
94
            'used' => Yii::t('hipanel:finance:tariff', 'Used'),
95
        ]);
96
    }
97
98
    /**
99
     * {@inheritdoc}
100
     * @return TariffQuery
101
     */
102
    public static function find($options = [])
103
    {
104
        return new TariffQuery(get_called_class(), [
105
            'options' => $options,
106
        ]);
107
    }
108
109
    public function getGeneralType()
110
    {
111
        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...
112
            return 'domain';
113
        } elseif (in_array($this->type, [static::TYPE_OPENVZ, static::TYPE_XEN], 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...
114
            return 'server';
115
        }
116
117
        return null;
118
    }
119
120
    /**
121
     * Method creates and returns corresponding Calculation model.
122
     *
123
     * @return Calculation
124
     */
125
    public function getCalculationModel()
126
    {
127
        return new Calculation([
128
            '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...
129
            '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...
130
            'object' => $this->getGeneralType(),
131
        ]);
132
    }
133
}
134