Issues (213)

src/models/Package.php (2 issues)

1
<?php
2
/**
3
 * Server module for HiPanel
4
 *
5
 * @link      https://github.com/hiqdev/hipanel-module-server
6
 * @package   hipanel-module-server
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2015-2019, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hipanel\modules\server\models;
12
13
use hipanel\modules\finance\models\Calculation;
14
use hipanel\modules\finance\models\ServerResource;
15
use hipanel\modules\finance\models\stubs\ServerResourceStub;
16
use hipanel\modules\finance\models\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 Calculation */
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
    /**
55
     * @return float
56
     */
57
    public function getPrice()
58
    {
59
        return $this->calculation->forCurrency($this->getTariff()->currency)->value;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->calculatio...iff()->currency)->value returns the type string which is incompatible with the documented return type double.
Loading history...
60
    }
61
62
    /**
63
     * @throws InvalidConfigException
64
     * @return string
65
     */
66
    public function getDisplayPrice()
67
    {
68
        return Yii::t('hipanel:server:order', '{price}/mo', [
69
            'price' => Yii::$app->formatter->asCurrency($this->getPrice(), Yii::$app->params['currency']),
70
        ]);
71
    }
72
73
    /**
74
     * @return string
75
     */
76
    public function getName()
77
    {
78
        return $this->_tariff->name;
79
    }
80
81
    /**
82
     * @return string tariff type
83
     */
84
    public function getType()
85
    {
86
        return $this->_tariff->type;
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);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->_tariff->getResourceByType($type) also could return the type hipanel\modules\finance\...tubs\ServerResourceStub which is incompatible with the documented return type hipanel\modules\finance\...ServerResource|resource.
Loading history...
96
    }
97
98
    /**
99
     * @param string $type
100
     * @param bool $stubWhenNotFound whether to return Resource Stub when
101
     * `$tariff` does not have a relevant resource
102
     * @return ServerResource|ServerResourceStub|null
103
     */
104
    public function getResourceByModelType($type, $stubWhenNotFound = true)
105
    {
106
        foreach ($this->_tariff->resources as $resource) {
107
            if ($resource->model_type === $type) {
108
                return $resource;
109
            }
110
        }
111
112
        return $stubWhenNotFound ? $this->_tariff->getStubResource($type) : null;
113
    }
114
115
    /**
116
     * @return array
117
     */
118
    public function getLocations()
119
    {
120
        $data = [
121
            Tariff::TYPE_XEN => [
122
                1 => Yii::t('hipanel:server:order', 'Netherlands, Amsterdam'),
123
                2 => Yii::t('hipanel:server:order', 'USA, Ashburn'),
124
            ],
125
            Tariff::TYPE_OPENVZ => [
126
                2 => Yii::t('hipanel:server:order', 'USA, Ashburn'),
127
                3 => Yii::t('hipanel:server:order', 'Netherlands, Amsterdam'),
128
            ],
129
        ];
130
131
        return $data[$this->_tariff->type];
132
    }
133
134
    /**
135
     * @return string
136
     */
137
    public function getId()
138
    {
139
        if (!isset($this->_id)) {
140
            $this->_id = hash('crc32b', implode('_', ['server', 'order', uniqid()]));
141
        }
142
143
        return $this->_id;
144
    }
145
}
146