Passed
Push — master ( 6f620d...c9831c )
by Thiago
48s
created

Cart::getId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace MrPrompt\ShoppingCart;
4
5
use MrPrompt\ShoppingCart\Contracts\CartInterface;
6
use MrPrompt\ShoppingCart\Contracts\ItemInterface;
7
use SplObjectStorage;
8
9
class Cart extends SplObjectStorage implements CartInterface
10
{
11
    private $id;
12
13 10
    public function __construct(string $id, array $items = [])
14
    {
15 10
        $this->id = $id;
16
        
17 10
        array_walk($items, [ $this, 'attach']);
18 10
    }
19
20 1
    public function getId(): string
21
    {
22 1
        return $this->id;
23
    }
24
    
25 2
    public function addItem(ItemInterface $item): bool
26
    {
27 2
        if ($this->contains($item)) {
28 1
            return false;
29
        }
30
31 2
        $this->attach($item);
32
        
33 2
        return true;
34
    }
35
36 2
    public function removeItem(ItemInterface $item): bool
37
    {
38 2
        if ($this->contains($item)) {
39 1
            $this->detach($item);
40
41 1
            return true;
42
        }
43
44 1
        return false;
45
    }
46
47 1
    public function cleanUp(): bool
48
    {
49 1
        $this->removeAll($this);
50
51 1
        return $this->isEmpty();
52
    }
53
54 2
    public function isEmpty(): bool
55
    {
56 2
        return $this->count() === 0;
57
    }
58
}
59