TagController   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 29
c 2
b 0
f 0
dl 0
loc 112
rs 10
wmc 9

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A store() 0 11 2
A destroy() 0 9 1
A edit() 0 6 1
A create() 0 5 1
A index() 0 5 1
A update() 0 13 2
1
<?php
2
3
namespace App\Http\Controllers\Admin;
4
5
use App\Http\Controllers\Controller;
6
use App\Http\Requests\DestroyTagRequest;
7
use App\Http\Requests\StoreTagRequest;
8
use App\Http\Requests\UpdateTagRequest;
9
use App\Models\Tag;
10
use App\Services\TagFormFields;
11
use Illuminate\Http\Request;
12
13
class TagController extends Controller
14
{
15
    /**
16
     * Create a new controller instance.
17
     *
18
     * @return void
19
     */
20
    public function __construct()
21
    {
22
        $this->middleware('auth');
23
    }
24
25
    /**
26
     * Display a listing of the resource.
27
     *
28
     * @return \Illuminate\Http\Response
29
     */
30
    public function index()
31
    {
32
        $tags = Tag::all();
33
34
        return view('admin.tag.index')->withTags($tags);
0 ignored issues
show
Bug Best Practice introduced by
The expression return view('admin.tag.index')->withTags($tags) returns the type Illuminate\View\View which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
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
        $data = TagFormFields::formData();
45
46
        return view('admin.tag.create', $data);
0 ignored issues
show
Bug Best Practice introduced by
The expression return view('admin.tag.create', $data) returns the type Illuminate\View\View which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
47
    }
48
49
    /**
50
     * Store a newly created resource in storage.
51
     *
52
     * @param \App\Http\Requests\StoreTagRequest $request
53
     *
54
     * @return \Illuminate\Http\Response
55
     */
56
    public function store(StoreTagRequest $request)
57
    {
58
        $tag = new Tag();
59
        $fields = TagFormFields::fields();
60
        foreach (array_keys($fields) as $field) {
61
            $tag->$field = $request->get($field);
62
        }
63
        $tag->save();
64
65
        return redirect('/admin/tags')
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect('/admin/...y('tag' => $tag->tag))) also could return the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
66
                ->withSuccess(trans('messages.success.tag-created', ['tag' => $tag->tag]));
67
    }
68
69
    /**
70
     * Show the form for editing the specified resource.
71
     *
72
     * @param \Illuminate\Http\Request $request
73
     * @param int                      $id
74
     *
75
     * @return \Illuminate\Http\Response
76
     */
77
    public function edit(Request $request, $id)
78
    {
79
        $tag = Tag::findOrFail($id);
80
        $data = TagFormFields::formData($tag);
81
82
        return view('admin.tag.edit', $data);
0 ignored issues
show
Bug Best Practice introduced by
The expression return view('admin.tag.edit', $data) returns the type Illuminate\View\View which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
83
    }
84
85
    /**
86
     * Update the specified resource in storage.
87
     *
88
     * @param \App\Http\Requests\UpdateTagRequest $request
89
     * @param int                                 $id
90
     *
91
     * @return \Illuminate\Http\Response
92
     */
93
    public function update(UpdateTagRequest $request, $id)
94
    {
95
        $tag = Tag::findOrFail($id);
96
        $fields = TagFormFields::fields();
97
98
        foreach (array_keys($fields) as $field) {
99
            $tag->$field = $request->get($field);
100
        }
101
102
        $tag->save();
103
104
        return redirect("/admin/tags/$id/edit")
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect('/admin/...y('tag' => $tag->tag))) also could return the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
105
                    ->withSuccess(trans('messages.success.tag-updated', ['tag' => $tag->tag]));
106
    }
107
108
    /**
109
     * Remove the specified resource from storage.
110
     *
111
     * @param \App\Http\Requests\DestroyTagRequest $request
112
     * @param int                                  $id
113
     *
114
     * @return \Illuminate\Http\Response
115
     */
116
    public function destroy(DestroyTagRequest $request, $id)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

116
    public function destroy(/** @scrutinizer ignore-unused */ DestroyTagRequest $request, $id)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
117
    {
118
        $tag = Tag::findOrFail($id);
119
        $tag->posts()->detach();
120
        $tag->delete();
121
122
        return redirect()
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect()->route...y('tag' => $tag->tag))) also could return the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
123
            ->route('showtags')
124
            ->withSuccess(trans('messages.success.tag-deleted', ['tag' => $tag->tag]));
125
    }
126
}
127