|
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\logic\Calculator; |
|
15
|
|
|
use hipanel\modules\finance\models\Calculation; |
|
16
|
|
|
use hipanel\modules\finance\models\ServerResource; |
|
17
|
|
|
use hipanel\modules\finance\models\stubs\ServerResourceStub; |
|
18
|
|
|
use hipanel\modules\finance\models\Tariff; |
|
19
|
|
|
use Yii; |
|
20
|
|
|
use yii\base\InvalidConfigException; |
|
21
|
|
|
use yii\base\Model; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Class Package is a wrapper for [[Tariff]], its parts, resources and calculation of its price. |
|
25
|
|
|
* @property string $name |
|
26
|
|
|
*/ |
|
27
|
|
|
class Package extends Model |
|
28
|
|
|
{ |
|
29
|
|
|
/** |
|
30
|
|
|
* @var string CRC32 temporary uniq id of the package |
|
31
|
|
|
*/ |
|
32
|
|
|
private $_id; |
|
33
|
|
|
|
|
34
|
|
|
/** @var Tariff */ |
|
35
|
|
|
protected $_tariff; |
|
36
|
|
|
|
|
37
|
|
|
/** @var Calculation */ |
|
38
|
|
|
public $calculation; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @param Tariff $tariff |
|
42
|
|
|
*/ |
|
43
|
|
|
public function setTariff($tariff) |
|
44
|
|
|
{ |
|
45
|
|
|
$this->_tariff = $tariff; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @return Tariff |
|
50
|
|
|
*/ |
|
51
|
|
|
public function getTariff() |
|
52
|
|
|
{ |
|
53
|
|
|
return $this->_tariff; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @return float |
|
58
|
|
|
*/ |
|
59
|
|
|
public function getPrice() |
|
60
|
|
|
{ |
|
61
|
|
|
return $this->calculation->forCurrency($this->getTariff()->currency)->value; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @throws InvalidConfigException |
|
66
|
|
|
* @return string |
|
67
|
|
|
*/ |
|
68
|
|
|
public function getDisplayPrice() |
|
69
|
|
|
{ |
|
70
|
|
|
return Yii::t('hipanel:server:order', '{price}/mo', [ |
|
71
|
|
|
'price' => Yii::$app->formatter->asCurrency($this->getPrice(), Yii::$app->params['currency']), |
|
72
|
|
|
]); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @return string |
|
77
|
|
|
*/ |
|
78
|
|
|
public function getName() |
|
79
|
|
|
{ |
|
80
|
|
|
return $this->_tariff->name; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @return string tariff type |
|
85
|
|
|
*/ |
|
86
|
|
|
public function getType() |
|
87
|
|
|
{ |
|
88
|
|
|
return $this->_tariff->type; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @param $type |
|
93
|
|
|
* @return \hipanel\modules\finance\models\DomainResource|ServerResource|Resource |
|
94
|
|
|
*/ |
|
95
|
|
|
public function getResourceByType($type) |
|
96
|
|
|
{ |
|
97
|
|
|
return $this->_tariff->getResourceByType($type); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @param string $type |
|
102
|
|
|
* @param bool $stubWhenNotFound whether to return Resource Stub when |
|
103
|
|
|
* `$tariff` does not have a relevant resource |
|
104
|
|
|
* @return ServerResource|ServerResourceStub|null |
|
105
|
|
|
*/ |
|
106
|
|
|
public function getResourceByModelType($type, $stubWhenNotFound = true) |
|
107
|
|
|
{ |
|
108
|
|
|
foreach ($this->_tariff->resources as $resource) { |
|
109
|
|
|
if ($resource->model_type === $type) { |
|
110
|
|
|
return $resource; |
|
|
|
|
|
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
return $stubWhenNotFound ? $this->_tariff->getStubResource($type) : null; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* @return array |
|
119
|
|
|
*/ |
|
120
|
|
|
public function getLocations() |
|
121
|
|
|
{ |
|
122
|
|
|
$data = [ |
|
123
|
|
|
Tariff::TYPE_XEN => [ |
|
124
|
|
|
1 => Yii::t('hipanel:server:order', 'Netherlands, Amsterdam'), |
|
125
|
|
|
2 => Yii::t('hipanel:server:order', 'USA, Ashburn'), |
|
126
|
|
|
], |
|
127
|
|
|
Tariff::TYPE_OPENVZ => [ |
|
128
|
|
|
2 => Yii::t('hipanel:server:order', 'USA, Ashburn'), |
|
129
|
|
|
3 => Yii::t('hipanel:server:order', 'Netherlands, Amsterdam'), |
|
130
|
|
|
], |
|
131
|
|
|
]; |
|
132
|
|
|
|
|
133
|
|
|
return $data[$this->_tariff->type]; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* @return string |
|
138
|
|
|
*/ |
|
139
|
|
|
public function getId() |
|
140
|
|
|
{ |
|
141
|
|
|
if (!isset($this->_id)) { |
|
142
|
|
|
$this->_id = hash('crc32b', implode('_', ['server', 'order', uniqid()])); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
return $this->_id; |
|
146
|
|
|
} |
|
147
|
|
|
} |
|
148
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.