Completed
Push — master ( 81b279...95954c )
by Dmitry
07:31
created

Package::getResourceValue_cpu()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 7
ccs 0
cts 7
cp 0
rs 9.4285
cc 2
eloc 5
nc 2
nop 0
crap 6
1
<?php
2
3
/*
4
 * Server module for HiPanel
5
 *
6
 * @link      https://github.com/hiqdev/hipanel-module-server
7
 * @package   hipanel-module-server
8
 * @license   BSD-3-Clause
9
 * @copyright Copyright (c) 2015-2016, HiQDev (http://hiqdev.com/)
10
 */
11
12
namespace hipanel\modules\server\models;
13
14
use hipanel\modules\server\cart\Tariff;
15
use hipanel\modules\stock\models\Part;
16
use Yii;
17
use yii\base\InvalidConfigException;
18
use yii\base\Model;
19
20
/**
21
 * Class Package is a wrapper for [[Tariff]], its parts, resources and calculation of its price.
22
 * @property string $name
23
 */
24
class Package extends Model
25
{
26
    /**
27
     * @var string CRC32 temporary uniq id of the package
28
     */
29
    private $_id;
30
31
    /** @var Tariff */
32
    protected $_tariff;
33
34
    /** @var Part[] */
35
    public $parts = [];
36
37
    /** @var array */
38
    public $calculation;
39
40
    /**
41
     * @var
42
     */
43
    protected $_resources;
44
45
    public function init()
46
    {
47
        if (empty($this->parts)) {
48
            foreach ($this->getTariff()->resources as $resource) {
49
                if (isset($resource->part)) {
50
                    $this->parts[$resource->part->id] = $resource->part;
51
                }
52
            }
53
        }
54
    }
55
56
    /**
57
     * @param Tariff $tariff
58
     */
59
    public function setTariff($tariff)
60
    {
61
        $this->_tariff = $tariff;
62
    }
63
64
    /**
65
     * @throws InvalidConfigException
66
     * TODO: implement and get rid of many magic functions bellow
67
     */
68
    protected function initResources()
69
    {
70
        foreach ($this->_tariff->resources as $resource) {
71
            $id = $resource->id;
72
73
            $this->_resources[$id] = Yii::createObject([
74
                'class' => $this->buildResourceClass($resource),
75
                'resource' => $resource,
76
            ]);
77
        }
78
    }
79
80
    /**
81
     * @param $resource
82
     * @return string
83
     * TODO: implement and get rid of many magic functions bellow
84
     */
85
    protected function buildResourceClass($resource)
86
    {
87
        if ($this->parts[$resource->object_id]) {
88
            return 'yii\base\Object';
89
        } else {
90
            return 'yii\base\Object';
91
        }
92
    }
93
94
    /**
95
     * @return Tariff
96
     */
97
    public function getTariff()
98
    {
99
        return $this->_tariff;
100
    }
101
102
    protected function getResourceValue_panel()
103
    {
104
        $result = Yii::t('hipanel/server/order', 'No panel / {hipanelLink}', ['hipanelLink' => 'HiPanel']); // todo: add faq link
105
        if ($this->getResourceByType('isp5')->quantity > 0) {
106
            $result .= ' / ' . Yii::t('hipanel/server/order', 'ISP manager');
107
        }
108
109
        return $result;
110
    }
111
112
    protected function getResourceTitle_panel()
113
    {
114
        return Yii::t('hipanel/server/order', 'Control panel');
115
    }
116
117
    protected function getResourceValue_purpose()
118
    {
119
        return Yii::t('hipanel/server/order/purpose', $this->_tariff->label);
0 ignored issues
show
Documentation introduced by
The property label does not exist on object<hipanel\modules\server\cart\Tariff>. 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...
120
    }
121
122
    protected function getResourceTitle_purpose()
123
    {
124
        return Yii::t('hipanel/server/order', 'Purpose');
125
    }
126
127
    /**
128
     * @return float
129
     */
130
    public function getPrice()
131
    {
132
        return $this->calculation['value']['usd']['value'];
133
    }
134
135
    /**
136
     * @throws InvalidConfigException
137
     * @return string
138
     */
139
    public function getDisplayPrice()
140
    {
141
        return Yii::t('hipanel/server/order', '{price}/mo', [
142
            'price' => Yii::$app->formatter->asCurrency($this->getPrice(), Yii::$app->params['currency']),
143
        ]);
144
    }
145
146
    /**
147
     * @return string
148
     */
149
    public function getName()
150
    {
151
        return $this->_tariff->name;
0 ignored issues
show
Documentation introduced by
The property name does not exist on object<hipanel\modules\server\cart\Tariff>. 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...
152
    }
153
154
    /**
155
     * @param string $type
156
     * @throws InvalidConfigException
157
     * @return mixed
158
     */
159 View Code Duplication
    public function getResourceValue($type)
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...
160
    {
161
        $method = 'getResourceValue_' . $type;
162
163
        if (method_exists($this, $method)) {
164
            return call_user_func([$this, $method]);
165
        }
166
167
        throw new InvalidConfigException("Resource type \"$type\" is not described in the Package");
168
    }
169
170
    /**
171
     * @param string $type
172
     * @throws InvalidConfigException
173
     * @return string
174
     */
175 View Code Duplication
    public function getResourceTitle($type)
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...
176
    {
177
        $method = 'getResourceTitle_' . $type;
178
        if (method_exists($this, $method)) {
179
            return call_user_func([$this, $method]);
180
        }
181
182
        throw new InvalidConfigException("Resource type \"$type\" is not described in the Package");
183
    }
184
185
    /**
186
     * @param $type
187
     * @throws InvalidConfigException
188
     * @return array|null
189
     */
190 View Code Duplication
    public function getOverusePrice($type)
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...
191
    {
192
        $method = 'getResourceOveruse_' . $type;
193
        if (method_exists($this, $method)) {
194
            return call_user_func([$this, $method]);
195
        }
196
197
        throw new InvalidConfigException("Overuse getter for resource type \"$type\" is not described in the Package");
198
    }
199
200
    /**
201
     * @param string $type
202
     * @return Part|null
203
     */
204
    public function getPartByType($type)
205
    {
206
        foreach ($this->parts as $part) {
207
            if ($part->model_type === $type) {
208
                return $part;
209
            }
210
        }
211
212
        return null;
213
    }
214
215
    /**
216
     * @param string $type
217
     * @return Resource|null
218
     */
219
    public function getResourceByType($type)
220
    {
221
        foreach ($this->_tariff->resources as $resource) {
222
            if ($resource->type === $type) {
223
                return $resource;
224
            }
225
        }
226
227
        return null;
228
    }
229
230
    /**
231
     * @param string $type
232
     * @return resource|null
233
     */
234
    public function getResourceByModelType($type)
235
    {
236
        foreach ($this->_tariff->resources as $resource) {
237
            if ($resource->model_type === $type) {
238
                return $resource;
239
            }
240
        }
241
242
        return null;
243
    }
244
245
    /**
246
     * @return array
247
     */
248
    public function getLocations()
249
    {
250
        return [
251
            1 => Yii::t('hipanel/server/order', 'Netherlands, Amsterdam'),
252
        ];
253
    }
254
255
    /**
256
     * @return string
257
     */
258
    public function getId()
259
    {
260
        if (!isset($this->_id)) {
261
            $this->_id = hash('crc32b', implode('_', ['server', 'order', uniqid()]));
262
        }
263
264
        return $this->_id;
265
    }
266
}
267