Completed
Push — master ( 30851e...5701cd )
by Dmitry
06:20 queued 02:16
created

DomainTariffManager::getFormOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

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 7
ccs 0
cts 7
cp 0
rs 9.4285
cc 1
eloc 5
nc 1
nop 0
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-2017, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hipanel\modules\finance\logic;
12
13
use hiqdev\hiart\ConnectionInterface;
14
use hipanel\modules\finance\forms\DomainTariffForm;
15
use hipanel\modules\finance\models\Tariff;
16
use hiqdev\hiart\ResponseErrorException;
17
use Yii;
18
use yii\web\NotFoundHttpException;
19
use yii\web\UnprocessableEntityHttpException;
20
21
class DomainTariffManager extends AbstractTariffManager
22
{
23
    /**
24
     * @var DomainTariffForm
25
     */
26
    public $form;
27
28
    /**
29
     * {@inheritdoc}
30
     */
31
    protected $type = Tariff::TYPE_DOMAIN;
32
33
    /**
34
     * @var ConnectionInterface
35
     */
36
    private $connection;
37
38
    public function __construct(ConnectionInterface $connection, $config = [])
39
    {
40
        $this->connection = $connection;
41
42
        parent::__construct($config);
43
    }
44
45
    public function init()
46
    {
47
        parent::init();
48
49
        if (!Yii::getAlias('@domain', true)) {
50
            throw new NotFoundHttpException('Domain module is missing');
51
        }
52
53
        $this->formOptions['zones'] = $this->getZones();
54
    }
55
56 View Code Duplication
    public function insert()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
57
    {
58
        $data = $this->form->toArray();
59
60
        try {
61
            $result = Tariff::perform('create', $data);
62
        } catch (ResponseErrorException $e) {
63
            throw new UnprocessableEntityHttpException($e->getMessage(), 0, $e);
64
        }
65
66
        $this->form->id = $result['id'];
67
68
        return true;
69
    }
70
71
    public function update()
72
    {
73
        $data = $this->form->toArray();
74
75
        try {
76
            $result = Tariff::perform('update', $data);
0 ignored issues
show
Unused Code introduced by
$result is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
77
        } catch (ResponseErrorException $e) {
78
            throw new UnprocessableEntityHttpException($e->getMessage(), 0, $e);
79
        }
80
81
        return true;
82
    }
83
84
    protected function getFormOptions()
85
    {
86
        return array_merge([
87
            'class' => DomainTariffForm::class,
88
            'zones' => $this->getZones(),
89
        ], parent::getFormOptions());
90
    }
91
92
    /**
93
     * @return array
94
     */
95
    protected function getZones()
96
    {
97
        $command = $this->connection->createCommand();
98
        return $command->perform('getZones', '');
99
    }
100
}
101