Completed
Push — master ( 0ac49a...440acf )
by Dmitry
07:30
created

TariffController   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 130
Duplicated Lines 60 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 24.24%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 21
lcom 1
cbo 7
dl 78
loc 130
ccs 16
cts 66
cp 0.2424
rs 10
c 1
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A actionCreateOvds() 13 13 3
A actionCreateCertificate() 13 13 3
A actionCreateSvds() 13 14 3
B actions() 0 27 1
A actionCreateDomain() 13 13 3
A actionView() 0 6 1
A actionCreateServer() 13 13 3
A actionUpdate() 12 12 3
A actionCopy() 0 8 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
 * 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\controllers;
12
13
use hipanel\actions\ComboSearchAction;
14
use hipanel\actions\IndexAction;
15
use hipanel\actions\SmartDeleteAction;
16
use hipanel\actions\SmartUpdateAction;
17
use hipanel\actions\ValidateFormAction;
18
use hipanel\models\Ref;
19
use hipanel\modules\finance\logic\DomainTariffManager;
20
use hipanel\modules\finance\logic\TariffManagerFactory;
21
use hipanel\modules\finance\models\CertificateResource;
22
use Yii;
23
24
class TariffController extends \hipanel\base\CrudController
25
{
26 1
    public function actions()
27
    {
28
        return [
29
            'index' => [
30 1
                'class' => IndexAction::class,
31 1
                'data' => function () {
32
                    return [
33
                        'types' => Ref::getList('type,tariff', 'hipanel:finance:tariff:types'),
34
                    ];
35 1
                },
36 1
            ],
37
            'search' => [
38 1
                'class' => ComboSearchAction::class,
39 1
            ],
40
            'validate-form' => [
41 1
                'class' => ValidateFormAction::class,
42 1
            ],
43
            'set-note' => [
44 1
                'class' => SmartUpdateAction::class,
45 1
                'success' => Yii::t('hipanel', 'Note updated'),
46 1
            ],
47
            'delete' => [
48 1
                'class' => SmartDeleteAction::class,
49 1
                'success' => Yii::t('hipanel:finance:tariff', 'Tariff deleted'),
50 1
            ],
51 1
        ];
52
    }
53
54 View Code Duplication
    public function actionCreateDomain($parent_id = null)
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...
55
    {
56
        /** @var DomainTariffManager $manager */
57
        $manager = TariffManagerFactory::createByType('domain', $parent_id);
58
        $form = $manager->form;
59
60
        if (Yii::$app->request->isPost && $form->load(Yii::$app->request->post())) {
61
            $manager->insert();
62
            return $this->redirect(['view', 'id' => $form->id]);
63
        }
64
65
        return $this->render('domain/create', ['model' => $form]);
66
    }
67
68 View Code Duplication
    public function actionCreateSvds($parent_id = null)
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...
69
    {
70
        /** @var DomainTariffManager $manager */
71
        $manager = TariffManagerFactory::createByType('svds', $parent_id);
72
73
        $form = $manager->form;
74
75
        if (Yii::$app->request->isPost && $form->load(Yii::$app->request->post())) {
76
            $manager->insert();
77
            return $this->redirect(['view', 'id' => $form->id]);
78
        }
79
80
        return $this->render('vds/create', ['model' => $form]);
81
    }
82
83 View Code Duplication
    public function actionCreateOvds($parent_id = null)
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...
84
    {
85
        /** @var DomainTariffManager $manager */
86
        $manager = TariffManagerFactory::createByType('ovds', $parent_id);
87
        $form = $manager->form;
88
89
        if (Yii::$app->request->isPost && $form->load(Yii::$app->request->post())) {
90
            $manager->insert();
91
            return $this->redirect(['view', 'id' => $form->id]);
92
        }
93
94
        return $this->render('vds/create', ['model' => $form]);
95
    }
96
97 View Code Duplication
    public function actionCreateCertificate($parent_id = null)
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...
98
    {
99
        /** @var CertificateResource $manager */
100
        $manager = TariffManagerFactory::createByType('certificate', $parent_id);
101
        $form = $manager->form;
0 ignored issues
show
Documentation introduced by
The property form does not exist on object<hipanel\modules\f...ls\CertificateResource>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write 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.");
        }
    }

}

Since the property has write access only, you can use the @property-write 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...
102
103
        if (Yii::$app->request->isPost && $form->load(Yii::$app->request->post())) {
104
            $manager->insert();
105
            return $this->redirect(['view', 'id' => $form->id]);
106
        }
107
108
        return $this->render('certificate/create', ['model' => $form]);
109
    }
110
111 View Code Duplication
    public function actionCreateServer($parent_id = null)
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...
112
    {
113
        /** @var CertificateResource $manager */
114
        $manager = TariffManagerFactory::createByType('server', $parent_id);
115
        $form = $manager->form;
0 ignored issues
show
Documentation introduced by
The property form does not exist on object<hipanel\modules\f...ls\CertificateResource>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write 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.");
        }
    }

}

Since the property has write access only, you can use the @property-write 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...
116
117
        if (Yii::$app->request->isPost && $form->load(Yii::$app->request->post())) {
118
            $manager->insert();
119
            return $this->redirect(['view', 'id' => $form->id]);
120
        }
121
122
        return $this->render('server/create', ['model' => $form]);
123
    }
124
125 View Code Duplication
    public function actionUpdate($id)
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...
126
    {
127
        $manager = TariffManagerFactory::createById($id, ['scenario' => 'update']);
128
        $form = $manager->form;
129
130
        if (Yii::$app->request->isPost && $form->load(Yii::$app->request->post())) {
131
            $manager->update();
132
            return $this->redirect(['view', 'id' => $form->id]);
133
        }
134
135
        return $this->render($manager->getType() . '/update', ['model' => $form]);
136
    }
137
138
    public function actionCopy()
139
    {
140
        $id = Yii::$app->request->post('selection')[0];
141
        $manager = TariffManagerFactory::createById($id, ['scenario' => 'create']);
142
        $form = $manager->form;
143
144
        return $this->render($manager->getType() . '/copy', ['model' => $form]);
145
    }
146
147
    public function actionView($id)
148
    {
149
        $manager = TariffManagerFactory::createById($id);
150
151
        return $this->render('view', ['manager' => $manager]);
152
    }
153
}
154