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