Completed
Push — master ( b66556...89216d )
by Dmitry
21:57
created

TariffController::actionCreateCertificate()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 14

Duplication

Lines 13
Ratio 92.86 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 13
loc 14
ccs 0
cts 7
cp 0
rs 9.7998
c 0
b 0
f 0
cc 3
nc 2
nop 1
crap 12
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\filters\EasyAccessControl;
19
use hipanel\models\Ref;
20
use hipanel\modules\finance\logic\DomainTariffManager;
21
use hipanel\modules\finance\logic\TariffManagerFactory;
22
use hipanel\modules\finance\models\CertificateResource;
23
use Yii;
24
25
class TariffController extends \hipanel\base\CrudController
26
{
27 View Code Duplication
    public function behaviors()
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...
28
    {
29
        return array_merge(parent::behaviors(), [
0 ignored issues
show
Bug Best Practice introduced by
The return type of return array_merge(paren...'*' => 'plan.read')))); (array<*,array>) is incompatible with the return type of the parent method hipanel\base\Controller::behaviors of type array[].

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
30
            'access-control' => [
31
                'class' => EasyAccessControl::class,
32
                'actions' => [
33
                    'create,import,copy' => 'plan.create',
34
                    'update'    => 'plan.update',
35
                    'delete'    => 'plan.delete',
36
                    '*'         => 'plan.read',
37
                ],
38
            ],
39
        ]);
40
    }
41
42 1
    public function actions()
43
    {
44 1
        return array_merge(parent::actions(), [
45 1
            'index' => [
46
                'class' => IndexAction::class,
47
                'data' => function () {
48
                    return [
49
                        'types' => Ref::getList('type,tariff', 'hipanel:finance:tariff:types'),
50
                    ];
51 1
                },
52
            ],
53
            'search' => [
54
                'class' => ComboSearchAction::class,
55
            ],
56
            'validate-form' => [
57
                'class' => ValidateFormAction::class,
58
            ],
59
            'set-note' => [
60
                'class' => SmartUpdateAction::class,
61 1
                'success' => Yii::t('hipanel', 'Note updated'),
62
            ],
63
            'delete' => [
64
                'class' => SmartDeleteAction::class,
65 1
                'success' => Yii::t('hipanel:finance:tariff', 'Tariff deleted'),
66
            ],
67
        ]);
68
    }
69
70 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...
71
    {
72
        /** @var DomainTariffManager $manager */
73
        $manager = TariffManagerFactory::createByType('domain', $parent_id);
74
        $form = $manager->form;
75
76
        if (Yii::$app->request->isPost && $form->load(Yii::$app->request->post())) {
77
            $manager->insert();
78
79
            return $this->redirect(['view', 'id' => $form->id]);
80
        }
81
82
        return $this->render('domain/create', ['model' => $form]);
83
    }
84
85 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...
86
    {
87
        /** @var DomainTariffManager $manager */
88
        $manager = TariffManagerFactory::createByType('svds', $parent_id);
89
90
        $form = $manager->form;
91
92
        if (Yii::$app->request->isPost && $form->load(Yii::$app->request->post())) {
93
            $manager->insert();
94
95
            return $this->redirect(['view', 'id' => $form->id]);
96
        }
97
98
        return $this->render('vds/create', ['model' => $form]);
99
    }
100
101 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...
102
    {
103
        /** @var DomainTariffManager $manager */
104
        $manager = TariffManagerFactory::createByType('ovds', $parent_id);
105
        $form = $manager->form;
106
107
        if (Yii::$app->request->isPost && $form->load(Yii::$app->request->post())) {
108
            $manager->insert();
109
110
            return $this->redirect(['view', 'id' => $form->id]);
111
        }
112
113
        return $this->render('vds/create', ['model' => $form]);
114
    }
115
116 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...
117
    {
118
        /** @var CertificateResource $manager */
119
        $manager = TariffManagerFactory::createByType('certificate', $parent_id);
120
        $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...
121
122
        if (Yii::$app->request->isPost && $form->load(Yii::$app->request->post())) {
123
            $manager->insert();
124
125
            return $this->redirect(['view', 'id' => $form->id]);
126
        }
127
128
        return $this->render('certificate/create', ['model' => $form]);
129
    }
130
131 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...
132
    {
133
        /** @var CertificateResource $manager */
134
        $manager = TariffManagerFactory::createByType('server', $parent_id);
135
        $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...
136
137
        if (Yii::$app->request->isPost && $form->load(Yii::$app->request->post())) {
138
            $manager->insert();
139
140
            return $this->redirect(['view', 'id' => $form->id]);
141
        }
142
143
        return $this->render('server/create', ['model' => $form]);
144
    }
145
146 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...
147
    {
148
        $manager = TariffManagerFactory::createById($id, ['scenario' => 'update']);
149
        $form = $manager->form;
150
151
        if (Yii::$app->request->isPost && $form->load(Yii::$app->request->post())) {
152
            $manager->update();
153
154
            return $this->redirect(['view', 'id' => $form->id]);
155
        }
156
157
        return $this->render($manager->getType() . '/update', ['model' => $form]);
158
    }
159
160
    public function actionCopy()
161
    {
162
        $id = Yii::$app->request->post('selection')[0];
163
        $manager = TariffManagerFactory::createById($id, ['scenario' => 'create']);
164
        $form = $manager->form;
165
        $form->id = null;
166
167
        return $this->render($manager->getType() . '/copy', ['model' => $form]);
168
    }
169
170
    public function actionView($id)
171
    {
172
        $manager = TariffManagerFactory::createById($id);
173
174
        return $this->render('view', ['manager' => $manager]);
175
    }
176
}
177