Completed
Push — master ( 2a4dc4...452aea )
by Scott
02:20
created

CartRepository::setInventory()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 21
rs 9.3142
cc 3
eloc 12
nc 3
nop 2
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
            ->with('items')
105
            ->firstOrFail();
106
    }
107
108
    /**
109
     * Set an item's quantity in the curent cart.
110
     *
111
     * @param  int
112
     * @param  int
113
     * @return \Bedard\Shop\Models\Cart
114
     */
115
    public function setInventory($inventoryId, $quantity)
116
    {
117
        $inventory = Inventory::findOrFail($inventoryId);
118
119
        if ($quantity <= 0) {
120
            return $this->deleteInventory($inventory->id);
121
        }
122
123
        $cart = $this->getCart();
124
        $item = CartItem::firstOrCreate([
125
            'cart_id' => $cart->id,
126
            'inventory_id' => $inventory->id,
127
        ]);
128
129
        $item->quantity = $quantity;
130
        if ($item->quantity > $inventory->quantity) {
131
            $item->quantity = $inventory->quantity;
132
        }
133
134
        return $item->save();
135
    }
136
}
137