Completed
Push — master ( a45d97...ed433d )
by Dmitry
08:33 queued 05:59
created

AbstractTariffManager::collectParentTariffIds()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 17
ccs 0
cts 13
cp 0
rs 9.4285
cc 3
eloc 10
nc 3
nop 0
crap 12
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-2017, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hipanel\modules\finance\logic;
12
13
use hipanel\modules\finance\forms\AbstractTariffForm;
14
use hipanel\modules\finance\models\Tariff;
15
use Yii;
16
use yii\base\Object;
17
use yii\helpers\ArrayHelper;
18
use yii\web\ForbiddenHttpException;
19
use yii\web\NotFoundHttpException;
20
21
abstract class AbstractTariffManager extends Object
22
{
23
    /**
24
     * @var Tariff[] they array of all available parent tariffs
25
     * @see findParentTariffs()
26
     */
27
    protected $parentTariffs;
28
29
    /**
30
     * @var AbstractTariffForm
31
     */
32
    public $form;
33
34
    /**
35
     * @var array options used to build [[form]]
36
     * @see buildForm()
37
     */
38
    public $formOptions = [];
39
40
    /**
41
     * @var string
42
     */
43
    public $scenario;
44
45
    /**
46
     * @var Tariff The actual tariff
47
     */
48
    protected $tariff;
49
50
    /**
51
     * @var string The type used to find parent tariffs
52
     */
53
    protected $type;
54
55
    public function init()
56
    {
57
        $this->fillParentTariffs();
58
        $this->buildForm();
59
    }
60
61
    /**
62
     * Fills [[form]] property with a proper [[AbstractTariffForm]] object.
63
     */
64
    protected function buildForm()
65
    {
66
        $this->form = Yii::createObject(array_merge([
67
            'scenario' => $this->scenario,
68
            'parentTariffs' => $this->parentTariffs,
69
            'tariff' => $this->tariff,
70
        ], $this->getFormOptions()));
71
    }
72
73
    protected function getFormOptions()
74
    {
75
        return $this->formOptions;
76
    }
77
78
    protected function fillParentTariffs()
79
    {
80
        $ids = $this->collectParentTariffIds();
81
82
        $this->parentTariffs = Tariff::find()
83
            ->where(['id' => $ids])
84
            ->details()
85
            ->all();
86
    }
87
88
    /**
89
     * Collects parent tariff ids. Used in [[fillParentTariffs]]
90
     *
91
     * @return array
92
     * @throws NotFoundHttpException
93
     */
94
    protected function collectParentTariffIds()
95
    {
96
        if (!isset($this->tariff)) {
97
            $availableTariffs = Tariff::find()
98
                ->action('get-available-info')
99
                ->andFilterWhere(['type' => $this->type])
100
                ->all();
101
102
            return ArrayHelper::getColumn($availableTariffs, 'id');
103
        }
104
105
        if (isset($this->tariff->parent_id)) {
106
            return [$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 __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...
107
        }
108
109
        throw new NotFoundHttpException('No available tariffs found');
110
    }
111
112
    public function getType()
113
    {
114
        return $this->type;
115
    }
116
117
    /**
118
     * @param Tariff $tariff
119
     */
120
    public function setTariff($tariff)
121
    {
122
        $this->tariff = $tariff;
123
    }
124
}
125