Completed
Push — master ( 4b3a6a...ccebfa )
by Dmitry
04:39
created

Resource::getPart()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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