Completed
Push — master ( 95954c...739542 )
by Dmitry
17:54
created

Package::getStubResource()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
ccs 0
cts 7
cp 0
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
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\ServerResource;
15
use hipanel\modules\finance\models\stubs\ServerResourceStub;
16
use hipanel\modules\server\cart\Tariff;
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 array */
36
    public $calculation;
37
38
    /**
39
     * @param Tariff $tariff
40
     */
41
    public function setTariff($tariff)
42
    {
43
        $this->_tariff = $tariff;
44
    }
45
46
    /**
47
     * @return Tariff
48
     */
49
    public function getTariff()
50
    {
51
        return $this->_tariff;
52
    }
53
54
    public function getStubResource($type)
55
    {
56
        return new ServerResourceStub([
57
            'tariff' => $this->_tariff,
58
            'type' => $type
59
        ]);
60
    }
61
62
    /**
63
     * @return float
64
     */
65
    public function getPrice()
66
    {
67
        return $this->calculation['value']['usd']['value'];
68
    }
69
70
    /**
71
     * @throws InvalidConfigException
72
     * @return string
73
     */
74
    public function getDisplayPrice()
75
    {
76
        return Yii::t('hipanel/server/order', '{price}/mo', [
77
            'price' => Yii::$app->formatter->asCurrency($this->getPrice(), Yii::$app->params['currency']),
78
        ]);
79
    }
80
81
    /**
82
     * @return string
83
     */
84
    public function getName()
85
    {
86
        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...
87
    }
88
89
    /**
90
     * @param $type
91
     * @return \hipanel\modules\finance\models\DomainResource|ServerResource|Resource
92
     */
93
    public function getResourceByType($type)
94
    {
95
        return $this->_tariff->getResourceByType($type);
96
    }
97
98
    /**
99
     * @param string $type
100
     * @return ServerResource|null
101
     */
102
    public function getResourceByModelType($type)
103
    {
104
        foreach ($this->_tariff->resources as $resource) {
105
            if ($resource->model_type === $type) {
106
                return $resource;
107
            }
108
        }
109
110
        return null;
111
    }
112
113
    /**
114
     * @return array
115
     */
116
    public function getLocations()
117
    {
118
        return [
119
            1 => Yii::t('hipanel/server/order', 'Netherlands, Amsterdam'),
120
        ];
121
    }
122
123
    /**
124
     * @return string
125
     */
126
    public function getId()
127
    {
128
        if (!isset($this->_id)) {
129
            $this->_id = hash('crc32b', implode('_', ['server', 'order', uniqid()]));
130
        }
131
132
        return $this->_id;
133
    }
134
}
135