TariffManagerFactory::createByType()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 11
ccs 0
cts 10
cp 0
rs 9.9
cc 1
nc 1
nop 3
crap 2
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-2019, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hipanel\modules\finance\logic;
12
13
use hipanel\helpers\ArrayHelper;
14
use hipanel\modules\finance\models\Tariff;
15
use Yii;
16
use yii\web\BadRequestHttpException;
17
use yii\web\NotFoundHttpException;
18
19
class TariffManagerFactory
20
{
21
    /**
22
     * @param integer $id Tariff ID
23
     * @param array $options that will be passed to the object as configuration
24
     * @throws BadRequestHttpException
25
     * @throws NotFoundHttpException
26
     * @return AbstractTariffManager|object
27
     */
28
    public static function createById($id, $options = [])
29
    {
30
        if (empty($id)) {
31
            throw new BadRequestHttpException('ID is missing');
32
        }
33
34
        $model = Tariff::find()->byId($id)->details()->one();
35
36
        if ($model === null) {
37
            throw new NotFoundHttpException('Tariff was not found');
38
        }
39
40
        $model->scenario = ArrayHelper::getValue($options, 'scenario', $model::SCENARIO_DEFAULT);
41
42
        return Yii::createObject(array_merge([
43
            'class' => static::buildClassName($model->type),
44
            'tariff' => $model,
45
        ], $options));
46
    }
47
48
    /**
49
     * @param string $type Tariff type
50
     * @param integer $parent_id the parent tariff id
51
     * @param array $options that will be passed to the object as configuration
52
     * @return AbstractTariffManager|object
53
     */
54
    public static function createByType($type, $parent_id = null, $options = [])
55
    {
56
        $options = array_merge([
57
            'parent_id' => $parent_id,
58
            'formOptions' => [
59
                'scenario' => 'create',
60
            ],
61
        ], $options);
62
63
        return Yii::createObject(array_merge(['class' => static::buildClassName($type)], $options));
64
    }
65
66
    /**
67
     * @param string $type Tariff type
68
     * @return string
69
     */
70
    protected static function buildClassName($type)
71
    {
72
        return 'hipanel\modules\finance\logic\\' . ucfirst($type) . 'TariffManager';
73
    }
74
}
75