Completed
Push — master ( cf838f...441ca2 )
by Dmitry
04:31
created

Package::getResourceValue_cpu()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 6
ccs 0
cts 5
cp 0
rs 9.4285
cc 1
eloc 4
nc 1
nop 0
crap 2
1
<?php
2
3
namespace hipanel\modules\server\models;
4
5
use hipanel\modules\server\cart\Tariff;
6
use hipanel\modules\stock\models\Part;
7
use Yii;
8
use yii\base\InvalidConfigException;
9
use yii\base\Model;
10
11
/**
12
 * Class Package is a wrapper for [[Tariff]], its parts, resources and calculation of its price.
13
 * @property string $name
14
 */
15
class Package extends Model
16
{
17
    /**
18
     * @var string CRC32 temporary uniq id of the package
19
     */
20
    private $_id;
21
22
    /** @var Tariff */
23
    protected $_tariff;
24
25
    /** @var Part[] */
26
    public $parts;
27
28
    /** @var array */
29
    public $calculation;
30
31
    /**
32
     * @var
33
     */
34
    protected $_resources;
35
36
    public function init()
37
    {
38
        //$this->initResources();
0 ignored issues
show
Unused Code Comprehensibility introduced by
84% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
39
    }
40
41
    /**
42
     * @param Tariff $tariff
43
     */
44
    public function setTariff($tariff)
45
    {
46
        $this->_tariff = $tariff;
47
    }
48
49
    /**
50
     * @throws InvalidConfigException
51
     * TODO: implement and get rid of many magic functions bellow
52
     */
53
    protected function initResources()
54
    {
55
        foreach ($this->_tariff->resources as $resource) {
56
            $id = $resource->id;
57
58
            $this->_resources[$id] = Yii::createObject([
59
                'class' => $this->buildResourceClass($resource),
60
                'resource' => $resource
61
            ]);
62
        }
63
    }
64
65
    /**
66
     * @param $resource
67
     * @return string
68
     * TODO: implement and get rid of many magic functions bellow
69
     */
70
    protected function buildResourceClass($resource)
71
    {
72
        if ($this->parts[$resource->object_id]) {
73
            return 'yii\base\Object';
74
        } else {
75
            return 'yii\base\Object';
76
        }
77
    }
78
79
    /**
80
     * @return Tariff
81
     */
82
    public function getTariff()
83
    {
84
        return $this->_tariff;
85
    }
86
87
    protected function getResourceTitle_cpu()
88
    {
89
        return Yii::t('hipanel/server', 'CPU');
90
    }
91
92
    protected function getResourceValue_cpu()
93
    {
94
        $part = $this->getPartByType('cpu');
95
        preg_match('/((\d+) cores?)$/i', $part->partno, $matches);
96
        return Yii::t('hipanel/server', '{0, plural, one{# core} other{# cores}}', $matches[2]);
0 ignored issues
show
Documentation introduced by
$matches[2] is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
97
    }
98
99
    protected function getResourceTitle_ram()
100
    {
101
        return Yii::t('hipanel/server', 'RAM');
102
    }
103
104
    protected function getResourceValue_ram()
105
    {
106
        $part = $this->getPartByType('ram');
107
        preg_match('/((\d{1,5}) MB)$/i', $part->partno, $matches);
108
        return Yii::t('yii', '{nFormatted} GB', ['nFormatted' => (int)$matches[2] / 1024]); // Gb
109
    }
110
111 View Code Duplication
    protected function getResourceOveruse_ram()
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...
112
    {
113
        return [
114
            'price' => Yii::$app->formatter->asCurrency(4, Yii::$app->params['currency']),
115
            'unit' => Yii::t('yii', '{nFormatted} GB', ['nFormatted' => 1])
116
        ];
117
    }
118
119
    protected function getResourceTitle_hdd()
120
    {
121
        return Yii::t('hipanel/server', 'SSD');
122
    }
123
124
    protected function getResourceValue_hdd()
125
    {
126
        $part = $this->getPartByType('hdd');
127
        preg_match('/((\d{1,5}) GB)$/i', $part->partno, $matches);
128
        return Yii::t('yii', '{nFormatted} GB', ['nFormatted' => (int)$matches[2]]); // Gb
129
    }
130
131 View Code Duplication
    protected function getResourceOveruse_hdd()
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...
132
    {
133
        // TODO: extract from overuse resource
134
        return [
135
            'price' => Yii::$app->formatter->asCurrency(0.2, Yii::$app->params['currency']),
136
            'unit' => Yii::t('yii', '{nFormatted} GB', ['nFormatted' => 1])
137
        ];
138
    }
139
140
    protected function getResourceTitle_ip()
141
    {
142
        return Yii::t('hipanel/server', 'Dedicated IP');
143
    }
144
145
    protected function getResourceValue_ip()
146
    {
147
        return $this->getResourceByType('ip_num')->quantity;
148
    }
149
150 View Code Duplication
    protected function getResourceOveruse_ip()
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...
151
    {
152
        // TODO: extract from overuse resource
153
        return [
154
            'price' => Yii::$app->formatter->asCurrency(3.5, Yii::$app->params['currency']),
155
            'unit' => Yii::t('yii', '{n} IP', ['n' => 1])
156
        ];
157
    }
158
159
    protected function getResourceTitle_support_time()
160
    {
161
        return Yii::t('hipanel/server', '24/7 support');
162
    }
163
164
    protected function getResourceValue_support_time()
165
    {
166
        $quantity = $this->getResourceByType('support_time')->quantity;
167
        if ($quantity == 1) {
168
            return Yii::t('hipanel/server', 'Bronze');
169
        } elseif ($quantity == 1.5) {
170
            return Yii::t('hipanel/server', 'Silver');
171
        } elseif ($quantity == 2) {
172
            return Yii::t('hipanel/server', 'Gold');
173
        } elseif ($quantity == 3) {
174
            return Yii::t('hipanel/server', 'Platinum');
175
        } else {
176
            return Yii::t('hipanel/server', '{n, plural, one{# hour} other {# hours}}', ['n' => $quantity]);
177
        }
178
    }
179
180 View Code Duplication
    protected function getResourceOveruse_traffic()
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...
181
    {
182
        // TODO: extract from overuse resource
183
        return [
184
            'price' => Yii::$app->formatter->asCurrency(0.02, Yii::$app->params['currency']),
185
            'unit' => Yii::t('yii', '{nFormatted} GB', ['nFormatted' => 1])
186
        ];
187
    }
188
189
    protected function getResourceTitle_traffic()
190
    {
191
        return Yii::t('hipanel/server', 'Traffic');
192
    }
193
194
    protected function getResourceValue_traffic()
195
    {
196
        return Yii::t('yii', '{nFormatted} GB',
197
            ['nFormatted' => $this->getResourceByType('server_traf_max')->quantity]);
198
    }
199
200
    protected function getResourceValue_speed()
201
    {
202
        return Yii::t('hipanel/server', '{n} Gbit/s', ['n' => 1]);
203
    }
204
205
    protected function getResourceTitle_speed()
206
    {
207
        return Yii::t('hipanel/server', 'Port speed');
208
    }
209
210
    protected function getResourceValue_panel()
211
    {
212
        $result = Yii::t('hipanel/server', 'No panel / {hipanelLink}',
213
            ['hipanelLink' => 'HiPanel']); // todo: add faq link
214
        if ($this->getResourceByType('isp5')->quantity > 0) {
215
            $result .= ' / ' . Yii::t('hipanel/server', 'ISP manager');
216
        }
217
218
        return $result;
219
    }
220
221
    protected function getResourceTitle_panel()
222
    {
223
        return Yii::t('hipanel/server', 'Control panel');
224
    }
225
226
    protected function getResourceValue_purpose()
227
    {
228
        return Yii::t('hipanel/server', $this->_tariff->label); // todo: translate possible labels
229
    }
230
231
    protected function getResourceTitle_purpose()
232
    {
233
        return Yii::t('hipanel/server', 'Purpose');
234
    }
235
236
237
    /**
238
     * @return float
239
     */
240
    public function getPrice()
241
    {
242
        return $this->calculation['value']['usd']['value'];
243
    }
244
245
    /**
246
     * @return string
247
     * @throws InvalidConfigException
248
     */
249
    public function getDisplayPrice()
250
    {
251
        return Yii::t('hipanel/server', '{price}/mo', [
252
            'price' => Yii::$app->formatter->asCurrency($this->getPrice(), Yii::$app->params['currency'])
253
        ]);
254
    }
255
256
    /**
257
     * @return string
258
     */
259
    public function getName()
260
    {
261
        return $this->_tariff->name;
262
    }
263
264
    /**
265
     * @param string $type
266
     * @return mixed
267
     * @throws InvalidConfigException
268
     */
269 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...
270
    {
271
        $method = 'getResourceValue_' . $type;
272
273
        if (method_exists($this, $method)) {
274
            return call_user_func([$this, $method]);
275
        }
276
277
        throw new InvalidConfigException("Resource type \"$type\" is not described in the Package");
278
    }
279
280
    /**
281
     * @param string $type
282
     * @return string
283
     * @throws InvalidConfigException
284
     */
285 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...
286
    {
287
        $method = 'getResourceTitle_' . $type;
288
        if (method_exists($this, $method)) {
289
            return call_user_func([$this, $method]);
290
        }
291
292
        throw new InvalidConfigException("Resource type \"$type\" is not described in the Package");
293
    }
294
295
    /**
296
     * @param $type
297
     * @return array|null
298
     * @throws InvalidConfigException
299
     */
300 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...
301
    {
302
        $method = 'getResourceOveruse_' . $type;
303
        if (method_exists($this, $method)) {
304
            return call_user_func([$this, $method]);
305
        }
306
307
        throw new InvalidConfigException("Overuse getter for resource type \"$type\" is not described in the Package");
308
309
    }
310
311
    /**
312
     * @param string $type
313
     * @return Part|null
314
     */
315
    public function getPartByType($type)
316
    {
317
        foreach ($this->parts as $part) {
318
            if ($part->model_type === $type) {
319
                return $part;
320
            }
321
        }
322
323
        return null;
324
    }
325
326
    /**
327
     * @param string $type
328
     * @return Resource|null
329
     */
330
    public function getResourceByType($type)
331
    {
332
        foreach ($this->_tariff->resources as $resource) {
333
            if ($resource->type === $type) {
334
                return $resource;
335
            }
336
        }
337
338
        return null;
339
    }
340
341
    /**
342
     * @param string $type
343
     * @return Resource|null
344
     */
345
    public function getResourceByModelType($type)
346
    {
347
        foreach ($this->_tariff->resources as $resource) {
348
            if ($resource->model_type === $type) {
349
                return $resource;
350
            }
351
        }
352
353
        return null;
354
    }
355
356
    /**
357
     * @return array
358
     */
359
    public function getLocations()
360
    {
361
        return [
362
            1 => Yii::t('hipanel/server', 'Netherlands, Amsterdam'),
363
        ];
364
    }
365
366
    /**
367
     * @return string
368
     */
369
    public function getId()
370
    {
371
        if (!isset($this->_id)) {
372
            $this->_id = hash('crc32b', implode('_', ['server', 'order', uniqid()]));
373
        }
374
375
        return $this->_id;
376
    }
377
}
378