Completed
Push — master ( 5a5e4b...77b1bb )
by Dmitry
06:22
created

Calculation::primaryKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
c 1
b 0
f 0
cc 1
eloc 2
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 Guzzle\Plugin\ErrorResponse\Exception\ErrorResponseException;
15
use Yii;
16
use yii\base\InvalidParamException;
17
18
/**
19
 * Class Calculation.
20
 */
21
class Calculation extends \hipanel\base\Model
22
{
23
    use \hipanel\base\ModelTrait;
24
25
    /** {@inheritdoc} */
26
    public static function index()
27
    {
28
        return 'actions';
29
    }
30
31
    /** {@inheritdoc} */
32
    public static function type()
33
    {
34
        return 'action';
35
    }
36
37
    /** {@inheritdoc} */
38
    public static function primaryKey()
39
    {
40
        return ['tariff_id'];
41
    }
42
43
    /** {@inheritdoc} */
44
    public function init()
45
    {
46
        if (Yii::$app->user->isGuest) {
47
            $this->seller = Yii::$app->params['seller'];
0 ignored issues
show
Documentation introduced by
The property seller does not exist on object<hipanel\modules\f...nce\models\Calculation>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write 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.");
        }
    }

}

Since the property has write access only, you can use the @property-write 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...
48
        } else {
49
            $this->client = Yii::$app->user->identity->username;
0 ignored issues
show
Documentation introduced by
The property client does not exist on object<hipanel\modules\f...nce\models\Calculation>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write 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.");
        }
    }

}

Since the property has write access only, you can use the @property-write 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...
50
        }
51
52
        $this->synchronize();
53
    }
54
55
    public function getValue()
56
    {
57
        return $this->hasMany(Value::class, ['tariff_id' => 'currency'])->indexBy('currency');
58
    }
59
60
    public function forCurrency($currency)
61
    {
62
        if (!isset($this->value[$currency])) {
0 ignored issues
show
Documentation introduced by
The property value does not exist on object<hipanel\modules\f...nce\models\Calculation>. 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...
63
            throw new InvalidParamException("Calculation for currency \"$currency\" does not exist");
64
        }
65
66
        return $this->value[$currency];
0 ignored issues
show
Documentation introduced by
The property value does not exist on object<hipanel\modules\f...nce\models\Calculation>. 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...
67
    }
68
69
    /** {@inheritdoc} */
70 View Code Duplication
    public function rules()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
71
    {
72
        return [
73
            [['tariff_id', 'object', 'seller', 'client', 'type', 'currency', 'item'], 'safe'],
74
            [['amount'], 'number'],
75
        ];
76
    }
77
78
    /**
79
     * Synchronises the model to represent actual state of [[position]]
80
     * The method must update values, that affects the calculation and
81
     * can be changed in cart without position re-adding.
82
     * For example: quantity.
83
     */
84
    public function synchronize()
85
    {
86
        return true;
87
    }
88
}
89