Completed
Push — master ( b87a46...8351d4 )
by Dmitry
04:00
created

AbstractTariffForm   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 262
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 5
Bugs 0 Features 1
Metric Value
wmc 27
c 5
b 0
f 1
lcom 1
cbo 5
dl 0
loc 262
rs 10
ccs 0
cts 134
cp 0

20 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 8 2
A initTariff() 0 6 1
A ensureTariff() 0 8 2
A ensureScenario() 0 6 2
A setDefaultTariff() 0 10 1
A rules() 0 9 1
A fields() 0 6 1
A attributes() 0 8 1
A getResources() 0 4 1
A setResources() 0 4 1
A getResourceTypes() 0 4 1
A attributeLabels() 0 9 1
A load() 0 4 1
B selectBaseTariff() 0 24 4
A getBaseTariffsList() 0 7 1
A insert() 0 4 1
A update() 0 4 1
A getTariff() 0 4 1
A setTariff() 0 13 2
A getPrimaryKey() 0 4 1
1
<?php
2
3
namespace hipanel\modules\finance\forms;
4
5
use hipanel\modules\finance\models\Tariff;
6
use Yii;
7
use yii\base\InvalidConfigException;
8
use yii\helpers\ArrayHelper;
9
10
abstract class AbstractTariffForm extends \yii\base\Model
11
{
12
    /**
13
     * @var int Tariff ID
14
     */
15
    public $id;
16
17
    /**
18
     * @var string Tariff name
19
     */
20
    public $name;
21
22
    /**
23
     * @var int Parent tariff ID
24
     */
25
    public $parent_id;
26
27
    /**
28
     * @var Tariff[] array of available base tariffs
29
     */
30
    public $baseTariffs;
31
32
    /**
33
     * @var Tariff the selected base tariff
34
     */
35
    public $baseTariff;
36
37
    /**
38
     * @var Tariff
39
     */
40
    protected $tariff;
41
42
    /**
43
     * @var \hipanel\modules\finance\models\Resource[]
44
     */
45
    protected $_resources;
46
47
    /**
48
     * @inheritdoc
49
     */
50
    public function init()
51
    {
52
        if (!isset($this->baseTariffs)) {
53
            throw new InvalidConfigException('Property "baseTariffs" must be filled');
54
        }
55
56
        $this->initTariff();
57
    }
58
59
    /**
60
     * Initializes tariff
61
     * @void
62
     */
63
    protected function initTariff()
64
    {
65
        $this->selectBaseTariff();
66
        $this->ensureTariff();
67
        $this->ensureScenario();
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
95
     */
96
    protected function setDefaultTariff()
97
    {
98
        $this->setTariff($this->baseTariff);
99
100
        // Default tariff's id and name are useless on create
101
        $this->id = null;
102
        $this->name = null;
103
104
        return true;
105
    }
106
107
    /** @inheritdoc */
108
    public function rules()
109
    {
110
        return [
111
            [['name'], 'required', 'on' => ['create', 'update']],
112
            [['parent_id', 'id'], 'integer', 'on' => ['create', 'update']],
113
            [['parent_id'], 'required', 'on' => ['create']],
114
            [['id'], 'required', 'on' => ['update']],
115
        ];
116
    }
117
118
    /** @inheritdoc */
119
    public function fields()
120
    {
121
        return ArrayHelper::merge(array_combine($this->attributes(), $this->attributes()), [
122
            'resources' => '_resources'
123
        ]);
124
    }
125
126
    /** @inheritdoc */
127
    public function attributes()
128
    {
129
        return [
130
            'id',
131
            'parent_id',
132
            'name',
133
        ];
134
    }
135
136
    /**
137
     * @return \hipanel\modules\finance\models\Resource[]
138
     */
139
    public function getResources()
140
    {
141
        return $this->_resources;
142
    }
143
144
    /**
145
     * @param \hipanel\modules\finance\models\Resource[] $resources
146
     * @throws InvalidConfigException when not implemented
147
     */
148
    public function setResources($resources)
149
    {
150
        throw new InvalidConfigException('Method "setResources" must be implemented');
151
    }
152
153
    /**
154
     * @return array
155
     */
156
    public function getResourceTypes()
157
    {
158
        return reset($this->baseTariff->resources)->getTypes();
159
    }
160
161
    /**
162
     * @return array
163
     */
164
    public function attributeLabels()
165
    {
166
        return [
167
            'parent_id' => Yii::t('hipanel/finance/tariff', 'Parent tariff'),
168
            'name' => Yii::t('hipanel/finance/tariff', 'Name'),
169
            'label' => Yii::t('hipanel/finance/tariff', 'Label'),
170
            'note' => Yii::t('hipanel', 'Note'),
171
        ];
172
    }
173
174
    /**
175
     * @param array $data to be loaded
176
     * @return bool
177
     * @throws InvalidConfigException when not implemented
178
     */
179
    public function load($data)
180
    {
181
        throw new InvalidConfigException("Method load must be implemented");
182
    }
183
184
    /**
185
     * Selects one of [[baseTariffs]] and puts it to [[baseTariff]]
186
     *
187
     * @return bool
188
     */
189
    public function selectBaseTariff()
190
    {
191
        if (!isset($this->parent_id)) {
192
            if (isset($this->tariff)) {
193
                $this->parent_id = $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 __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...
194
            } else {
195
                $this->parent_id = ArrayHelper::getValue(reset($this->baseTariffs), 'id');
0 ignored issues
show
Security Bug introduced by
It seems like reset($this->baseTariffs) targeting reset() can also be of type false; however, yii\helpers\BaseArrayHelper::getValue() does only seem to accept array|object, did you maybe forget to handle an error condition?
Loading history...
196
            }
197
        }
198
199
        $filtered = array_filter($this->baseTariffs, function ($model) {
200
            return $model->id == $this->parent_id;
201
        });
202
203
        if (count($filtered) !== 1) {
204
            Yii::error('Found ' . count($filtered) . ' base tariffs. Must be exactly one');
205
            return false;
206
        }
207
208
        $this->baseTariff = reset($filtered);
209
        $this->parent_id = $this->baseTariff->id;
210
211
        return true;
212
    }
213
214
    /**
215
     * Builds key-value array of [[baseTariffs]]
216
     *  - key: tariff id
217
     *  - value: tariff name
218
     *
219
     * @return array
220
     */
221
    public function getBaseTariffsList()
222
    {
223
        return array_combine(
224
            ArrayHelper::getColumn($this->baseTariffs, 'id'),
225
            ArrayHelper::getColumn($this->baseTariffs, 'name')
226
        );
227
    }
228
229
    public function insert($runValidation = true)
0 ignored issues
show
Unused Code introduced by
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...
230
    {
231
        throw new InvalidConfigException("Method insert must be implemented");
232
    }
233
234
    public function update($runValidation = true)
0 ignored issues
show
Unused Code introduced by
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...
235
    {
236
        throw new InvalidConfigException("Method update must be implemented");
237
    }
238
239
    /**
240
     * @return Tariff
241
     */
242
    public function getTariff()
243
    {
244
        return $this->tariff;
245
    }
246
247
    /**
248
     * Sets [[tariff]]
249
     *
250
     * @param Tariff $tariff
251
     * @return bool
252
     */
253
    public function setTariff($tariff)
254
    {
255
        if ($tariff === null) {
256
            return false;
257
        }
258
259
        $this->tariff = $tariff;
260
261
        $this->id = $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 __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...
262
        $this->name = $tariff->name;
0 ignored issues
show
Documentation introduced by
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...
263
264
        return true;
265
    }
266
267
    public function getPrimaryKey()
268
    {
269
        return ['id'];
270
    }
271
}
272