Completed
Pull Request — master (#18)
by
unknown
12:24
created

ShoppingCart::getTotalMoney()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
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 Money\MoneyParser;
16
use Yii;
17
use yz\shoppingcart\CartActionEvent;
18
19
/**
20
 * Class ShoppingCart.
21
 * @property AbstractCartPosition[] $positions
22
 */
23
class ShoppingCart extends \yz\shoppingcart\ShoppingCart
24
{
25
    /**
26
     * @var AbstractCartPosition[]
27
     * TODO make local AbstractCartPosition
28
     */
29
    protected $_positions = [];
30
31
    /**
32
     * The cart module.
33
     */
34
    public $module;
35
36
    /**
37
     * @return integer
38
     */
39 1
    public function getCount()
40
    {
41 1
        return count($this->_positions);
42
    }
43
44 1
    public function getQuantity()
45
    {
46 1
        $count = 0;
47 1
        foreach ($this->_positions as $position) {
48 1
            $count += $position->getQuantity();
49
        }
50
51 1
        return $count;
52
    }
53
54 2
    public function getSubtotal()
55
    {
56 2
        return $this->getCost(false);
57
    }
58
59 2
    public function getTotal()
60
    {
61 2
        return $this->getCost(true);
62
    }
63
64 1
    public function getTotalMoney()
65
    {
66 1
        // TODO: decide how to get MoneyParser correctly
67
        return Yii::$container->get(MoneyParser::class)
68
            ->parse((string)$this->getTotal(), strtoupper($this->getCurrency()));
69 1
    }
70
71 1
    public function getDiscount()
72
    {
73
        return $this->getTotal() - $this->getSubtotal();
74
    }
75
76
    public function formatCurrency($sum, $currency = null)
77
    {
78
        return $sum !== null ? Yii::$app->formatter->format($sum, ['currency', $currency ?? $this->getCurrency()]) : '--';
79
    }
80
81
    /**
82
     * Sets cart from serialized string
83
     * @param string $serialized
84
     */
85
    public function setSerialized($serialized)
86
    {
87
        try {
88
            parent::setSerialized($serialized);
89
        } catch (\Exception $e) {
90
            Yii::error('Failed to unserlialize cart: ' . $e->getMessage(), __METHOD__);
91
            $this->_positions = [];
92
            $this->saveToSession();
93
        }
94
    }
95
96
    /**
97
     * Checks whether any of cart positions has error in `id` attribute.
98
     * @return boolean
99
     */
100
    public function hasErrors()
101
    {
102
        foreach ($this->_positions as $position) {
103
            if ($position->hasErrors('id')) {
104
                return true;
105
            }
106
        }
107
108
        return false;
109
    }
110
111
    /**
112
     * @param CartPositionInterface[] $positions
113
     */
114
    public function putPositions($positions)
115
    {
116
        foreach ($positions as $position) {
117
            if (isset($this->_positions[$position->getId()])) {
118
                if ($position instanceof DontIncrementQuantityWhenAlreadyInCart) {
119
                    continue;
120
                }
121
                $existingPosition = $this->_positions[$position->getId()];
122
                $existingPosition->setQuantity($existingPosition->getQuantity() + 1);
123
            } else {
124
                if ($position->getQuantity() <= 0) {
125
                    $position->setQuantity(1);
126
                }
127
                $this->_positions[$position->getId()] = $position;
128
            }
129
        }
130
131
        $this->trigger(self::EVENT_CART_CHANGE, new CartActionEvent([
132
            'action' => CartActionEvent::ACTION_POSITION_PUT,
133
        ]));
134
        if ($this->storeInSession)
135
            $this->saveToSession();
136
    }
137
138
    public function getCurrency(): ?string
139
    {
140
        if (!empty($this->_positions)) {
141
            return reset($this->_positions)->currency;
142
        }
143
144
        return Yii::$app->params['currency'];
145
    }
146
}
147