VdsTariffForm::setResources()   A
last analyzed

Complexity

Conditions 5
Paths 4

Size

Total Lines 22

Duplication

Lines 22
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 22
loc 22
ccs 0
cts 18
cp 0
rs 9.2568
cc 5
nc 4
nop 1
crap 30
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-2019, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hipanel\modules\finance\forms;
12
13
use hipanel\helpers\ArrayHelper;
14
use hipanel\modules\finance\models\ServerResource;
15
use yii\web\UnprocessableEntityHttpException;
16
17
class VdsTariffForm extends AbstractTariffForm
18
{
19
    public $note;
20
    public $label;
21
22 View Code Duplication
    public function load($data, $formName = null)
0 ignored issues
show
Duplication introduced by SilverFire - Dmitry Naumenko
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...
23
    {
24
        $this->setAttributes($data[$this->formName()]);
25
        $this->setResources($data[(new ServerResource())->formName()]);
26
27
        return true;
28
    }
29
30 View Code Duplication
    public function rules()
0 ignored issues
show
Duplication introduced by SilverFire - Dmitry Naumenko
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...
31
    {
32
        $rules = parent::rules();
33
        $rules[] = [['note', 'label'], 'safe', 'on' => ['create', 'update']];
34
35
        return $rules;
36
    }
37
38
    /**
39
     * @return \hipanel\modules\finance\models\ServerResource[]
40
     */
41 View Code Duplication
    public function getHardwareResources()
0 ignored issues
show
Duplication introduced by SilverFire - Dmitry Naumenko
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...
42
    {
43
        /** @var ServerResource[] $resources */
44
        $resources = array_filter($this->tariff->resources, function ($model) {
45
            /** @var ServerResource $model */
46
            return $model->isHardwareTypeCorrect();
47
        });
48
49
        if (empty($resources)) {
50
            return [];
51
        }
52
53
        $order = array_keys(reset($resources)->getHardwareTypes());
54
55
        return $this->sortResourcesByDefinedOrder($resources, $order, 'model_type');
56
    }
57
58
    /**
59
     * @param ServerResource[] $resources
60
     * @param array $order array of ordered values. $resources array will be re-ordered according this order
61
     * @param string $key the key that will be used to re-order
62
     * @return array
63
     */
64 View Code Duplication
    private function sortResourcesByDefinedOrder($resources, $order, $key)
0 ignored issues
show
Duplication introduced by SilverFire - Dmitry Naumenko
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...
65
    {
66
        $result = [];
67
        $resources = ArrayHelper::index($resources, $key);
68
69
        foreach ($order as $type) {
70
            if (isset($resources[$type])) {
71
                $result[] = $resources[$type];
72
            }
73
        }
74
75
        return $result;
76
    }
77
78
    /**
79
     * @return \hipanel\modules\finance\models\ServerResource[]
80
     */
81 View Code Duplication
    public function getOveruseResources()
0 ignored issues
show
Duplication introduced by SilverFire - Dmitry Naumenko
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...
82
    {
83
        /** @var ServerResource[] $resources */
84
        $resources = array_filter($this->tariff->resources, function ($model) {
85
            /** @var ServerResource $model */
86
            return $model->isTypeCorrect();
87
        });
88
        if (empty($resources)) {
89
            return [];
90
        }
91
92
        $order = array_keys(reset($resources)->getTypes());
93
94
        return $this->sortResourcesByDefinedOrder($resources, $order, 'type');
95
    }
96
97 View Code Duplication
    public function getParentOveruseResource($type_id)
0 ignored issues
show
Duplication introduced by SilverFire - Dmitry Naumenko
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
        return reset(array_filter($this->parentTariff->resources, function ($resource) use ($type_id) {
0 ignored issues
show
Bug introduced by SilverFire - Dmitry Naumenko
array_filter($this->pare...ce->isTypeCorrect(); }) cannot be passed to reset() as the parameter $array expects a reference.
Loading history...
100
            /** @var ServerResource $resource */
101
            return strcmp($resource->type_id, $type_id) === 0 && $resource->isTypeCorrect();
0 ignored issues
show
Documentation introduced by SilverFire - Dmitry Naumenko
The property type_id does not exist on object<hipanel\modules\f...\models\ServerResource>. Since you implemented __get, maybe consider adding a @property annotation.

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

}

If the property has read access only, you can use the @property-read 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
    }
104
105
    /**
106
     * @return \hipanel\modules\finance\models\ServerResource[]
107
     */
108 View Code Duplication
    public function getParentHardwareResource($object_id)
0 ignored issues
show
Duplication introduced by SilverFire - Dmitry Naumenko
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...
109
    {
110
        return reset(array_filter($this->parentTariff->resources, function ($resource) use ($object_id) {
0 ignored issues
show
Bug introduced by SilverFire - Dmitry Naumenko
array_filter($this->pare...rdwareTypeCorrect(); }) cannot be passed to reset() as the parameter $array expects a reference.
Loading history...
111
            /** @var ServerResource $resource */
112
            return strcmp($resource->object_id, $object_id) === 0 && $resource->isHardwareTypeCorrect();
0 ignored issues
show
Documentation introduced by SilverFire - Dmitry Naumenko
The property object_id does not exist on object<hipanel\modules\f...\models\ServerResource>. Since you implemented __get, maybe consider adding a @property annotation.

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

}

If the property has read access only, you can use the @property-read 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...
113
        }));
114
    }
115
116
    /** {@inheritdoc} */
117 View Code Duplication
    public function setResources($resources)
0 ignored issues
show
Duplication introduced by SilverFire - Dmitry Naumenko
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...
118
    {
119
        $result = [];
120
        foreach ((array) $resources as $resource) {
121
            if ($resource instanceof ServerResource) {
122
                $result[] = $resource;
123
                continue;
124
            }
125
126
            $model = new ServerResource(['scenario' => $this->scenario]);
127
128
            if ($model->load($resource, '') && $model->validate()) {
129
                $result[] = $model;
130
            } else {
131
                throw new UnprocessableEntityHttpException('Failed to load resource model: ' . reset($model->getFirstErrors()));
0 ignored issues
show
Bug introduced by SilverFire - Dmitry Naumenko
$model->getFirstErrors() cannot be passed to reset() as the parameter $array expects a reference.
Loading history...
132
            }
133
        }
134
135
        $this->_resources = $result;
136
137
        return $this;
138
    }
139
}
140