Completed
Push — master ( ba992d...dc755b )
by Davide
07:03
created

EventCategoryController::saveOnDb()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use Validator;
6
use App\EventCategory;
7
use Illuminate\Support\Str;
8
use Illuminate\Http\Request;
9
use Illuminate\Support\Facades\DB;
10
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
11
12
class EventCategoryController extends Controller
13
{
14
    /* Restrict the access to this resource just to logged in users except show view */
15
    public function __construct()
16
    {
17
        $this->middleware('admin', ['except' => ['show']]);
18
    }
19
20
    /**
21
     * Display a listing of the resource.
22
     *
23
     * @return \Illuminate\Http\Response
24
     */
25
    public function index()
26
    {
27
        $eventCategories = EventCategory::latest()->paginate(20);
28
29
        // Countries available for translations
30
        $countriesAvailableForTranslations = LaravelLocalization::getSupportedLocales();
31
32
        return view('eventCategories.index', compact('eventCategories'))
0 ignored issues
show
Bug introduced by
The method with does only exist in Illuminate\View\View, but not in Illuminate\Contracts\View\Factory.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
33
            ->with('i', (request()->input('page', 1) - 1) * 20)
34
            ->with('countriesAvailableForTranslations', $countriesAvailableForTranslations);
35
    }
36
37
    /**
38
     * Show the form for creating a new resource.
39
     *
40
     * @return \Illuminate\Http\Response
41
     */
42
    public function create()
43
    {
44
        return view('eventCategories.create');
45
    }
46
47
    /**
48
     * Store a newly created resource in storage.
49
     *
50
     * @param  \Illuminate\Http\Request  $request
51
     * @return \Illuminate\Http\Response
52
     */
53 View Code Duplication
    public function store(Request $request)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
54
    {
55
56
        // Validate form datas
57
        $validator = Validator::make($request->all(), [
58
                'name' => 'required',
59
            ]);
60
        if ($validator->fails()) {
61
            return back()->withErrors($validator)->withInput();
62
        }
63
64
        $eventCategory = new EventCategory();
65
66
        $this->saveOnDb($request, $eventCategory);
67
68
        return redirect()->route('eventCategories.index')
69
                        ->with('success', __('messages.category_added_successfully'));
70
    }
71
72
    /**
73
     * Display the specified resource.
74
     *
75
     * @param  \App\EventCategory  $eventCategory
76
     * @return \Illuminate\Http\Response
77
     */
78
    public function show(EventCategory $eventCategory)
79
    {
80
        return view('eventCategories.show', compact('eventCategory'));
81
    }
82
83
    /**
84
     * Show the form for editing the specified resource.
85
     *
86
     * @param  \App\EventCategory  $eventCategory
87
     * @return \Illuminate\Http\Response
88
     */
89
    public function edit(EventCategory $eventCategory)
90
    {
91
        return view('eventCategories.edit', compact('eventCategory'));
92
    }
93
94
    /**
95
     * Update the specified resource in storage.
96
     *
97
     * @param  \Illuminate\Http\Request  $request
98
     * @param  \App\EventCategory  $eventCategory
99
     * @return \Illuminate\Http\Response
100
     */
101 View Code Duplication
    public function update(Request $request, EventCategory $eventCategory)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
102
    {
103
        request()->validate([
104
            'name' => 'required',
105
        ]);
106
107
        $this->saveOnDb($request, $eventCategory);
108
109
        return redirect()->route('eventCategories.index')
110
                        ->with('success', __('messages.category_updated_successfully'));
111
    }
112
113
    /**
114
     * Remove the specified resource from storage.
115
     *
116
     * @param  \App\EventCategory  $eventCategory
117
     * @return \Illuminate\Http\Response
118
     */
119
    public function destroy(EventCategory $eventCategory)
120
    {
121
        $eventCategory->delete();
122
123
        return redirect()->route('eventCategories.index')
124
                        ->with('success', __('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 \App\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 \App\EventCategory $eventCategory
150
     * @return void
151
     */
152
    public function saveOnDb($request, $eventCategory)
153
    {
154
        $eventCategory->name = $request->get('name');
155
        $eventCategory->slug = Str::slug($eventCategory->name, '-');
156
157
        $eventCategory->save();
158
    }
159
}
160