Completed
Push — master ( 834dc7...c72fcf )
by Dmitry
14:24
created

CertificateTariffManager::init()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 2
eloc 4
nc 2
nop 0
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\models\Ref;
14
use hipanel\modules\finance\forms\CertificateTariffForm;
15
use hipanel\modules\finance\forms\DomainTariffForm;
16
use hipanel\modules\finance\models\Tariff;
17
use hiqdev\hiart\ConnectionInterface;
18
use hiqdev\hiart\ResponseErrorException;
19
use Yii;
20
use yii\web\NotFoundHttpException;
21
use yii\web\UnprocessableEntityHttpException;
22
23
/**
24
 * Class CertificateTariffManager
25
 *
26
 * @author Dmytro Naumenko <[email protected]>
27
 */
28
class CertificateTariffManager extends AbstractTariffManager
29
{
30
    /**
31
     * @var DomainTariffForm
32
     */
33
    public $form;
34
35
    /**
36
     * {@inheritdoc}
37
     */
38
    protected $type = Tariff::TYPE_CERT;
39
40
    /**
41
     * @var ConnectionInterface
42
     */
43
    private $connection;
44
45
    public function __construct(ConnectionInterface $connection, $config = [])
46
    {
47
        $this->connection = $connection;
48
49
        parent::__construct($config);
50
    }
51
52
    public function init()
53
    {
54
        parent::init();
55
56
        if (!Yii::getAlias('@certificate', true)) {
57
            throw new NotFoundHttpException('Certificate module is missing');
58
        }
59
    }
60
61
    public function insert()
62
    {
63
        $data = $this->form->toArray();
64
65
        try {
66
            $result = Tariff::perform('create', $data);
67
        } catch (ResponseErrorException $e) {
68
            throw new UnprocessableEntityHttpException($e->getMessage(), 0, $e);
69
        }
70
71
        $this->form->id = $result['id'];
72
73
        return true;
74
    }
75
76 View Code Duplication
    public function update()
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...
77
    {
78
        $data = $this->form->toArray();
79
80
        try {
81
            $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...
82
        } catch (ResponseErrorException $e) {
83
            throw new UnprocessableEntityHttpException($e->getMessage(), 0, $e);
84
        }
85
86
        return true;
87
    }
88
89
    protected function getFormOptions()
90
    {
91
        return array_merge([
92
            'class' => CertificateTariffForm::class,
93
            'certificateTypes' => Ref::getList('type,certificate', 'hipanel:certificate', [
94
                'select' => 'id_label',
95
                'mapOptions' => ['from' => 'id']
96
            ]),
97
        ], parent::getFormOptions());
98
    }
99
}
100