1<?php
2
3namespace App\Services;
4
5use Illuminate\Support\Collection;
6use Illuminate\Session\SessionManager;
7
8class CartService {
9 const MINIMUM_QUANTITY = 1;
10 const DEFAULT_INSTANCE = 'shopping-cart';
11
12 protected $session;
13 protected $instance;
14
15 /**
16 * Constructs a new cart object.
17 *
18 * @param Illuminate\Session\SessionManager $session
19 */
20 public function __construct(SessionManager $session)
21 {
22 $this->session = $session;
23 }
24
25 /**
26 * Adds a new item to the cart.
27 *
28 * @param string $id
29 * @param string $name
30 * @param string $price
31 * @param string $quantity
32 * @param array $options
33 * @return void
34 */
35 public function add($id, $name, $price, $quantity, $options = []): void
36 {
37 $cartItem = $this->createCartItem($name, $price, $quantity, $options);
38
39 $content = $this->getContent();
40
41 if ($content->has($id)) {
42 $cartItem->put('quantity', $content->get($id)->get('quantity') + $quantity);
43 }
44
45 $content->put($id, $cartItem);
46
47 $this->session->put(self::DEFAULT_INSTANCE, $content);
48 }
49
50 /**
51 * Updates the quantity of a cart item.
52 *
53 * @param string $id
54 * @param string $action
55 * @return void
56 */
57 public function update(string $id, string $action): void
58 {
59 $content = $this->getContent();
60
61 if ($content->has($id)) {
62 $cartItem = $content->get($id);
63
64 switch ($action) {
65 case 'plus':
66 $cartItem->put('quantity', $content->get($id)->get('quantity') + 1);
67 break;
68 case 'minus':
69 $updatedQuantity = $content->get($id)->get('quantity') - 1;
70
71 if ($updatedQuantity < self::MINIMUM_QUANTITY) {
72 $updatedQuantity = self::MINIMUM_QUANTITY;
73 }
74
75 $cartItem->put('quantity', $updatedQuantity);
76 break;
77 }
78
79 $content->put($id, $cartItem);
80
81 $this->session->put(self::DEFAULT_INSTANCE, $content);
82 }
83 }
84
85 /**
86 * Removes an item from the cart.
87 *
88 * @param string $id
89 * @return void
90 */
91 public function remove(string $id): void
92 {
93 $content = $this->getContent();
94
95 if ($content->has($id)) {
96 $this->session->put(self::DEFAULT_INSTANCE, $content->except($id));
97 }
98 }
99
100 /**
101 * Clears the cart.
102 *
103 * @return void
104 */
105 public function clear(): void
106 {
107 $this->session->forget(self::DEFAULT_INSTANCE);
108 }
109
110 /**
111 * Returns the content of the cart.
112 *
113 * @return Illuminate\Support\Collection
114 */
115 public function content(): Collection
116 {
117 return is_null($this->session->get(self::DEFAULT_INSTANCE)) ? collect([]) : $this->session->get(self::DEFAULT_INSTANCE);
118 }
119
120 /**
121 * Returns total price of the items in the cart.
122 *
123 * @return string
124 */
125 public function total(): string
126 {
127 $content = $this->getContent();
128
129 $total = $content->reduce(function ($total, $item) {
130 return $total += $item->get('price') * $item->get('quantity');
131 });
132
133 return number_format($total, 2);
134 }
135
136 /**
137 * Returns the content of the cart.
138 *
139 * @return Illuminate\Support\Collection
140 */
141 protected function getContent(): Collection
142 {
143 return $this->session->has(self::DEFAULT_INSTANCE) ? $this->session->get(self::DEFAULT_INSTANCE) : collect([]);
144 }
145
146 /**
147 * Creates a new cart item from given inputs.
148 *
149 * @param string $name
150 * @param string $price
151 * @param string $quantity
152 * @param array $options
153 * @return Illuminate\Support\Collection
154 */
155 protected function createCartItem(string $name, string $price, string $quantity, array $options): Collection
156 {
157 $price = floatval($price);
158 $quantity = intval($quantity);
159
160 if ($quantity < self::MINIMUM_QUANTITY) {
161 $quantity = self::MINIMUM_QUANTITY;
162 }
163
164 return collect([
165 'name' => $name,
166 'price' => $price,
167 'quantity' => $quantity,
168 'options' => $options,
169 ]);
170 }
171}
172
1use App\Services\Transistor;
2
3$this->app->resolving(Transistor::class, function ($api, $app) {
4 // Called when container resolves objects of type "HelpSpot\API"...
5});
6
7$this->app->resolving(function ($object, $app) {
8 // Called when container resolves object of any type...
9});