Completed
Push — master ( 7d6821...1d6bc5 )
by Dmitry
10:57
created

EnsureDeleteRelatedPosition::removeRelated()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
namespace hiqdev\yii2\cart\behaviors;
4
5
use hiqdev\yii2\cart\CartPositionInterface;
6
use hiqdev\yii2\cart\ShoppingCart;
7
use yii\base\Behavior;
8
use yz\shoppingcart\CartActionEvent;
9
10
class EnsureDeleteRelatedPosition extends Behavior
11
{
12
    public function events()
13
    {
14
        return [
15
            ShoppingCart::EVENT_BEFORE_POSITION_REMOVE => 'removeRelated',
16
        ];
17
    }
18
19
    public function removeRelated(CartActionEvent $event): void
20
    {
21
        /** @var ShoppingCart $cart */
22
        $cart = $event->sender;
23
        /** @var CartPositionInterface $position */
24
        $position = $event->position;
25
        if ($related = $cart->findRelatedFor($position)) {
26
            $cart->remove($related);
27
        }
28
    }
29
}
30