Completed
Push — master ( 77b1bb...7f3712 )
by Dmitry
04:57
created

AbstractTariffManager::calculator()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 0
cts 3
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 0
crap 6
1
<?php
2
3
namespace hipanel\modules\finance\logic;
4
5
use hipanel\modules\finance\forms\AbstractTariffForm;
6
use hipanel\modules\finance\models\Tariff;
7
use Yii;
8
use yii\base\InvalidConfigException;
9
use yii\base\Object;
10
use yii\helpers\ArrayHelper;
11
use yii\web\ForbiddenHttpException;
12
13
abstract class AbstractTariffManager extends Object
14
{
15
    /**
16
     * @var Tariff[] they array of all available base tariffs
17
     * @see findBaseTariffs()
18
     */
19
    protected $baseTariffs;
20
21
    /**
22
     * @var AbstractTariffForm
23
     */
24
    public $form;
25
26
    /**
27
     * @var array options used to build [[form]]
28
     * @see buildForm()
29
     */
30
    public $formOptions = [];
31
32
    /**
33
     * @var string
34
     */
35
    public $scenario;
36
37
    /**
38
     * @var Tariff The actual tariff
39
     */
40
    protected $tariff;
41
42
    /**
43
     * @var string The type used to find base tariff
44
     */
45
    protected $type;
46
    
47
    public function init()
48
    {
49
        $this->findBaseTariffs();
50
        $this->buildForm();
51
    }
52
53
    /**
54
     * Fills [[form]] property with a proper [[AbstractTariffForm]] object
55
     */
56
    protected function buildForm()
57
    {
58
        $this->form = Yii::createObject(array_merge([
59
            'scenario' => $this->scenario,
60
            'baseTariffs' => $this->baseTariffs,
61
            'tariff' => $this->tariff
62
        ], $this->getFormOptions()));
63
    }
64
65
    protected function getFormOptions()
66
    {
67
        return $this->formOptions;
68
    }
69
70
    protected function findBaseTariffs()
71
    {
72
        $availableTariffs = Tariff::find(['scenario' => 'get-available-info'])
73
            ->andFilterWhere(['type' => $this->type])
74
            ->all();
75
76
        if (empty($availableTariffs)) {
77
            throw new ForbiddenHttpException('No available tariffs found');
78
        }
79
80
        $this->baseTariffs = Tariff::find()
81
            ->where(['id' => ArrayHelper::getColumn($availableTariffs, 'id')])
82
            ->details()
83
            ->all();
84
    }
85
86
    public function getType()
87
    {
88
        return $this->type;
89
    }
90
91
    /**
92
     * @param Tariff $tariff
93
     */
94
    public function setTariff($tariff)
95
    {
96
        $this->tariff = $tariff;
97
    }
98
}
99