Completed
Push — master ( dfd782...77e626 )
by Andrii
12:20
created

ShoppingCart::getCurrency()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 0
cts 1
cp 0
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 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
    /**
36
     * @return integer
37
     */
38
    public function getCount()
39 1
    {
40
        return count($this->_positions);
41 1
    }
42
43
    public function getQuantity()
44 1
    {
45
        $count = 0;
46 1
        foreach ($this->_positions as $position) {
47 1
            $count += $position->getQuantity();
48 1
        }
49
50
        return $count;
51 1
    }
52
53
    public function getSubtotal()
54 2
    {
55
        return $this->getCost(false);
56 2
    }
57
58
    public function getTotal()
59 2
    {
60
        return $this->getCost(true);
61 2
    }
62
63
    public function getDiscount()
64 1
    {
65
        return $this->getTotal() - $this->getSubtotal();
66 1
    }
67
68
    public function formatCurrency($sum, $currency = null)
69 1
    {
70
        return Yii::$app->formatter->format($sum, ['currency', $currency ?? $this->getCurrency()]);
71 1
    }
72
73
    /**
74
     * Sets cart from serialized string
75
     * @param string $serialized
76
     */
77
    public function setSerialized($serialized)
78
    {
79
        try {
80
            parent::setSerialized($serialized);
81
        } catch (\Exception $e) {
82
            Yii::error('Failed to unserlialize cart: ' . $e->getMessage(), __METHOD__);
83
            $this->_positions = [];
84
            $this->saveToSession();
85
        }
86
    }
87
88
    /**
89
     * Checks whether any of cart positions has error in `id` attribute.
90
     * @return boolean
91
     */
92
    public function hasErrors()
93
    {
94
        foreach ($this->_positions as $position) {
95
            if ($position->hasErrors('id')) {
96
                return true;
97
            }
98
        }
99
100
        return false;
101
    }
102
103
    /**
104
     * @param CartPositionInterface[] $positions
105
     */
106
    public function putPositions($positions)
107
    {
108
        foreach ($positions as $position) {
109
            if (isset($this->_positions[$position->getId()])) {
110
                if ($position instanceof DontIncrementQuantityWhenAlreadyInCart) {
111
                    continue;
112
                }
113
                $existingPosition = $this->_positions[$position->getId()];
114
                $existingPosition->setQuantity($existingPosition->getQuantity() + 1);
115
            } else {
116
                if ($position->getQuantity() <= 0) {
117
                    $position->setQuantity(1);
118
                }
119
                $this->_positions[$position->getId()] = $position;
120
            }
121
        }
122
123
        $this->trigger(self::EVENT_CART_CHANGE, new CartActionEvent([
124
            'action' => CartActionEvent::ACTION_POSITION_PUT,
125
        ]));
126
        if ($this->storeInSession)
127
            $this->saveToSession();
128
    }
129
130
    public function getCurrency(): ?string
131
    {
132
        if (!empty($this->_positions)) {
133
            return reset($this->_positions)->currency;
134
        }
135
136
        return null;
137
    }
138
}
139