Completed
Push — master ( 0622f1...018a2f )
by Dmitry
04:34
created

Resource::rules()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 4
Bugs 1 Features 1
Metric Value
c 4
b 1
f 1
dl 0
loc 13
ccs 0
cts 12
cp 0
rs 9.4285
cc 1
eloc 9
nc 1
nop 0
crap 2
1
<?php
2
3
/*
4
 * Finance module for HiPanel
5
 *
6
 * @link      https://github.com/hiqdev/hipanel-module-finance
7
 * @package   hipanel-module-finance
8
 * @license   BSD-3-Clause
9
 * @copyright Copyright (c) 2015-2016, HiQDev (http://hiqdev.com/)
10
 */
11
12
namespace hipanel\modules\finance\models;
13
14
use hipanel\modules\finance\models\decorators\AbstractResourceDecorator;
15
use hipanel\modules\stock\models\Part;
16
use Yii;
17
use yii\base\InvalidConfigException;
18
19
class Resource extends \hipanel\base\Model
20
{
21
    use \hipanel\base\ModelTrait;
22
23
    /** @var  AbstractResourceDecorator */
24
    protected $decorator;
25
26
    /** @inheritdoc */
27
    public static $i18nDictionary = 'hipanel/finance/tariff';
28
29
    /**
30
     * {@inheritdoc}
31
     */
32
    public function rules()
33
    {
34
        return [
35
            [['id', 'tariff_id', 'object_id', 'type_id', 'unit_id', 'currency_id', 'hardlim'], 'integer'],
36
            [['type', 'ftype', 'unit', 'unit_factor', 'currency'], 'safe'],
37
            [['price', 'fee', 'quantity', 'discount'], 'number'],
38
39
            [['object_id', 'type_id'], 'integer', 'on' => ['create', 'update']],
40
            [['type'], 'safe', 'on' => ['create', 'update']],
41
            [['price'], 'number', 'on' => ['create', 'update']],
42
            'create-required' => [['object_id', 'price'], 'required', 'on' => ['create', 'update']],
43
        ];
44
    }
45
46
    public function getTariff()
47
    {
48
        return $this->hasOne(Tariff::class, ['tariff_id' => 'id']);
49
    }
50
51
    public function getPart()
52
    {
53
        if (!Yii::getAlias('@part', false)) {
54
            throw new InvalidConfigException('Stock module is a must to retrieve resource parts');
55
        }
56
57
        return $this->hasOne(Part::class, ['object_id' => 'id']);
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63
    public function attributeLabels()
64
    {
65
        return $this->mergeAttributeLabels([
66
            'price' => Yii::t('hipanel/finance/tariff', 'Price per period')
67
        ]);
68
    }
69
70
    public function isTypeCorrect()
71
    {
72
        return isset($this->getTypes()[$this->type]);
0 ignored issues
show
Documentation introduced by
The property type does not exist on object<hipanel\modules\finance\models\Resource>. 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...
73
    }
74
75
    public function getTypes()
76
    {
77
        return [];
78
    }
79
80
    public function decorator()
81
    {
82
        throw new InvalidConfigException('Method "decorator" is not available for class Resource');
83
    }
84
}
85