Completed
Push — master ( bb1191...96b974 )
by Scott
02:16
created

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