Completed
Push — master ( a82fec...cf3991 )
by Dmitry
09:24
created

AbstractTariffManager::initParentTariff()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 9

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 14
ccs 0
cts 8
cp 0
rs 9.4285
cc 2
eloc 9
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-2017, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hipanel\modules\finance\logic;
12
13
use hipanel\modules\finance\forms\AbstractTariffForm;
14
use hipanel\modules\finance\models\Tariff;
15
use Yii;
16
use yii\base\InvalidConfigException;
17
use yii\base\Object;
18
use yii\helpers\ArrayHelper;
19
use yii\web\ForbiddenHttpException;
20
use yii\web\NotFoundHttpException;
21
22
abstract class AbstractTariffManager extends Object
23
{
24
    /**
25
     * @var int Parent tariff ID
26
     */
27
    public $parent_id;
28
29
    /**
30
     * @var AbstractTariffForm
31
     */
32
    public $form;
33
34
    /**
35
     * @var array options used to build [[form]]
36
     * @see buildForm()
37
     */
38
    public $formOptions = [];
39
40
    /**
41
     * @var string
42
     */
43
    public $scenario;
44
45
    /**
46
     * @var Tariff The actual tariff
47
     */
48
    protected $tariff;
49
50
    /**
51
     * @var Tariff Parent tariff
52
     */
53
    protected $parentTariff;
54
55
    /**
56
     * @var string The type used to find parent tariffs
57
     */
58
    protected $type;
59
60
    public function init()
61
    {
62
        if (!isset($this->type)) {
63
            throw new InvalidConfigException('Property "type" must be set');
64
        }
65
66
        $this->initParentTariff();
67
        $this->buildForm();
68
    }
69
70
    /**
71
     * Fills [[form]] property with a proper [[AbstractTariffForm]] object.
72
     */
73
    protected function buildForm()
74
    {
75
        $this->form = Yii::createObject(array_merge([
76
            'scenario' => $this->scenario,
77
            'parent_id' => $this->parent_id,
78
            'parentTariff' => $this->parentTariff,
79
            'tariff' => $this->tariff,
80
        ], $this->getFormOptions()));
81
    }
82
83
    protected function getFormOptions()
84
    {
85
        return $this->formOptions;
86
    }
87
88
    protected function initParentTariff()
89
    {
90
        $id = $this->determineParentTariff();
91
92
        if ($id === null) {
93
            return;
94
        }
95
96
        $this->parent_id = $id;
97
        $this->parentTariff = Tariff::find()
0 ignored issues
show
Documentation Bug introduced by
It seems like \hipanel\modules\finance...$id))->details()->one() can also be of type object<hiqdev\hiart\ActiveRecord> or array. However, the property $parentTariff is declared as type object<hipanel\modules\finance\models\Tariff>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
98
            ->where(['id' => $id])
99
            ->details()
100
            ->one();
101
    }
102
103
    /**
104
     * Finds parent tariff ID
105
     *
106
     * @return int
107
     */
108
    protected function determineParentTariff()
109
    {
110
        if (!isset($this->tariff)) {
111
            if (!empty($this->parent_id)) {
112
                return $this->parent_id;
113
            }
114
115
            return null;
116
        }
117
118
        if (isset($this->tariff->parent_id)) {
119
            return $this->tariff->parent_id;
0 ignored issues
show
Documentation introduced by
The property parent_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...
120
        }
121
122
        return $this->tariff->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...
123
    }
124
125
    public function getType()
126
    {
127
        return $this->type;
128
    }
129
130
    /**
131
     * @param Tariff $tariff
132
     */
133
    public function setTariff($tariff)
134
    {
135
        $this->tariff = $tariff;
136
    }
137
}
138