Completed
Push — master ( 4720cc...98d1d1 )
by Dmitry
05:22
created

AbstractCartPosition   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 195
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 20
lcom 3
cbo 5
dl 0
loc 195
ccs 0
cts 51
cp 0
rs 10
c 0
b 0
f 0

13 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 6 2
A getPrice() 0 4 1
A setPrice() 0 4 1
A getCost() 0 8 2
getId() 0 1 ?
A getCalculationModel() 0 16 3
A getPurchaseModel() 0 10 2
A getValue() 0 4 1
A setValue() 0 4 1
A serialize() 0 4 1
A serializationMap() 0 9 1
A unserializationMap() 0 8 1
A unserialize() 0 15 4
1
<?php
2
/**
3
 * Finance module for HiPanel
4
 *
5
 * @link      https://github.com/hiqdev/hipanel-module-finance
6
 * @package   hipanel-module-finance
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2015-2017, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hipanel\modules\finance\cart;
12
13
use hipanel\modules\finance\models\CalculableModelInterface;
14
use hipanel\modules\finance\models\Calculation;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, hipanel\modules\finance\cart\Calculation.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
15
use hiqdev\hiart\ActiveRecord;
16
use hiqdev\yii2\cart\CartPositionInterface;
17
use hiqdev\yii2\cart\CartPositionTrait;
18
use Serializable;
19
use Yii;
20
use yii\base\InvalidConfigException;
21
22
/**
23
 * AbstractCartPosition represents position (item) in cart.
24
 * Holds:
25
 * - calculation object
26
 * - purchase object
27
 * - price for single item
28
 * - value for selected quantity
29
 *
30
 * @property Calculation $actionCalcModel
31
 */
32
abstract class AbstractCartPosition extends ActiveRecord implements CartPositionInterface, CalculableModelInterface, Serializable
33
{
34
    use CartPositionTrait;
35
36
    /**
37
     * @var string|array|Calculation
38
     *  - string: the action calculation model class name
39
     *  - array: array of options for [[Yii::createObject()]] call
40
     *  - object: the object that extends [[Calculation]] class and represents calculation of the specified object type
41
     */
42
    protected $_calculationModel;
43
44
    /**
45
     * @var string|array
46
     *  - string: the position purchase model class name
47
     *  - array: array of options for [[Yii::createObject()]] call
48
     */
49
    protected $_purchaseModel;
50
51
    /**
52
     * @var double the price of the 1 piece of the position
53
     */
54
    protected $_price;
55
56
    /**
57
     * @var double the full value of position
58
     */
59
    protected $_value;
60
61
    /**
62
     * {@inheritdoc}
63
     */
64
    public function init()
65
    {
66
        if ($this->_purchaseModel === null) {
67
            throw new InvalidConfigException('Purchase model is not defined. The position can not be ordered');
68
        }
69
    }
70
71
    /**
72
     * @return double
73
     */
74
    public function getPrice()
75
    {
76
        return $this->_price;
77
    }
78
79
    /**
80
     * Sets the [[price]].
81
     *
82
     * The $price will be casted to double
83
     *
84
     * @param mixed $price
85
     */
86
    public function setPrice($price)
87
    {
88
        $this->_price = (float) $price;
89
    }
90
91
    /**
92
     * {@inheritdoc}
93
     */
94
    public function getCost($withDiscount = true)
95
    {
96
        if ($withDiscount) {
97
            return $this->getValue();
98
        } else {
99
            return $this->getQuantity() * $this->getPrice();
100
        }
101
    }
102
103
    /**
104
     * The method returns the `crc32b` hash of a distinct condition
105
     * Example:.
106
     *
107
     * ```php
108
     *    return hash('crc32b', implode('_', ['domain', 'registration', $this->name]));
109
     * ```
110
     *
111
     * @return string
112
     */
113
    abstract public function getId();
114
115
    /**
116
     * @param array $options Options that override defaults on [[Yii::createObject()]]
117
     * @throws \yii\base\InvalidConfigException
118
     * @return Calculation
119
     */
120
    public function getCalculationModel($options = [])
121
    {
122
        if (!($this->_calculationModel instanceof Calculation)) {
123
            $config = ['position' => $this];
124
125
            if (is_string($this->_calculationModel)) {
126
                $config['class'] = $this->_calculationModel;
127
            }
128
129
            $this->_calculationModel = Yii::createObject(array_merge($config, $options));
130
        } else {
131
            $this->_calculationModel->synchronize();
132
        }
133
134
        return $this->_calculationModel;
135
    }
136
137
    /**
138
     * @param array $options Options that override defaults on [[Yii::createObject()]]
139
     * @throws \yii\base\InvalidConfigException
140
     * @return AbstractPurchase
141
     */
142
    public function getPurchaseModel($options = [])
143
    {
144
        $config = ['position' => $this];
145
146
        if (is_string($this->_purchaseModel)) {
147
            $config['class'] = $this->_purchaseModel;
148
        }
149
150
        return Yii::createObject(array_merge($config, $options));
151
    }
152
153
    /**
154
     * Returns the value of the domain.
155
     * @return double
156
     */
157
    public function getValue()
158
    {
159
        return $this->_value;
160
    }
161
162
    /**
163
     * Sets the [[value]].
164
     *
165
     * The $value will be casted to double
166
     *
167
     * @param mixed $value
168
     */
169
    public function setValue($value)
170
    {
171
        $this->_value = (float) $value;
172
    }
173
174
    public function serialize()
175
    {
176
        return serialize($this->serializationMap());
177
    }
178
179
    /**
180
     * Method stores map of attributes that must be serialized.
181
     *
182
     * @return array key is the attribute name where the value should be extracted
183
     * In case when the value shoule be assigned in a special way, use [[unserializationMap]]
184
     * to map key to a closure, that will set the value.
185
     *
186
     * @see unserializationMap
187
     */
188
    protected function serializationMap()
189
    {
190
        return [
191
            'attributes' => $this->getAttributes(),
192
            '_quantity' => $this->_quantity,
193
            '_price' => $this->_price,
194
            '_value' => $this->_value
195
        ];
196
    }
197
198
    /**
199
     * @return array
200
     * Key is the attribute name, value - the closure to unserialize the value
201
     */
202
    protected function unserializationMap()
203
    {
204
        return [
205
            'attributes' => function ($value) {
206
                $this->setAttributes($value, false);
207
            },
208
        ];
209
    }
210
211
    public function unserialize($serialized)
212
    {
213
        $map = $this->unserializationMap();
214
        $array = unserialize($serialized);
215
        foreach ($array as $key => $value) {
216
            if (!isset($map[$key])) {
217
                $this->{$key} = $value;
218
                continue;
219
            }
220
221
            if ($map[$key] instanceof \Closure) {
222
                $map[$key]($value);
223
            }
224
        }
225
    }
226
}
227