Completed
Push — master ( ed48ab...3849c5 )
by Dmitry
11:59
created

Package::getType()   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 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 0
cts 3
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\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
    public function getStubResource($type)
57
    {
58
        return new ServerResourceStub([
59
            'tariff' => $this->_tariff,
60
            'type' => $type
61
        ]);
62
    }
63
64
    /**
65
     * @return float
66
     */
67
    public function getPrice()
68
    {
69
        return $this->calculation->forCurrency($this->getTariff()->currency)->value;
70
    }
71
72
    /**
73
     * @throws InvalidConfigException
74
     * @return string
75
     */
76
    public function getDisplayPrice()
77
    {
78
        return Yii::t('hipanel/server/order', '{price}/mo', [
79
            'price' => Yii::$app->formatter->asCurrency($this->getPrice(), Yii::$app->params['currency']),
80
        ]);
81
    }
82
83
    /**
84
     * @return string
85
     */
86
    public function getName()
87
    {
88
        return $this->_tariff->name;
89
    }
90
91
    /**
92
     * @return string tariff type
93
     */
94
    public function getType()
95
    {
96
        return $this->_tariff->type;
97
    }
98
99
    /**
100
     * @param $type
101
     * @return \hipanel\modules\finance\models\DomainResource|ServerResource|Resource
102
     */
103
    public function getResourceByType($type)
104
    {
105
        return $this->_tariff->getResourceByType($type);
106
    }
107
108
    /**
109
     * @param string $type
110
     * @return ServerResource|null
111
     */
112
    public function getResourceByModelType($type)
113
    {
114
        foreach ($this->_tariff->resources as $resource) {
115
            if ($resource->model_type === $type) {
116
                return $resource;
117
            }
118
        }
119
120
        return null;
121
    }
122
123
    /**
124
     * @return array
125
     */
126
    public function getLocations()
127
    {
128
        $data = [
129
            Tariff::TYPE_XEN => [
130
                1 => Yii::t('hipanel/server/order', 'Netherlands, Amsterdam'),
131
                2 => Yii::t('hipanel/server/order', 'USA, Ashburn'),
132
            ],
133
            Tariff::TYPE_OPENVZ => [
134
                2 => Yii::t('hipanel/server/order', 'USA, Ashburn'),
135
                3 => Yii::t('hipanel/server/order', 'Netherlands, Amsterdam'),
136
            ],
137
        ];
138
139
        return $data[$this->_tariff->type];
140
    }
141
142
    /**
143
     * @return string
144
     */
145
    public function getId()
146
    {
147
        if (!isset($this->_id)) {
148
            $this->_id = hash('crc32b', implode('_', ['server', 'order', uniqid()]));
149
        }
150
151
        return $this->_id;
152
    }
153
}
154