Completed
Push — master ( 150de4...ecb3c3 )
by Dmitry
05:20
created

AbstractTariffManager::findParentTariffs()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 10

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 15
ccs 0
cts 12
cp 0
rs 9.4285
cc 2
eloc 10
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\Object;
9
use yii\helpers\ArrayHelper;
10
use yii\web\ForbiddenHttpException;
11
12
abstract class AbstractTariffManager extends Object
13
{
14
    /**
15
     * @var Tariff[] they array of all available parent tariffs
16
     * @see findParentTariffs()
17
     */
18
    protected $parentTariffs;
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
    /**
37
     * @var Tariff The actual tariff
38
     */
39
    protected $tariff;
40
41
    /**
42
     * @var string The type used to find parent tariffs
43
     */
44
    protected $type;
45
    
46
    public function init()
47
    {
48
        $this->findParentTariffs();
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
            'parentTariffs' => $this->parentTariffs,
60
            'tariff' => $this->tariff
61
        ], $this->getFormOptions()));
62
    }
63
64
    protected function getFormOptions()
65
    {
66
        return $this->formOptions;
67
    }
68
69
    protected function findParentTariffs()
70
    {
71
        $availableTariffs = Tariff::find(['scenario' => 'get-available-info'])
72
            ->andFilterWhere(['type' => $this->type])
73
            ->all();
74
75
        if (empty($availableTariffs)) {
76
            throw new ForbiddenHttpException('No available tariffs found');
77
        }
78
79
        $this->parentTariffs = Tariff::find()
80
            ->where(['id' => ArrayHelper::getColumn($availableTariffs, 'id')])
81
            ->details()
82
            ->all();
83
    }
84
85
    public function getType()
86
    {
87
        return $this->type;
88
    }
89
90
    /**
91
     * @param Tariff $tariff
92
     */
93
    public function setTariff($tariff)
94
    {
95
        $this->tariff = $tariff;
96
    }
97
}
98