Completed
Push — master ( 7f8270...8a9c98 )
by Scott
02:11
created

CartRepository::loadCart()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
1
<?php namespace Bedard\Shop\Repositories;
2
3
use Bedard\Shop\Models\Cart;
4
use Bedard\Shop\Models\CartItem;
5
use Bedard\Shop\Models\CartSettings;
6
use Bedard\Shop\Models\Inventory;
7
use Cookie;
8
use Session;
9
10
class CartRepository
11
{
12
    /**
13
     * @var string  Cart persistence key.
14
     */
15
    const CART_KEY = 'bedard_shop_cart';
16
17
    /**
18
     * @var \Bedard\Shop\Models\Cart
19
     */
20
    protected $cart = null;
21
22
    /**
23
     * Add an item to the curent cart.
24
     *
25
     * @param  int
26
     * @param  int
27
     * @return \Bedard\Shop\Models\Cart
28
     */
29
    public function addInventory($inventoryId, $quantity)
30
    {
31
        $inventory = Inventory::findOrFail($inventoryId);
32
33
        $cart = $this->getCart();
34
        $item = CartItem::firstOrCreate([
35
            'cart_id' => $cart->id,
36
            'inventory_id' => $inventory->id,
37
        ]);
38
39
        $item->quantity += $quantity;
40
        if ($item->quantity > $inventory->quantity) {
41
            $item->quantity = $inventory->quantity;
42
        }
43
44
        return $item->save();
45
    }
46
47
    /**
48
     * Create a new cart.
49
     *
50
     * @return \Bedard\Shop\Models\Cart
51
     */
52
    public function create()
53
    {
54
        $this->cart = Cart::create();
55
56
        Session::put(self::CART_KEY, $this->cart->token);
57
        Cookie::queue(self::CART_KEY, $this->cart->token, CartSettings::getLifespan());
58
59
        return $this->cart;
60
    }
61
62
    /**
63
     * Delete an inventory from the cart.
64
     *
65
     * @param  int
66
     * @return \Bedard\Shop\Models\Cart
67
     */
68
    public function deleteInventory($inventoryId)
69
    {
70
        $cart = $this->getCart();
71
72
        $item = $cart->items()->whereInventoryId($inventoryId)->first();
73
74
        if ($item) {
75
            return $item->delete();
76
        }
77
78
        return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type documented by Bedard\Shop\Repositories...sitory::deleteInventory of type Bedard\Shop\Models\Cart.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
79
    }
80
81
    /**
82
     * Get the current cart, or create one if none exists.
83
     *
84
     * @throws \Illuminate\Database\Eloquent\ModelNotFoundException
85
     * @return \Bedard\Shop\Models\Cart
86
     */
87
    public function getCart()
88
    {
89
        if ($this->cart !== null) {
90
            return $this->cart;
91
        }
92
93
        $token = Session::get(self::CART_KEY);
94
        if (! $token && Cookie::has(self::CART_KEY)) {
95
            $token = Cookie::get(self::CART_KEY);
96
        }
97
98
        if (! $token) {
99
            return $this->create();
100
        }
101
102
        return Cart::whereToken($token)
103
            ->isOpen()
104
            ->firstOrFail();
105
    }
106
107
    /**
108
     * Load the related cart data.
109
     *
110
     * @return \Bedard\Shop\Models\Cart
111
     */
112
    public function loadCart()
113
    {
114
        $this->getCart()->load('items.inventory.product');
115
116
        return $this->cart;
117
    }
118
119
    /**
120
     * Set an item's quantity in the curent cart.
121
     *
122
     * @param  int
123
     * @param  int
124
     * @return \Bedard\Shop\Models\Cart
125
     */
126
    public function setInventory($inventoryId, $quantity)
127
    {
128
        $inventory = Inventory::findOrFail($inventoryId);
129
130
        if ($quantity <= 0) {
131
            return $this->deleteInventory($inventory->id);
132
        }
133
134
        $cart = $this->getCart();
135
        $item = CartItem::firstOrCreate([
136
            'cart_id' => $cart->id,
137
            'inventory_id' => $inventory->id,
138
        ]);
139
140
        $item->quantity = $quantity;
141
        if ($item->quantity > $inventory->quantity) {
142
            $item->quantity = $inventory->quantity;
143
        }
144
145
        return $item->save();
146
    }
147
148
    /**
149
     * Update multiple inventories.
150
     *
151
     * @param  array  $inventories
152
     * @return void
153
     */
154
    public function updateInventories(array $inventories)
155
    {
156
        foreach ($inventories as $inventoryId => $quantity) {
157
            $this->setInventory($inventoryId, $quantity);
158
        }
159
    }
160
}
161