PriceListController::update()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 24
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 10
c 0
b 0
f 0
nc 2
nop 2
dl 0
loc 24
rs 9.9332
1
<?php
2
3
namespace App\Http\Controllers\Admin;
4
5
use App\Http\Controllers\Controller;
6
use App\Http\Requests\Admin\CreateUpdatePriceList;
7
use App\PriceCategory;
8
use App\PriceList;
9
10
class PriceListController extends Controller
11
{
12
    public function create(CreateUpdatePriceList $request)
13
    {
14
        $validated = $request->validated();
15
        $list = PriceList::create([
16
            'name' => $validated['name']
17
        ]);
18
        // On successfull creation redirect the browser to the object
19
        return redirect()
20
            ->route('admin.dependencies.prices.list.get', $list)
21
            ->with('status', 'Created ' . $list->name . '!');
22
    }
23
24
    public function get(PriceList $list)
25
    {
26
        return view('admin.dependencies.manage-price-list', [
27
            'list' => $list,
28
            'categories' => PriceCategory::orderBy('name', 'ASC')->get()
29
        ]);
30
    }
31
32
    public function update(PriceList $list, CreateUpdatePriceList $request)
33
    {
34
        $validated = $request->validated();
35
36
        // Set the new submitted (direct) list properties
37
        $list->name = $validated['name'];
38
        $list->save();
39
40
        // Detach all categories from the list
41
        $list->categories()->detach();
42
43
        // Only attach categories if submitted (not required)
44
        if($validated['categories']) {
45
            foreach( $validated['categories'] as $category )
46
            {
47
                // Attach only the given categories to the list
48
                $list->categories()->attach( $category['id'], ['priority' => $category['priority']] );
49
            }
50
        }
51
52
        // On successfull update redirect the browser to the list object
53
        return redirect()
54
            ->route('admin.dependencies.prices.list.get', $list)
55
            ->with('status', 'Update successfull!');
56
    }
57
58
    public function delete(PriceList $list)
59
    {
60
        $list->categories()->detach();
61
        $list->delete();
62
63
        // On successfull deletion redirect the browser to the overview
64
        return redirect()
65
            ->route('admin.dependencies.dashboard')
66
            ->with('status', 'Deleted list successfull!');
67
    }
68
}
69