Completed
Push — master ( c2ec5b...28d9e0 )
by Andrii
03:08
created

ShoppingCart   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 2

Test Coverage

Coverage 72.72%

Importance

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

7 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
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 1
    public function getCount()
39
    {
40 1
        return count($this->_positions);
41
    }
42
43 1
    public function getQuantity()
44
    {
45 1
        $count = 0;
46 1
        foreach ($this->_positions as $position) {
47 1
            $count += $position->getQuantity();
48 1
        }
49
50 1
        return $count;
51
    }
52
53 2
    public function getSubtotal()
54
    {
55 2
        return $this->getCost(false);
56
    }
57
58 2
    public function getTotal()
59
    {
60 2
        return $this->getCost(true);
61
    }
62
63 1
    public function getDiscount()
64
    {
65 1
        return $this->getTotal() - $this->getSubtotal();
66
    }
67
68 1
    public function formatCurrency($sum, $currency = null)
69
    {
70 1
        return Yii::$app->formatter->format($sum, ['currency', $currency ?: $this->currency]);
71
    }
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