Completed
Push — master ( 19e0d8...b3dedc )
by Andrii
08:56 queued 06:45
created

ShoppingCart::getSubtotal()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
ccs 0
cts 0
cp 0
cc 1
eloc 2
nc 1
nop 0
crap 2
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
17
/**
18
 * Class ShoppingCart.
19
 * @property AbstractCartPosition[] $positions
20
 */
21
class ShoppingCart extends \yz\shoppingcart\ShoppingCart
22
{
23
    /**
24
     * @var AbstractCartPosition[]
25
     */
26
    protected $_positions = [];
27
28
    /**
29
     * The cart module.
30
     */
31
    public $module;
32
33
    public $currency = 'usd';
34
35
    /**
36
     * @return integer
37
     */
38
    public function getCount()
39
    {
40
        return count($this->_positions);
41
    }
42
43 1
    public function getQuantity()
44
    {
45 1
        $count = 0;
46 1
        foreach ($this->_positions as $position) {
47
            $count += $position->getQuantity();
48
        }
49
50 1
        return $count;
51
    }
52
53
    public function getSubtotal()
54
    {
55
        return $this->getCost(false);
56
    }
57
58
    public function getTotal()
59
    {
60
        return $this->getCost(true);
61
    }
62
63
    public function getDiscount()
64
    {
65
        return $this->getTotal() - $this->getSubtotal();
66
    }
67
68 1
    public function formatCurrency($sum, $currency = null)
69
    {
70
        return Yii::$app->formatter->format($sum, ['currency', $currency ?: $this->currency]);
71 1
    }
72
73
    /**
74
     * Checks whether any of cart positions has error in `id` attribute.
75
     * @return boolean
76
     */
77
    public function hasErrors()
78
    {
79
        foreach ($this->_positions as $position) {
80
            if ($position->hasErrors('id')) {
81
                return true;
82
            }
83
        }
84
85
        return false;
86
    }
87
}
88