GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Push — master ( 4032bb...90a43b )
by Steeven
02:53
created

Cart::add()   B

Complexity

Conditions 9
Paths 10

Size

Total Lines 36
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 24
c 0
b 0
f 0
nc 10
nop 1
dl 0
loc 36
rs 8.0555
1
<?php
2
/**
3
 * This file is part of the O2System Framework package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 *
8
 * @author         Steeve Andrian Salim
9
 * @copyright      Copyright (c) Steeve Andrian Salim
10
 */
11
12
// ------------------------------------------------------------------------
13
14
namespace O2System\Framework\Services;
15
16
// ------------------------------------------------------------------------
17
18
use O2System\Spl\Patterns\Structural\Repository\AbstractRepository;
19
20
/**
21
 * Class Cart
22
 *
23
 * @package O2System\Framework\Services
24
 */
25
class Cart extends AbstractRepository
26
{
27
    /**
28
     * Cart::__construct
29
     */
30
    public function __construct()
31
    {
32
        if (empty($_SESSION[ 'o2system' ][ 'cart' ])) {
33
            $_SESSION[ 'o2system' ][ 'cart' ] = [];
34
        }
35
36
        $this->storage =& $_SESSION[ 'o2system' ][ 'cart' ];
37
    }
38
39
    // ------------------------------------------------------------------------
40
41
    /**
42
     * Cart::add
43
     *
44
     * @param array $item
45
     */
46
    public function add(array $item)
47
    {
48
        $item = array_merge([
49
            'id'       => null,
50
            'sku'      => null,
51
            'quantity' => 1,
52
            'price'    => 0,
53
            'discount' => 0,
54
            'name'     => null,
55
            'options'  => [],
56
        ], $item);
57
58
        // set sku
59
        $sku = empty($item[ 'sku' ]) ? $item[ 'id' ] : $item[ 'sku' ];
60
61
        // set sub-total
62
        $item[ 'subTotal' ][ 'price' ] = $item[ 'price' ] * $item[ 'quantity' ];
63
        $item[ 'subTotal' ][ 'discount' ] = 0;
64
65
        if (is_numeric($item[ 'discount' ])) {
66
            $item[ 'subTotal' ][ 'discount' ] = $item[ 'subTotal' ][ 'price' ] - $item[ 'discount' ];
67
        } elseif (is_string($item[ 'discount' ]) && strpos($item[ 'discount' ], '+') !== false) {
68
            $discounts = explode('+', $item[ 'discount' ]);
69
            if (count($discounts)) {
70
                $item[ 'subTotal' ][ 'discount' ] = $item[ 'subTotal' ][ 'price' ] * (intval(reset($discounts)) / 100);
71
                foreach (array_slice($discounts, 1) as $discount) {
72
                    $item[ 'subTotal' ][ 'discount' ] += $item[ 'subTotal' ][ 'discount' ] * (intval($discount) / 100);
73
                }
74
            }
75
        } elseif (is_string($item[ 'discount' ]) && strpos($item[ 'discount' ], '%') !== false) {
76
            $item[ 'subTotal' ][ 'discount' ] = $item[ 'subTotal' ][ 'price' ] * (intval($item[ 'discount' ]) / 100);
77
        }
78
79
        $item[ 'subTotal' ][ 'weight' ] = $item[ 'weight' ] * $item[ 'quantity' ];
80
81
        $this->storage[ $sku ] = $item;
82
    }
83
84
    // ------------------------------------------------------------------------
85
86
    /**
87
     * Cart::update
88
     *
89
     * @param string $sku
90
     * @param array  $item
91
     *
92
     * @return bool
93
     */
94
    public function update($sku, array $item)
95
    {
96
        if ($this->offsetExists($sku)) {
97
            $item = array_merge($this->offsetGet($sku), $item);
0 ignored issues
show
Bug introduced by
It seems like $this->offsetGet($sku) can also be of type null; however, parameter $array1 of array_merge() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

97
            $item = array_merge(/** @scrutinizer ignore-type */ $this->offsetGet($sku), $item);
Loading history...
98
99
            // update sub-total
100
            $item[ 'subTotal' ][ 'price' ] = $item[ 'price' ] * $item[ 'quantity' ];
101
            $item[ 'subTotal' ][ 'weight' ] = $item[ 'weight' ] * $item[ 'quantity' ];
102
103
            $this->storage[ $sku ] = $item;
104
105
            return true;
106
        }
107
108
        return false;
109
    }
110
111
    // ------------------------------------------------------------------------
112
113
    /**
114
     * Cart::getTotalWeight
115
     *
116
     * @return int
117
     */
118
    public function getTotalWeight()
119
    {
120
        $totalWeight = 0;
121
122
        if ($this->count()) {
123
            foreach ($this->storage as $id => $item) {
124
                if (isset($item[ 'subTotal' ][ 'weight' ])) {
125
                    $totalWeight += (int)$item[ 'weight' ];
126
                }
127
            }
128
        }
129
130
        return $totalWeight;
131
    }
132
133
    // ------------------------------------------------------------------------
134
135
    /**
136
     * Cart::getTotalPrice
137
     *
138
     * @return int
139
     */
140
    public function getTotalPrice()
141
    {
142
        $totalPrice = 0;
143
144
        if ($this->count()) {
145
            foreach ($this->storage as $id => $item) {
146
                if (isset($item[ 'subTotal' ][ 'price' ])) {
147
                    $totalPrice += (int)$item[ 'price' ];
148
                }
149
            }
150
        }
151
152
        return $totalPrice;
153
    }
154
155
    // ------------------------------------------------------------------------
156
157
    /**
158
     * Card::destroy
159
     */
160
    public function destroy()
161
    {
162
        unset($_SESSION[ 'o2system' ][ 'cart' ]);
163
        parent::destroy();
164
    }
165
}