EventCategoryController   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 146
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
eloc 29
c 1
b 0
f 0
dl 0
loc 146
ccs 38
cts 38
cp 1
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A edit() 0 3 1
A store() 0 17 2
A index() 0 10 1
A update() 0 10 1
A destroy() 0 6 1
A show() 0 3 1
A __construct() 0 3 1
A saveOnDb() 0 6 1
A create() 0 3 1
1
<?php
2
3
namespace DavideCasiraghi\LaravelEventsCalendar\Http\Controllers;
4
5
use DavideCasiraghi\LaravelEventsCalendar\Models\EventCategory;
6
use Illuminate\Http\Request;
7
use Illuminate\Support\Facades\DB;
8
use Illuminate\Support\Str;
9
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
10
use Validator;
11
12
class EventCategoryController extends Controller
13
{
14
    /* Restrict the access to this resource just to logged in users except show view */
15 12
    public function __construct()
16
    {
17 12
        $this->middleware('admin', ['except' => ['show']]);
18 12
    }
19
20
    /**
21
     * Display a listing of the resource.
22
     *
23
     * @return \Illuminate\View\View
24
     */
25 5
    public function index()
26
    {
27 5
        $eventCategories = EventCategory::latest()->paginate(20);
28
29
        // Countries available for translations
30 5
        $countriesAvailableForTranslations = LaravelLocalization::getSupportedLocales();
31
32 5
        return view('laravel-events-calendar::eventCategories.index', compact('eventCategories'))
33 5
            ->with('i', (request()->input('page', 1) - 1) * 20)
34 5
            ->with('countriesAvailableForTranslations', $countriesAvailableForTranslations);
35
    }
36
37
    /**
38
     * Show the form for creating a new resource.
39
     *
40
     * @return \Illuminate\View\View
41
     */
42 1
    public function create()
43
    {
44 1
        return view('laravel-events-calendar::eventCategories.create');
45
    }
46
47
    /**
48
     * Store a newly created resource in storage.
49
     *
50
     * @param  \Illuminate\Http\Request  $request
51
     * @return \Illuminate\Http\RedirectResponse
52
     */
53 2
    public function store(Request $request)
54
    {
55
56
        // Validate form datas
57 2
        $validator = Validator::make($request->all(), [
58 2
            'name' => 'required',
59
        ]);
60 2
        if ($validator->fails()) {
61 1
            return back()->withErrors($validator)->withInput();
62
        }
63
64 1
        $eventCategory = new EventCategory();
65
66 1
        $this->saveOnDb($request, $eventCategory);
67
68 1
        return redirect()->route('eventCategories.index')
69 1
                        ->with('success', __('laravel-events-calendar::messages.category_added_successfully'));
70
    }
71
72
    /**
73
     * Display the specified resource.
74
     *
75
     * @param  \DavideCasiraghi\LaravelEventsCalendar\Models\EventCategory  $eventCategory
76
     * @return \Illuminate\View\View
77
     */
78 1
    public function show(EventCategory $eventCategory)
79
    {
80 1
        return view('laravel-events-calendar::eventCategories.show', compact('eventCategory'));
81
    }
82
83
    /**
84
     * Show the form for editing the specified resource.
85
     *
86
     * @param  \DavideCasiraghi\LaravelEventsCalendar\Models\EventCategory  $eventCategory
87
     * @return \Illuminate\View\View
88
     */
89 1
    public function edit(EventCategory $eventCategory)
90
    {
91 1
        return view('laravel-events-calendar::eventCategories.edit', compact('eventCategory'));
92
    }
93
94
    /**
95
     * Update the specified resource in storage.
96
     *
97
     * @param  \Illuminate\Http\Request  $request
98
     * @param  \DavideCasiraghi\LaravelEventsCalendar\Models\EventCategory  $eventCategory
99
     * @return \Illuminate\Http\RedirectResponse
100
     */
101 2
    public function update(Request $request, EventCategory $eventCategory)
102
    {
103 2
        request()->validate([
104 2
            'name' => 'required',
105
        ]);
106
107 1
        $this->saveOnDb($request, $eventCategory);
108
109 1
        return redirect()->route('eventCategories.index')
110 1
                        ->with('success', __('laravel-events-calendar::messages.category_updated_successfully'));
111
    }
112
113
    /**
114
     * Remove the specified resource from storage.
115
     *
116
     * @param  \DavideCasiraghi\LaravelEventsCalendar\Models\EventCategory  $eventCategory
117
     * @return \Illuminate\Http\RedirectResponse
118
     */
119 1
    public function destroy(EventCategory $eventCategory)
120
    {
121 1
        $eventCategory->delete();
122
123 1
        return redirect()->route('eventCategories.index')
124 1
                        ->with('success', __('laravel-events-calendar::messages.category_deleted_successfully'));
125
    }
126
127
    // **********************************************************************
128
129
    /**
130
     * Return the single event category datas by cat id.
131
     *
132
     * @param  int $cat_id
133
     * @return \DavideCasiraghi\LaravelEventsCalendar\Models\EventCategory
134
     */
135
    /*public function eventcategorydata($cat_id)
136
    {
137
        $ret = DB::table('event_categories')->where('id', $cat_id)->first();
138
        //dump($ret);
139
140
        return $ret;
141
    }*/
142
143
    // **********************************************************************
144
145
    /**
146
     * Save/Update the record on DB.
147
     *
148
     * @param  \Illuminate\Http\Request  $request
149
     * @param \DavideCasiraghi\LaravelEventsCalendar\Models\EventCategory $eventCategory
150
     * @return void
151
     */
152 2
    public function saveOnDb(Request $request, EventCategory $eventCategory): void
153
    {
154 2
        $eventCategory->name = $request->get('name');
0 ignored issues
show
Bug introduced by
The property name does not seem to exist on DavideCasiraghi\LaravelE...ar\Models\EventCategory. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
155 2
        $eventCategory->slug = Str::slug($eventCategory->name, '-');
0 ignored issues
show
Bug introduced by
The property slug does not seem to exist on DavideCasiraghi\LaravelE...ar\Models\EventCategory. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
156
157 2
        $eventCategory->save();
158 2
    }
159
}
160