Completed
Push — master ( b862dd...81b279 )
by Dmitry
05:28
created

Package::getResourceTitle_purpose()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

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 4
ccs 0
cts 4
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
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\finance\models\Resource;
15
use hipanel\modules\server\cart\Tariff;
16
use hipanel\modules\stock\models\Part;
17
use Yii;
18
use yii\base\InvalidConfigException;
19
use yii\base\Model;
20
21
/**
22
 * Class Package is a wrapper for [[Tariff]], its parts, resources and calculation of its price.
23
 * @property string $name
24
 */
25
class Package extends Model
26
{
27
    /**
28
     * @var string CRC32 temporary uniq id of the package
29
     */
30
    private $_id;
31
32
    /** @var Tariff */
33
    protected $_tariff;
34
35
    /** @var Part[] */
36
    public $parts = [];
37
38
    /** @var array */
39
    public $calculation;
40
41
    /**
42
     * @var
43
     */
44
    protected $_resources;
45
46
    public function init()
47
    {
48
        if (empty($this->parts)) {
49
            foreach ($this->getTariff()->resources as $resource) {
50
                if (isset($resource->part)) {
51
                    $this->parts[$resource->part->id] = $resource->part;
52
                }
53
            }
54
        }
55
    }
56
57
    /**
58
     * @param Tariff $tariff
59
     */
60
    public function setTariff($tariff)
61
    {
62
        $this->_tariff = $tariff;
63
    }
64
65
    /**
66
     * @throws InvalidConfigException
67
     * TODO: implement and get rid of many magic functions bellow
68
     */
69
    protected function initResources()
70
    {
71
        foreach ($this->_tariff->resources as $resource) {
72
            $id = $resource->id;
73
74
            $this->_resources[$id] = Yii::createObject([
75
                'class' => $this->buildResourceClass($resource),
76
                'resource' => $resource,
77
            ]);
78
        }
79
    }
80
81
    /**
82
     * @param $resource
83
     * @return string
84
     * TODO: implement and get rid of many magic functions bellow
85
     */
86
    protected function buildResourceClass($resource)
87
    {
88
        if ($this->parts[$resource->object_id]) {
89
            return 'yii\base\Object';
90
        } else {
91
            return 'yii\base\Object';
92
        }
93
    }
94
95
    /**
96
     * @return Tariff
97
     */
98
    public function getTariff()
99
    {
100
        return $this->_tariff;
101
    }
102
103
    protected function getResourceTitle_cpu()
104
    {
105
        return Yii::t('hipanel/server/order', 'CPU');
106
    }
107
108
    protected function getResourceValue_cpu()
109
    {
110
        $part = $this->getPartByType('cpu');
111
        preg_match('/((\d+) cores?)$/i', $part->partno, $matches);
112
        $matches[2] = $matches[2] === null ? 0 : $matches[2];
113
        return Yii::t('hipanel/server/order', '{0, plural, one{# core} other{# cores}}', $matches[2]);
0 ignored issues
show
Documentation introduced by
$matches[2] is of type integer|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...
114
    }
115
116
    protected function getResourceTitle_ram()
117
    {
118
        return Yii::t('hipanel/server/order', 'RAM');
119
    }
120
121
    protected function getResourceValue_ram()
122
    {
123
        $part = $this->getPartByType('ram');
124
        preg_match('/((\d{1,5}) MB)$/i', $part->partno, $matches);
125
        return Yii::t('yii', '{nFormatted} GB', ['nFormatted' => (int) $matches[2] / 1024]); // Gb
126
    }
127
128 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...
129
    {
130
        return [
131
            'price' => Yii::$app->formatter->asCurrency(4, Yii::$app->params['currency']),
132
            'unit' => Yii::t('yii', '{nFormatted} GB', ['nFormatted' => 1]),
133
        ];
134
    }
135
136
    protected function getResourceTitle_hdd()
137
    {
138
        return Yii::t('hipanel/server/order', 'SSD');
139
    }
140
141
    protected function getResourceValue_hdd()
142
    {
143
        $part = $this->getPartByType('hdd');
144
        preg_match('/((\d{1,5}) GB)$/i', $part->partno, $matches);
145
        return Yii::t('yii', '{nFormatted} GB', ['nFormatted' => (int) $matches[2]]); // Gb
146
    }
147
148 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...
149
    {
150
        // TODO: extract from overuse resource
151
        return [
152
            'price' => Yii::$app->formatter->asCurrency(0.2, Yii::$app->params['currency']),
153
            'unit' => Yii::t('yii', '{nFormatted} GB', ['nFormatted' => 1]),
154
        ];
155
    }
156
157
    protected function getResourceTitle_ip()
158
    {
159
        return Yii::t('hipanel/server/order', 'Dedicated IP');
160
    }
161
162
    protected function getResourceValue_ip()
163
    {
164
        return $this->getResourceByType('ip_num')->quantity;
165
    }
166
167
    protected function getResourceTitle_chassis()
168
    {
169
        return Yii::t('hipanel/server/order', 'Chassis');
170
    }
171
172
    protected function getResourceValue_chassis()
173
    {
174
        return $this->getPartByType('chassis')->partno;
175
    }
176
177 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...
178
    {
179
        // TODO: extract from overuse resource
180
        return [
181
            'price' => Yii::$app->formatter->asCurrency(3.5, Yii::$app->params['currency']),
182
            'unit' => Yii::t('yii', '{n} IP', ['n' => 1]),
183
        ];
184
    }
185
186
    protected function getResourceTitle_support_time()
187
    {
188
        return Yii::t('hipanel/server/order', '24/7 support');
189
    }
190
191
    protected function getResourceValue_support_time()
192
    {
193
        $quantity = $this->getResourceByType('support_time')->quantity;
194
        if ($quantity === 1) {
195
            return Yii::t('hipanel/server/order', 'Bronze');
196
        } elseif ($quantity === 1.5) {
197
            return Yii::t('hipanel/server/order', 'Silver');
198
        } elseif ($quantity === 2) {
199
            return Yii::t('hipanel/server/order', 'Gold');
200
        } elseif ($quantity === 3) {
201
            return Yii::t('hipanel/server/order', 'Platinum');
202
        } else {
203
            return Yii::t('hipanel/server/order', '{n, plural, one{# hour} other{# hours}}', ['n' => $quantity]);
204
        }
205
    }
206
207 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...
208
    {
209
        // TODO: extract from overuse resource
210
        return [
211
            'price' => Yii::$app->formatter->asCurrency(0.02, Yii::$app->params['currency']),
212
            'unit' => Yii::t('yii', '{nFormatted} GB', ['nFormatted' => 1]),
213
        ];
214
    }
215
216
    protected function getResourceTitle_traffic()
217
    {
218
        return Yii::t('hipanel/server/order', 'Traffic');
219
    }
220
221
    protected function getResourceValue_traffic()
222
    {
223
        return Yii::t('yii', '{nFormatted} GB',
224
            ['nFormatted' => $this->getResourceByType('server_traf_max')->quantity]);
225
    }
226
227
    protected function getResourceValue_speed()
228
    {
229
        return Yii::t('hipanel/server/order', '{n} Gbit/s', ['n' => 1]);
230
    }
231
232
    protected function getResourceTitle_speed()
233
    {
234
        return Yii::t('hipanel/server/order', 'Port speed');
235
    }
236
237
    protected function getResourceValue_panel()
238
    {
239
        $result = Yii::t('hipanel/server/order', 'No panel / {hipanelLink}', ['hipanelLink' => 'HiPanel']); // todo: add faq link
240
        if ($this->getResourceByType('isp5')->quantity > 0) {
241
            $result .= ' / ' . Yii::t('hipanel/server/order', 'ISP manager');
242
        }
243
244
        return $result;
245
    }
246
247
    protected function getResourceTitle_panel()
248
    {
249
        return Yii::t('hipanel/server/order', 'Control panel');
250
    }
251
252
    protected function getResourceValue_purpose()
253
    {
254
        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...
255
    }
256
257
    protected function getResourceTitle_purpose()
258
    {
259
        return Yii::t('hipanel/server/order', 'Purpose');
260
    }
261
262
    /**
263
     * @return float
264
     */
265
    public function getPrice()
266
    {
267
        return $this->calculation['value']['usd']['value'];
268
    }
269
270
    /**
271
     * @throws InvalidConfigException
272
     * @return string
273
     */
274
    public function getDisplayPrice()
275
    {
276
        return Yii::t('hipanel/server/order', '{price}/mo', [
277
            'price' => Yii::$app->formatter->asCurrency($this->getPrice(), Yii::$app->params['currency']),
278
        ]);
279
    }
280
281
    /**
282
     * @return string
283
     */
284
    public function getName()
285
    {
286
        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...
287
    }
288
289
    /**
290
     * @param string $type
291
     * @throws InvalidConfigException
292
     * @return mixed
293
     */
294 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...
295
    {
296
        $method = 'getResourceValue_' . $type;
297
298
        if (method_exists($this, $method)) {
299
            return call_user_func([$this, $method]);
300
        }
301
302
        throw new InvalidConfigException("Resource type \"$type\" is not described in the Package");
303
    }
304
305
    /**
306
     * @param string $type
307
     * @throws InvalidConfigException
308
     * @return string
309
     */
310 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...
311
    {
312
        $method = 'getResourceTitle_' . $type;
313
        if (method_exists($this, $method)) {
314
            return call_user_func([$this, $method]);
315
        }
316
317
        throw new InvalidConfigException("Resource type \"$type\" is not described in the Package");
318
    }
319
320
    /**
321
     * @param $type
322
     * @throws InvalidConfigException
323
     * @return array|null
324
     */
325 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...
326
    {
327
        $method = 'getResourceOveruse_' . $type;
328
        if (method_exists($this, $method)) {
329
            return call_user_func([$this, $method]);
330
        }
331
332
        throw new InvalidConfigException("Overuse getter for resource type \"$type\" is not described in the Package");
333
    }
334
335
    /**
336
     * @param string $type
337
     * @return Part|null
338
     */
339
    public function getPartByType($type)
340
    {
341
        foreach ($this->parts as $part) {
342
            if ($part->model_type === $type) {
343
                return $part;
344
            }
345
        }
346
347
        return null;
348
    }
349
350
    /**
351
     * @param string $type
352
     * @return Resource|null
353
     */
354
    public function getResourceByType($type)
355
    {
356
        foreach ($this->_tariff->resources as $resource) {
357
            if ($resource->type === $type) {
358
                return $resource;
359
            }
360
        }
361
362
        return null;
363
    }
364
365
    /**
366
     * @param string $type
367
     * @return resource|null
368
     */
369
    public function getResourceByModelType($type)
370
    {
371
        foreach ($this->_tariff->resources as $resource) {
372
            if ($resource->model_type === $type) {
373
                return $resource;
374
            }
375
        }
376
377
        return null;
378
    }
379
380
    /**
381
     * @return array
382
     */
383
    public function getLocations()
384
    {
385
        return [
386
            1 => Yii::t('hipanel/server/order', 'Netherlands, Amsterdam'),
387
        ];
388
    }
389
390
    /**
391
     * @return string
392
     */
393
    public function getId()
394
    {
395
        if (!isset($this->_id)) {
396
            $this->_id = hash('crc32b', implode('_', ['server', 'order', uniqid()]));
397
        }
398
399
        return $this->_id;
400
    }
401
}
402