Completed
Push — master ( 3d52a5...b51608 )
by Dmitry
04:01
created

ServerTariffForm::sortResourcesByDefinedOrder()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 13
Ratio 100 %

Importance

Changes 0
Metric Value
dl 13
loc 13
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 3
nop 3
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\forms;
12
13
use hipanel\helpers\ArrayHelper;
14
use hipanel\modules\finance\logic\ServerTariffCalculatorInterface;
15
use hipanel\modules\finance\models\ServerResource;
16
use Yii;
17
use yii\web\UnprocessableEntityHttpException;
18
19
class ServerTariffForm extends AbstractTariffForm
20
{
21
    public $note;
22
    public $label;
23
24 View Code Duplication
    public function load($data, $formName = 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...
25
    {
26
        $this->setAttributes($data[$this->formName()]);
27
        $this->setResources($data[(new ServerResource())->formName()]);
28
29
        return true;
30
    }
31
32 View Code Duplication
    public function rules()
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...
33
    {
34
        $rules = parent::rules();
35
        $rules[] = [['note', 'label'], 'safe', 'on' => ['create', 'update']];
36
37
        return $rules;
38
    }
39
40
    /**
41
     * @return \hipanel\modules\finance\models\ServerResource[]
42
     */
43 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...
44
    {
45
        /** @var ServerResource[] $resources */
46
        $resources = array_filter($this->tariff->resources, function ($model) {
47
            /** @var ServerResource $model */
48
            return $model->isHardwareTypeCorrect();
49
        });
50
51
        if (empty($result)) {
0 ignored issues
show
Bug introduced by
The variable $result seems to never exist, and therefore empty should always return true. Did you maybe rename this variable?

This check looks for calls to isset(...) or empty() on variables that are yet undefined. These calls will always produce the same result and can be removed.

This is most likely caused by the renaming of a variable or the removal of a function/method parameter.

Loading history...
52
            return [];
53
        }
54
55
        $order = array_keys(reset($resources)->getHardwareTypes());
56
        return $this->sortResourcesByDefinedOrder($resources, $order, 'model_type');
57
    }
58
59
    /**
60
     * @param ServerResource[] $resources
61
     * @param array $order array of ordered values. $resources array will be re-ordered according this order
62
     * @param string $key the key that will be used to re-order
63
     * @return array
64
     */
65 View Code Duplication
    private function sortResourcesByDefinedOrder($resources, $order, $key)
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...
66
    {
67
        $result = [];
68
        $resources = ArrayHelper::index($resources, $key);
69
70
        foreach ($order as $type) {
71
            if (isset($resources[$type])) {
72
                $result[] = $resources[$type];
73
            }
74
        }
75
76
        return $result;
77
    }
78
79
    /**
80
     * @return \hipanel\modules\finance\models\ServerResource[]
81
     */
82 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...
83
    {
84
        /** @var ServerResource[] $resources */
85
        $resources = array_filter($this->tariff->resources, function ($model) {
86
            /** @var ServerResource $model */
87
            return $model->isTypeCorrect();
88
        });
89
90
        if (empty($resources)) {
91
            return [];
92
        }
93
94
        $order = array_keys(reset($resources)->getTypes());
95
96
        return $this->sortResourcesByDefinedOrder($resources, $order, 'type');
97
    }
98
99 View Code Duplication
    public function getParentOveruseResource($type_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...
100
    {
101
        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...
102
            /** @var ServerResource $resource */
103
            return strcmp($resource->type_id, $type_id) === 0 && $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...
104
        }));
105
    }
106
107
    /**
108
     * @return \hipanel\modules\finance\models\ServerResource[]
109
     */
110 View Code Duplication
    public function getParentHardwareResource($object_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...
111
    {
112
        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...
113
            /** @var ServerResource $resource */
114
            return strcmp($resource->object_id, $object_id) === 0 && $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...
115
        }));
116
    }
117
118
    /** {@inheritdoc} */
119 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...
120
    {
121
        $result = [];
122
        foreach ((array) $resources as $resource) {
123
            if ($resource instanceof ServerResource) {
124
                $result[] = $resource;
125
                continue;
126
            }
127
128
            $model = new ServerResource(['scenario' => $this->scenario]);
129
130
            if ($model->load($resource, '') && $model->validate()) {
131
                $result[] = $model;
132
            } else {
133
                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...
134
            }
135
        }
136
137
        $this->_resources = $result;
138
139
        return $this;
140
    }
141
142 View Code Duplication
    protected function calculator()
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...
143
    {
144
        if (!isset($this->_calculator)) {
145
            $this->_calculator = Yii::createObject(ServerTariffCalculatorInterface::class, [[$this->tariff]]);
146
        }
147
148
        return $this->_calculator;
149
    }
150
151 View Code Duplication
    protected function parentCalculator()
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...
152
    {
153
        if (!isset($this->_parentCalculator)) {
154
            $this->_parentCalculator = Yii::createObject(ServerTariffCalculatorInterface::class, [[$this->parentTariff]]);
155
        }
156
157
        return $this->_parentCalculator;
158
    }
159
}
160