Completed
Push — master ( 786f42...798816 )
by Dmitry
13:21
created

ShoppingCart   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 4

Test Coverage

Coverage 72.72%

Importance

Changes 3
Bugs 1 Features 1
Metric Value
wmc 17
c 3
b 1
f 1
lcom 3
cbo 4
dl 0
loc 95
ccs 16
cts 22
cp 0.7272
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getCount() 0 4 1
A getQuantity() 0 9 2
A getSubtotal() 0 4 1
A getTotal() 0 4 1
A getDiscount() 0 4 1
A formatCurrency() 0 4 2
A hasErrors() 0 10 3
B putPositions() 0 23 6
1
<?php
2
3
/*
4
 * Cart module for Yii2
5
 *
6
 * @link      https://github.com/hiqdev/yii2-cart
7
 * @package   yii2-cart
8
 * @license   BSD-3-Clause
9
 * @copyright Copyright (c) 2015-2016, HiQDev (http://hiqdev.com/)
10
 */
11
12
namespace hiqdev\yii2\cart;
13
14
use hipanel\modules\finance\cart\AbstractCartPosition;
15
use Yii;
16
use yz\shoppingcart\CartActionEvent;
17
18
/**
19
 * Class ShoppingCart.
20
 * @property AbstractCartPosition[] $positions
21
 */
22
class ShoppingCart extends \yz\shoppingcart\ShoppingCart
23
{
24
    /**
25
     * @var AbstractCartPosition[]
26
     * TODO make local AbstractCartPosition
27
     */
28
    protected $_positions = [];
29
30
    /**
31
     * The cart module.
32
     */
33
    public $module;
34
35
    public $currency = 'usd';
36
37
    /**
38 1
     * @return integer
39
     */
40 1
    public function getCount()
41
    {
42
        return count($this->_positions);
43 1
    }
44
45 1
    public function getQuantity()
46 1
    {
47 1
        $count = 0;
48 1
        foreach ($this->_positions as $position) {
49
            $count += $position->getQuantity();
50 1
        }
51
52
        return $count;
53 2
    }
54
55 2
    public function getSubtotal()
56
    {
57
        return $this->getCost(false);
58 2
    }
59
60 2
    public function getTotal()
61
    {
62
        return $this->getCost(true);
63 1
    }
64
65 1
    public function getDiscount()
66
    {
67
        return $this->getTotal() - $this->getSubtotal();
68 1
    }
69
70 1
    public function formatCurrency($sum, $currency = null)
71
    {
72
        return Yii::$app->formatter->format($sum, ['currency', $currency ?: $this->currency]);
73
    }
74
75
    /**
76
     * Checks whether any of cart positions has error in `id` attribute.
77
     * @return boolean
78
     */
79
    public function hasErrors()
80
    {
81
        foreach ($this->_positions as $position) {
82
            if ($position->hasErrors('id')) {
83
                return true;
84
            }
85
        }
86
87
        return false;
88
    }
89
90
    /**
91
     * @param CartPositionInterface[] $positions
92
     */
93
    public function putPositions($positions)
94
    {
95
        foreach ($positions as $position) {
96
            if (isset($this->_positions[$position->getId()])) {
97
                if ($position instanceof ImmutableQuantityInterface) {
98
                    continue;
99
                }
100
                $existingPosition = $this->_positions[$position->getId()];
101
                $existingPosition->setQuantity($existingPosition->getQuantity() + 1);
102
            } else {
103
                if ($position->getQuantity() <= 0) {
104
                    $position->setQuantity(1);
105
                }
106
                $this->_positions[$position->getId()] = $position;
107
            }
108
        }
109
110
        $this->trigger(self::EVENT_CART_CHANGE, new CartActionEvent([
111
            'action' => CartActionEvent::ACTION_POSITION_PUT,
112
        ]));
113
        if ($this->storeInSession)
114
            $this->saveToSession();
115
    }
116
}
117