Completed
Push — master ( 54947b...6a07df )
by Dmitry
04:48
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 0
Metric Value
c 0
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 Method

Rating   Name   Duplication   Size   Complexity  
A Package::getPrice() 0 4 1
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;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $resource; (resource|hipanel\modules...e\models\DomainResource) is incompatible with the return type documented by hipanel\modules\server\m...:getResourceByModelType of type hipanel\modules\finance\...ServerResourceStub|null.

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:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
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