These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
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-2019, HiQDev (http://hiqdev.com/) |
||
9 | */ |
||
10 | |||
11 | namespace hipanel\modules\finance\cart; |
||
12 | |||
13 | use hipanel\modules\finance\logic\Calculator; |
||
14 | use hipanel\modules\finance\models\Calculation; |
||
0 ignored issues
–
show
|
|||
15 | use hipanel\modules\finance\models\Value; |
||
16 | use hiqdev\yii2\cart\ShoppingCart; |
||
17 | use Yii; |
||
18 | use yii\web\UnprocessableEntityHttpException; |
||
19 | use yz\shoppingcart\CartActionEvent; |
||
20 | |||
21 | /** |
||
22 | * Class CartCalculator provides API to calculate [[cart]] positions value. |
||
23 | * |
||
24 | * Usage: |
||
25 | * |
||
26 | * ```php |
||
27 | * $calculator = new CartCalculator([ |
||
28 | * 'cart' => $this->cart |
||
29 | * ]); |
||
30 | * |
||
31 | * $calculator->run(); // will calculate prices for all cart positions and update them |
||
32 | * ``` |
||
33 | * |
||
34 | * Also can be bound to some cart event as handler: |
||
35 | * |
||
36 | * ```php |
||
37 | * $cart->on(Cart::EVENT_UPDATE, [CartCalculator::class, 'handle']); |
||
38 | * ``` |
||
39 | */ |
||
40 | final class CartCalculator extends Calculator |
||
41 | { |
||
42 | /** |
||
43 | * @var AbstractCartPosition[] |
||
44 | */ |
||
45 | protected $models; |
||
46 | |||
47 | /** |
||
48 | * @var ShoppingCart |
||
49 | */ |
||
50 | public $cart; |
||
51 | |||
52 | /** |
||
53 | * @var CartActionEvent |
||
54 | */ |
||
55 | public $event; |
||
56 | /** |
||
57 | * @var string[] |
||
58 | */ |
||
59 | private $positionsBeingRemoved = []; |
||
60 | |||
61 | /** |
||
62 | * Creates the instance of the object and runs the calculation. |
||
63 | * |
||
64 | * @param CartActionEvent $event The event |
||
65 | * @void |
||
66 | */ |
||
67 | public static function handle($event) |
||
68 | { |
||
69 | /** @var ShoppingCart $cart */ |
||
70 | $cart = $event->sender; |
||
71 | |||
72 | $calculator = new static($cart); |
||
73 | if ($event->action === CartActionEvent::ACTION_BEFORE_REMOVE) { |
||
74 | $calculator->positionsBeingRemoved[] = $event->position->getId(); |
||
75 | } |
||
76 | |||
77 | /** @noinspection UnusedFunctionResultInspection */ |
||
78 | $calculator->execute(); |
||
79 | } |
||
80 | |||
81 | /** |
||
82 | * @param ShoppingCart $cart |
||
83 | */ |
||
84 | public function __construct(ShoppingCart $cart) |
||
85 | { |
||
86 | $this->cart = $cart; |
||
87 | |||
88 | parent::__construct($this->cart->positions); |
||
89 | } |
||
90 | |||
91 | /** |
||
92 | * {@inheritdoc} |
||
93 | */ |
||
94 | public function execute() |
||
95 | { |
||
96 | // Do not try to calculate position that is being removed |
||
97 | foreach ($this->positionsBeingRemoved as $id) { |
||
98 | unset($this->models[$id]); |
||
99 | } |
||
100 | |||
101 | try { |
||
102 | parent::execute(); |
||
103 | } catch (UnprocessableEntityHttpException $e) { |
||
104 | throw CartIsBrokenException::forCart( |
||
105 | $this->cart, |
||
106 | Yii::t('hipanel:finance', 'Failed to calculate cart: {reason}', ['reason' => $e->getMessage()]) |
||
107 | ); |
||
108 | } |
||
109 | |||
110 | $this->applyCalculations(); |
||
111 | return $this->calculations; |
||
112 | } |
||
113 | |||
114 | /** |
||
115 | * Updates positions using the calculations provided with [[getCalculation]]. |
||
116 | */ |
||
117 | private function applyCalculations() |
||
118 | { |
||
119 | foreach ($this->models as $position) { |
||
120 | $id = $position->id; |
||
0 ignored issues
–
show
The property
id does not exist on object<hipanel\modules\f...t\AbstractCartPosition> . Since you implemented __set , maybe consider adding a @property annotation.
Since your code implements the magic setter <?php
/**
* @property int $x
* @property int $y
* @property string $text
*/
class MyLabel
{
private $properties;
private $allowedProperties = array('x', 'y', 'text');
public function __get($name)
{
if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
return $properties[$name];
} else {
return null;
}
}
public function __set($name, $value)
{
if (in_array($name, $this->allowedProperties)) {
$properties[$name] = $value;
} else {
throw new \LogicException("Property $name is not defined.");
}
}
}
Since the property has write access only, you can use the @property-write annotation instead. Of course, you may also just have mistyped another name, in which case you should fix the error. See also the PhpDoc documentation for @property.
Loading history...
|
|||
121 | $calculation = $this->getCalculation($id); |
||
122 | if (!$calculation instanceof Calculation) { |
||
123 | Yii::error('Cart position "' . $position->getName() . '" was removed from the cart because of failed value calculation. Normally this should never happen.', 'hipanel.cart'); |
||
124 | $this->cart->removeById($position->id); |
||
0 ignored issues
–
show
The property
id does not exist on object<hipanel\modules\f...t\AbstractCartPosition> . Since you implemented __get , maybe consider adding a @property annotation.
Since your code implements the magic getter <?php
/**
* @property int $x
* @property int $y
* @property string $text
*/
class MyLabel
{
private $properties;
private $allowedProperties = array('x', 'y', 'text');
public function __get($name)
{
if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
return $properties[$name];
} else {
return null;
}
}
public function __set($name, $value)
{
if (in_array($name, $this->allowedProperties)) {
$properties[$name] = $value;
} else {
throw new \LogicException("Property $name is not defined.");
}
}
}
If the property has read access only, you can use the @property-read annotation instead. Of course, you may also just have mistyped another name, in which case you should fix the error. See also the PhpDoc documentation for @property.
Loading history...
|
|||
125 | break; |
||
126 | } |
||
127 | |||
128 | $value = $this->getValue($position, $calculation); |
||
129 | $this->ensureCurrencyIsNotConflictingWithCart($position, $value); |
||
130 | |||
131 | $position->setPrice($value->price); |
||
132 | $position->setValue($value->value); |
||
133 | $position->setCurrency($value->currency); |
||
134 | } |
||
135 | } |
||
136 | |||
137 | private function getValue(AbstractCartPosition $position, Calculation $calculation): Value |
||
138 | { |
||
139 | $currency = Yii::$app->params['currency']; |
||
140 | |||
141 | /** @var Value $value */ |
||
142 | $value = $calculation->forCurrency($currency); |
||
143 | if (!$value instanceof Value) { |
||
144 | Yii::error('Cart position "' . $position->getName() . '" was removed from the cart because calculation for currency "' . $value->currency . '" is not available', 'hipanel.cart'); |
||
145 | $this->cart->removeById($position->id); |
||
0 ignored issues
–
show
The property
id does not exist on object<hipanel\modules\f...t\AbstractCartPosition> . Since you implemented __get , maybe consider adding a @property annotation.
Since your code implements the magic getter <?php
/**
* @property int $x
* @property int $y
* @property string $text
*/
class MyLabel
{
private $properties;
private $allowedProperties = array('x', 'y', 'text');
public function __get($name)
{
if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
return $properties[$name];
} else {
return null;
}
}
public function __set($name, $value)
{
if (in_array($name, $this->allowedProperties)) {
$properties[$name] = $value;
} else {
throw new \LogicException("Property $name is not defined.");
}
}
}
If the property has read access only, you can use the @property-read annotation instead. Of course, you may also just have mistyped another name, in which case you should fix the error. See also the PhpDoc documentation for @property.
Loading history...
|
|||
146 | } |
||
147 | |||
148 | return $value; |
||
149 | } |
||
150 | |||
151 | private function ensureCurrencyIsNotConflictingWithCart(AbstractCartPosition $position, Value $value): void |
||
152 | { |
||
153 | if ($this->cart->getCurrency() && $value->currency !== $this->cart->getCurrency()) { |
||
154 | throw MultiCurrencyException::forPosition($position, $this->cart, Yii::t('cart', 'Sorry, but now it is impossible to add the position with different currencies to the cart. Pay the current order to add this item to the cart.')); |
||
155 | } |
||
156 | } |
||
157 | } |
||
158 |
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/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 beforeOtherDir/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: