Completed
Push — master ( 150de4...ecb3c3 )
by Dmitry
05:20
created

VdsTariffForm::getParentHardwareResource()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 7
ccs 0
cts 3
cp 0
rs 9.4285
cc 2
eloc 3
nc 1
nop 1
crap 6
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, $formName = null)
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
     * @return \hipanel\modules\finance\models\ServerResource[]
67
     */
68 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...
69
    {
70
        /** @var ServerResource[] $resources */
71
        $resources = array_filter($this->tariff->resources, function ($model) {
72
            /** @var ServerResource $model */
73
            return $model->isTypeCorrect();
74
        });
75
        $order = array_keys(reset($resources)->getTypes());
76
77
        return $this->sortResourcesByDefinedOrder($resources, $order, 'type');
78
79
    }
80
81
    public function getParentOveruseResource($type_id)
82
    {
83
        return reset(array_filter($this->parentTariff->resources, function ($resource) use ($type_id) {
0 ignored issues
show
Bug introduced by
array_filter($this->pare...ce->isTypeCorrect(); }) cannot be passed to reset() as the parameter $array expects a reference.
Loading history...
84
            /** @var ServerResource $resource */
85
            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...
86
        }));
87
    }
88
89
    /**
90
     * @return \hipanel\modules\finance\models\ServerResource[]
91
     */
92
    public function getParentHardwareResource($object_id)
93
    {
94
        return reset(array_filter($this->parentTariff->resources, function ($resource) use ($object_id) {
0 ignored issues
show
Bug introduced by
array_filter($this->pare...rdwareTypeCorrect(); }) cannot be passed to reset() as the parameter $array expects a reference.
Loading history...
95
            /** @var ServerResource $resource */
96
            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...
97
        }));
98
    }
99
100
    /** @inheritdoc */
101 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...
102
    {
103
        $result = [];
104
        foreach ((array) $resources as $resource) {
105
            if ($resource instanceof ServerResource) {
106
                $result[] = $resource;
107
                continue;
108
            }
109
110
            $model = new ServerResource(['scenario' => $this->scenario]);
111
112
            if ($model->load($resource, '') && $model->validate()) {
113
                $result[] = $model;
114
            } else {
115
                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...
116
            }
117
        }
118
119
        $this->_resources = $result;
120
121
        return $this;
122
    }
123
}
124