laravel service example

Solutions on MaxInterview for laravel service example by the best coders in the world

showing results for - "laravel service example"
Sven
25 Sep 2016
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
queries leading to this page
laravel services examplelaravel make servicewhen to create a service in laravellaravel use service provider in controllerlaravel what is a servicewhat is laravel servicelaravel servicelaravel call a servicelaravel write serviceservice in laravellaravel service classes tutorialhow to create a service in laravelservice container in laravel examplemake new service in laravellaravel make service classmake service in laravel via artisancreate service in laravel 8laravel serviceswhat is a service laravelhow to create a service in laravel artisanservice provider example in laravelhow implement service pattern laravellaravel serviceworkercreate new service in laravelcreate servicers laravelcreate service laravelhow to make a service in laravelhow to get service class in laravel controllerservice container in laravel with examplelaravel make 3aservicelaravel lumen create servicelaravel how to create custom service functionslaravel call servicelaravel application as servicelaravel service layerservice provider in laravelcreate service in laravelwriting a service provider in laravelservice provider laravelwhat is a service provider in laravellaravel app servicewhat is service class and service method in laravellaravel call service from modellaravel 8 servicecreate a service class in laravelmake service class laravellaravel service provider examplelaravel service examplelaravel create serviceservice classes laravelhow to use service in laravellaravel construct servicelaravel create service classlaravel create service and inject to controllercreate service class in laravellaravel service 5claravel service patternlaravel service container examplelaravel service providercreate a laravel servicewhat is a service provider laravelwhat is laravel service classlaravel service classhow to make service in laravelservice laraveldefine laravel serviceservice layer laravelservice pattern laravellaravel service pattern laravel dailyhow to create service in laravellaravel service layaercreate a new service in laravelcreate service laravellaravel service example