Completed
Push — master ( f7bdb9...8d2b6e )
by Andrii
10:42
created

src/forms/ServerTariffForm.php (3 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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\logic\ServerTariffCalculatorInterface;
15
use hipanel\modules\finance\models\Resource;
16
use hipanel\modules\finance\models\ServerResource;
17
use Yii;
18
use yii\web\UnprocessableEntityHttpException;
19
20
class ServerTariffForm extends AbstractTariffForm
21
{
22
    public $note;
23
    public $label;
24
25 View Code Duplication
    public function load($data, $formName = null)
26
    {
27
        $this->setAttributes($data[$this->formName()]);
28
        $this->setResources($data[(new ServerResource())->formName()]);
29
30
        return true;
31
    }
32
33 View Code Duplication
    public function rules()
34
    {
35
        $rules = parent::rules();
36
        $rules[] = [['note', 'label'], 'safe', 'on' => ['create', 'update']];
37
        unset($rules['parent-id-required']);
38
39
        return $rules;
40
    }
41
42
    /**
43
     * @return \hipanel\modules\finance\models\ServerResource[]
44
     */
45 View Code Duplication
    public function getHardwareResources()
46
    {
47
        /** @var ServerResource[] $resources */
48
        $resources = array_filter($this->tariff->resources, function ($model) {
49
            /** @var ServerResource $model */
50
            return $model->isHardwareTypeCorrect();
51
        });
52
53
        if (empty($result)) {
54
            return [];
55
        }
56
57
        $order = array_keys(reset($resources)->getHardwareTypes());
58
59
        return $this->sortResourcesByDefinedOrder($resources, $order, 'model_type');
60
    }
61
62
    /**
63
     * @param ServerResource[] $resources
64
     * @param array $order array of ordered values. $resources array will be re-ordered according this order
65
     * @param string $key the key that will be used to re-order
66
     * @return array
67
     */
68 View Code Duplication
    private function sortResourcesByDefinedOrder($resources, $order, $key)
69
    {
70
        $result = [];
71
        $resources = ArrayHelper::index($resources, $key);
72
73
        foreach ($order as $type) {
74
            if (isset($resources[$type])) {
75
                $result[] = $resources[$type];
76
            }
77
        }
78
79
        return $result;
80
    }
81
82
    /**
83
     * @return ServerResource[]
84
     */
85
    public function getOrFakeOveruseResources()
86
    {
87
        $types = (new ServerResource())->getTypes();
88
        $resources = $this->getOveruseResources();
89
90
        $result = [];
91
92
        foreach ($types as $type => $name) {
93
            foreach ($resources as $resource) {
94
                if ($resource->type === $type) {
0 ignored issues
show
The property type 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...
95
                    $result[] = $resource;
96
                    continue 2;
97
                }
98
            }
99
100
            $result[] = (new ServerResource(['type' => $type, 'tariff_id' => $this->tariff->id]));
101
        }
102
103
        return $result;
104
    }
105
106
    /**
107
     * @return \hipanel\modules\finance\models\ServerResource[]
108
     */
109 View Code Duplication
    public function getOveruseResources()
110
    {
111
        /** @var ServerResource[] $resources */
112
        $resources = array_filter($this->tariff->resources, function ($model) {
113
            /** @var ServerResource $model */
114
            return $model->isTypeCorrect();
115
        });
116
117
        if (empty($resources)) {
118
            return [];
119
        }
120
121
        $order = array_keys(reset($resources)->getTypes());
122
123
        return $this->sortResourcesByDefinedOrder($resources, $order, 'type');
124
    }
125
126 View Code Duplication
    public function getParentOveruseResource($type_id)
127
    {
128
        return reset(array_filter($this->parentTariff->resources, function ($resource) use ($type_id) {
129
            /** @var ServerResource $resource */
130
            return strcmp($resource->type_id, $type_id) === 0 && $resource->isTypeCorrect();
0 ignored issues
show
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...
131
        }));
132
    }
133
134
    /**
135
     * @return \hipanel\modules\finance\models\ServerResource[]
136
     */
137 View Code Duplication
    public function getParentHardwareResource($object_id)
138
    {
139
        return reset(array_filter($this->parentTariff->resources, function ($resource) use ($object_id) {
140
            /** @var ServerResource $resource */
141
            return strcmp($resource->object_id, $object_id) === 0 && $resource->isHardwareTypeCorrect();
0 ignored issues
show
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...
142
        }));
143
    }
144
145
    /** {@inheritdoc} */
146 View Code Duplication
    public function setResources($resources)
147
    {
148
        $result = [];
149
        foreach ((array) $resources as $resource) {
150
            if ($resource instanceof ServerResource) {
151
                $result[] = $resource;
152
                continue;
153
            }
154
155
            $model = new ServerResource(['scenario' => $this->scenario]);
156
157
            if ($model->load($resource, '') && $model->validate()) {
158
                $result[] = $model;
159
            } else {
160
                throw new UnprocessableEntityHttpException('Failed to load resource model: ' . reset($model->getFirstErrors()));
161
            }
162
        }
163
164
        $this->_resources = $result;
165
166
        return $this;
167
    }
168
169 View Code Duplication
    protected function calculator()
170
    {
171
        if (!isset($this->_calculator)) {
172
            $this->_calculator = Yii::createObject(ServerTariffCalculatorInterface::class, [[$this->tariff]]);
173
        }
174
175
        return $this->_calculator;
176
    }
177
178 View Code Duplication
    protected function parentCalculator()
179
    {
180
        if (!isset($this->_parentCalculator)) {
181
            $this->_parentCalculator = Yii::createObject(ServerTariffCalculatorInterface::class, [[$this->parentTariff]]);
182
        }
183
184
        return $this->_parentCalculator;
185
    }
186
}
187