Completed
Push — master ( 0622f1...018a2f )
by Dmitry
04:34
created

VdsTariffForm::setResources()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 22
Code Lines 13

Duplication

Lines 22
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 22
loc 22
ccs 0
cts 18
cp 0
rs 8.6737
cc 5
eloc 13
nc 4
nop 1
crap 30
1
<?php
2
3
namespace hipanel\modules\finance\forms;
4
5
use hipanel\helpers\ArrayHelper;
6
use hipanel\modules\finance\models\ServerResource;
7
use yii\web\UnprocessableEntityHttpException;
8
9
class VdsTariffForm extends AbstractTariffForm
10
{
11
    public $note;
12
    public $label;
13
14
    public function load($data)
15
    {
16
        $this->setAttributes($data[$this->formName()]);
17
        $this->setResources($data[(new ServerResource())->formName()]);
18
19
        return true;
20
    }
21
22
    public function rules()
23
    {
24
        $rules = parent::rules();
25
        $rules[] = [['note', 'label'], 'safe', 'on' => ['create', 'update']];
26
27
        return $rules;
28
    }
29
30
    /**
31
     * @return \hipanel\modules\finance\models\ServerResource[]
32
     */
33 View Code Duplication
    public function getHardwareResources()
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...
34
    {
35
        /** @var ServerResource[] $resources */
36
        $resources = array_filter($this->tariff->resources, function ($model) {
37
            /** @var ServerResource $model */
38
            return $model->isHardwareTypeCorrect();
39
        });
40
        $order = array_keys(reset($resources)->getHardwareTypes());
41
42
        return $this->sortResourcesByDefinedOrder($resources, $order, 'model_type');
43
    }
44
45
    /**
46
     * @param ServerResource[] $resources
47
     * @param array $order array of ordered values. $resources array will be re-ordered according this order
48
     * @param string $key the key that will be used to re-order
49
     * @return array
50
     */
51
    private function sortResourcesByDefinedOrder($resources, $order, $key)
52
    {
53
        $result = [];
54
        $resources = ArrayHelper::index($resources, $key);
55
56
        foreach ($order as $type) {
57
            if (isset($resources[$type])) {
58
                $result[] = $resources[$type];
59
            }
60
        }
61
62
        return $result;
63
    }
64
65
66
67
    /**
68
     * @return \hipanel\modules\finance\models\ServerResource[]
69
     */
70 View Code Duplication
    public function getOveruseResources()
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 ServerResource[] $resources */
73
        $resources = array_filter($this->tariff->resources, function ($model) {
74
            /** @var ServerResource $model */
75
            return $model->isTypeCorrect();
76
        });
77
        $order = array_keys(reset($resources)->getTypes());
78
79
        return $this->sortResourcesByDefinedOrder($resources, $order, 'type');
80
81
    }
82
83
    public function getBaseOveruseResource($type_id)
84
    {
85
        return reset(array_filter($this->baseTariff->resources, function ($resource) use ($type_id) {
0 ignored issues
show
Bug introduced by
array_filter($this->base...ce->isTypeCorrect(); }) cannot be passed to reset() as the parameter $array expects a reference.
Loading history...
86
            /** @var ServerResource $resource */
87
            return $resource->type_id == $type_id && $resource->isTypeCorrect();
0 ignored issues
show
Documentation introduced by
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...
88
        }));
89
    }
90
91
    /**
92
     * @return \hipanel\modules\finance\models\ServerResource[]
93
     */
94
    public function getBaseHardwareResource($object_id)
95
    {
96
        return reset(array_filter($this->baseTariff->resources, function ($resource) use ($object_id) {
0 ignored issues
show
Bug introduced by
array_filter($this->base...rdwareTypeCorrect(); }) cannot be passed to reset() as the parameter $array expects a reference.
Loading history...
97
            /** @var ServerResource $resource */
98
            return $resource->object_id == $object_id && $resource->isHardwareTypeCorrect();
0 ignored issues
show
Documentation introduced by
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...
99
        }));
100
    }
101
102
    /** @inheritdoc */
103 View Code Duplication
    public function setResources($resources)
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...
104
    {
105
        $result = [];
106
        foreach ((array) $resources as $resource) {
107
            if ($resource instanceof ServerResource) {
108
                $result[] = $resource;
109
                continue;
110
            }
111
112
            $model = new ServerResource(['scenario' => $this->scenario]);
113
114
            if ($model->load($resource, '') && $model->validate()) {
115
                $result[] = $model;
116
            } else {
117
                throw new UnprocessableEntityHttpException('Failed to load resource model: ' . reset($model->getFirstErrors()));
0 ignored issues
show
Bug introduced by
$model->getFirstErrors() cannot be passed to reset() as the parameter $array expects a reference.
Loading history...
118
            }
119
        }
120
121
        $this->_resources = $result;
122
123
        return $this;
124
    }
125
}
126