Completed
Push — master ( c6b5de...db50e7 )
by Dmitry
04:45
created

src/forms/AbstractTariffForm.php (8 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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\forms;
12
13
use hipanel\modules\finance\logic\Calculator;
14
use hipanel\modules\finance\models\Tariff;
15
use Yii;
16
use yii\base\InvalidConfigException;
17
use yii\helpers\ArrayHelper;
18
19
abstract class AbstractTariffForm extends \yii\base\Model
20
{
21
    /**
22
     * @var int Tariff ID
23
     */
24
    public $id;
25
26
    /**
27
     * @var string Tariff name
28
     */
29
    public $name;
30
31
    /**
32
     * @var int Parent tariff ID
33
     */
34
    public $parent_id;
35
36
    /**
37
     * @var Tariff the selected parent tariff
38
     */
39
    public $parentTariff;
40
41
    /**
42
     * @var Tariff
43
     */
44
    protected $tariff;
45
46
    /**
47
     * @var \hipanel\modules\finance\models\Resource[]
48
     */
49
    protected $_resources;
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    public function init()
55
    {
56
        $this->initTariff();
57
    }
58
59
    /**
60
     * Initializes tariff.
61
     * @void
62
     */
63
    protected function initTariff()
64
    {
65
        if ($this->ensureTariff()) {
66
            $this->ensureScenario();
67
        }
68
    }
69
70
    /**
71
     * Ensures that [[tariff]] is set.
72
     * Otherwise calls [[setDefaultTariff()]].
73
     * @return bool
74
     */
75
    protected function ensureTariff()
76
    {
77
        if ($this->getTariff() instanceof Tariff) {
78
            return true;
79
        }
80
81
        return $this->setDefaultTariff();
82
    }
83
84
    protected function ensureScenario()
85
    {
86
        foreach ($this->tariff->resources as $resource) {
87
            $resource->scenario = $this->scenario;
88
        }
89
    }
90
91
    /**
92
     * Sets default tariff.
93
     *
94
     * @return bool success
95
     */
96
    protected function setDefaultTariff()
97
    {
98
        if (!$this->setTariff($this->parentTariff)) {
99
            return false;
100
        }
101
102
        // Default tariff's id and name are useless on create
103
        $this->id = null;
104
        $this->name = null;
105
106
        return true;
107
    }
108
109
    /** {@inheritdoc} */
110
    public function rules()
111
    {
112
        return [
113
            [['name'], 'required', 'on' => ['create', 'update']],
114
            [['parent_id', 'id'], 'integer', 'on' => ['create', 'update']],
115
            'parent-id-required' => [['parent_id'], 'required', 'on' => ['create']],
116
            [['id'], 'required', 'on' => ['update']],
117
        ];
118
    }
119
120
    /** {@inheritdoc} */
121
    public function fields()
122
    {
123
        return ArrayHelper::merge(array_combine($this->attributes(), $this->attributes()), [
124
            'resources' => '_resources',
125
        ]);
126
    }
127
128
    /** {@inheritdoc} */
129
    public function attributes()
130
    {
131
        return [
132
            'id',
133
            'parent_id',
134
            'name',
135
        ];
136
    }
137
138
    /**
139
     * @return \hipanel\modules\finance\models\Resource[]
140
     */
141
    public function getResources()
142
    {
143
        return $this->_resources;
144
    }
145
146
    /**
147
     * @param \hipanel\modules\finance\models\Resource[] $resources
148
     * @throws InvalidConfigException when not implemented
149
     */
150
    public function setResources($resources)
151
    {
152
        throw new InvalidConfigException('Method "setResources" must be implemented');
153
    }
154
155
    /**
156
     * @return array
157
     */
158
    public function getResourceTypes(): array
159
    {
160
        $res = $this->parentTariff->resources;
161
162
        return $res ? reset($res)->getTypes() : [];
163
    }
164
165
    /**
166
     * @return array
167
     */
168
    public function attributeLabels()
169
    {
170
        return [
171
            'parent_id' => Yii::t('hipanel:finance:tariff', 'Parent tariff'),
172
            'name' => Yii::t('hipanel:finance:tariff', 'Name'),
173
            'label' => Yii::t('hipanel:finance:tariff', 'Label'),
174
            'note' => Yii::t('hipanel', 'Note'),
175
        ];
176
    }
177
178
    /**
179
     * @param array $data to be loaded
180
     * @param null $formName
181
     * @throws InvalidConfigException when not implemented
182
     * @return bool
183
     */
184
    public function load($data, $formName = null)
185
    {
186
        throw new InvalidConfigException('Method load must be implemented');
187
    }
188
189
    public function insert($runValidation = true)
0 ignored issues
show
The parameter $runValidation is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
190
    {
191
        throw new InvalidConfigException('Method insert must be implemented');
192
    }
193
194
    public function update($runValidation = true)
0 ignored issues
show
The parameter $runValidation is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
195
    {
196
        throw new InvalidConfigException('Method update must be implemented');
197
    }
198
199
    /**
200
     * @return Tariff
201
     */
202
    public function getTariff()
203
    {
204
        return $this->tariff;
205
    }
206
207
    /**
208
     * Sets [[tariff]].
209
     *
210
     * @param Tariff $tariff
211
     * @return bool
212
     */
213
    public function setTariff($tariff)
214
    {
215
        if ($tariff === null) {
216
            return false;
217
        }
218
219
        $this->tariff = $tariff;
220
221
        $this->id = $tariff->id;
0 ignored issues
show
The property id does not exist on object<hipanel\modules\finance\models\Tariff>. 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...
222
        $this->name = $tariff->name;
0 ignored issues
show
The property name does not exist on object<hipanel\modules\finance\models\Tariff>. 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...
223
224
        return true;
225
    }
226
227
    public function getPrimaryKey()
228
    {
229
        return ['id'];
230
    }
231
232
    /**
233
     * @var Calculator
234
     */
235
    protected $_calculator;
236
237
    /**
238
     * Creates [[TariffCalculator]] object for the [[tariff]].
239
     *
240
     * @return Calculator
241
     */
242
    protected function calculator()
243
    {
244
        if (!isset($this->_calculator)) {
245
            $this->_calculator = new Calculator([$this->tariff]);
246
        }
247
248
        return $this->_calculator;
249
    }
250
251
    /**
252
     * @return \hipanel\modules\finance\models\Value
253
     */
254
    public function calculation()
255
    {
256
        return $this->calculator()->getCalculation($this->tariff->id)->forCurrency($this->tariff->currency);
0 ignored issues
show
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...
The property currency 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...
257
    }
258
259
    /**
260
     * @var Calculator
261
     */
262
    protected $_parentCalculator;
263
264
    /**
265
     * Creates [[TariffCalculator]] object for the [[parentTariff]].
266
     *
267
     * @return Calculator
268
     */
269
    protected function parentCalculator()
270
    {
271
        if (!isset($this->_parentCalculator)) {
272
            $this->_parentCalculator = new Calculator([$this->parentTariff]);
273
        }
274
275
        return $this->_parentCalculator;
276
    }
277
278
    /**
279
     * @return \hipanel\modules\finance\models\Value
280
     */
281
    public function parentCalculation()
282
    {
283
        return $this->parentCalculator()->getCalculation($this->parentTariff->id)->forCurrency($this->parentTariff->currency);
0 ignored issues
show
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...
The property currency 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...
284
    }
285
}
286