Completed
Push — master ( 844c41...030ae6 )
by Dmitry
04:17
created

DomainTariffManager   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 62
Duplicated Lines 41.94 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 8
c 2
b 0
f 0
lcom 1
cbo 6
dl 26
loc 62
ccs 0
cts 32
cp 0
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 2
A createForm() 0 4 1
A insert() 14 14 2
A update() 12 12 2
A getZones() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace hipanel\modules\finance\logic;
4
5
use hipanel\modules\finance\forms\DomainTariffForm;
6
use hipanel\modules\finance\models\Tariff;
7
use hiqdev\hiart\ErrorResponseException;
8
use yii\web\NotFoundHttpException;
9
use yii\web\UnprocessableEntityHttpException;
10
use Yii;
11
12
class DomainTariffManager extends AbstractTariffManager
13
{
14
    /**
15
     * @var DomainTariffForm
16
     */
17
    public $form;
18
19
    /**
20
     * @inheritdoc
21
     */
22
    protected $type = 'domain';
23
24
    public function __construct($tariff = null)
25
    {
26
        if (!Yii::getAlias('@domain', true)) {
27
            throw new NotFoundHttpException('Domain module is missing');
28
        }
29
30
        parent::__construct($tariff);
31
    }
32
33
    protected function createForm($tariff = null)
34
    {
35
        $this->form = (new DomainTariffForm())->fill($this->getZones(), $this->baseTariff, $tariff);
36
    }
37
38 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...
39
    {
40
        $data = $this->form->toArray();
41
42
        try {
43
            $result = Tariff::perform('Create', $data);
44
        } catch (ErrorResponseException $e) {
45
            throw new UnprocessableEntityHttpException($e->getMessage(), 0, $e);
46
        }
47
48
        $this->form->id = $result['id'];
49
50
        return true;
51
    }
52
53 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...
54
    {
55
        $data = $this->form->toArray();
56
57
        try {
58
            $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...
59
        } catch (ErrorResponseException $e) {
60
            throw new UnprocessableEntityHttpException($e->getMessage(), 0, $e);
61
        }
62
63
        return true;
64
    }
65
66
    /**
67
     * @return array
68
     */
69
    protected function getZones()
70
    {
71
        return Yii::$app->hiart->get('getZones');
72
    }
73
}
74