Completed
Push — master ( 0622f1...018a2f )
by Dmitry
04:34
created

AbstractTariffManager::getType()   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
c 1
b 0
f 0
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
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[] array of all available base tariffs
17
     */
18
    protected $baseTariffs;
19
20
    /**
21
     * @var AbstractTariffForm
22
     */
23
    public $form;
24
25
    /**
26
     * @var array options used to build [[form]]
27
     * @see buildForm()
28
     */
29
    public $formOptions = [];
30
31
    /**
32
     * @var string
33
     */
34
    public $scenario;
35
36
    /**protected
37
     * @var Tariff The actual tariff
38
     */
39
    protected $tariff;
40
41
    /**
42
     * @var string The type used to find base tariff
43
     */
44
    protected $type;
45
    
46
    public function init()
47
    {
48
        $this->findBaseTariffs();
49
        $this->buildForm();
50
    }
51
52
    /**
53
     * Fills [[form]] property with a proper [[AbstractTariffForm]] object
54
     */
55
    protected function buildForm()
56
    {
57
        $this->form = Yii::createObject(array_merge([
58
            'scenario' => $this->scenario,
59
            'baseTariffs' => $this->baseTariffs,
60
            'tariff' => $this->tariff
61
        ], $this->getFormOptions()));
62
    }
63
64
    protected function getFormOptions()
65
    {
66
        return $this->formOptions;
67
    }
68
69
    protected function findBaseTariffs()
70
    {
71
        $availableTariffs = Tariff::perform('GetAvailableInfo', ['type' => $this->type], true);
72
73
        if (empty($availableTariffs)) {
74
            throw new ForbiddenHttpException('No available tariffs found');
75
        }
76
77
        $this->baseTariffs = Tariff::find()
78
            ->where(['id' => array_keys($availableTariffs)])
79
            ->details()
80
            ->all();
81
    }
82
83
    public function getType()
84
    {
85
        return $this->type;
86
    }
87
88
    /**
89
     * @param Tariff $tariff
90
     */
91
    public function setTariff($tariff)
92
    {
93
        $this->tariff = $tariff;
94
    }
95
}
96