PostController   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 111
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 31
c 2
b 0
f 0
dl 0
loc 111
rs 10
wmc 7

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A edit() 0 6 1
A create() 0 6 1
A store() 0 7 1
A update() 0 10 1
A index() 0 11 1
A destroy() 0 9 1
1
<?php
2
3
namespace App\Http\Controllers\Admin;
4
5
use App\Http\Controllers\Controller;
6
use App\Http\Requests\DestroyPostRequest;
7
use App\Http\Requests\StorePostRequest;
8
use App\Http\Requests\UpdatePostRequest;
9
use App\Models\Post;
10
use App\Services\PostFormFields;
11
use Illuminate\Http\Request;
12
13
class PostController 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(Request $request)
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

30
    public function index(/** @scrutinizer ignore-unused */ Request $request)

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...
31
    {
32
        $posts = Post::with('tags')
33
            ->orderBy('id', 'desc')
34
            ->paginate(config('admin.posts_per_page'));
35
36
        $data = [
37
            'posts' => $posts,
38
        ];
39
40
        return view('admin.post.index')->with($data);
0 ignored issues
show
Bug Best Practice introduced by
The expression return view('admin.post.index')->with($data) returns the type Illuminate\View\View which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
41
    }
42
43
    /**
44
     * Show the form for creating a new resource.
45
     *
46
     * @return \Illuminate\Http\Response
47
     */
48
    public function create()
49
    {
50
        $service = new PostFormFields();
51
        $data = $service->handle();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $data is correct as $service->handle() targeting App\Services\PostFormFields::handle() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
52
53
        return view('admin.post.create', $data);
0 ignored issues
show
Bug Best Practice introduced by
The expression return view('admin.post.create', $data) returns the type Illuminate\View\View which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
Bug introduced by
$data of type void is incompatible with the type Illuminate\Contracts\Support\Arrayable|array expected by parameter $data of view(). ( Ignorable by Annotation )

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

53
        return view('admin.post.create', /** @scrutinizer ignore-type */ $data);
Loading history...
54
    }
55
56
    /**
57
     * Store a newly created post in storage.
58
     *
59
     * @param \App\Http\Requests\StorePostRequest $request
60
     *
61
     * @return \Illuminate\Http\Response
62
     */
63
    public function store(StorePostRequest $request)
64
    {
65
        $post = Post::create($request->postFillData());
66
        $post->syncTags($request->get('tags', []));
67
68
        return redirect()->route('editpost', [$post])
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect()->route...success.post-created')) also could return the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
69
                            ->withSuccess(trans('messages.success.post-created'));
70
    }
71
72
    /**
73
     * Show the form for editing the specified post.
74
     *
75
     * @param int $id
76
     *
77
     * @return \Illuminate\Http\Response
78
     */
79
    public function edit(Request $request, $id)
80
    {
81
        $service = new PostFormFields($id);
82
        $data = $service->handle();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $data is correct as $service->handle() targeting App\Services\PostFormFields::handle() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
83
84
        return view('admin.post.edit')->with($data);
0 ignored issues
show
Bug introduced by
$data of type void is incompatible with the type array|string expected by parameter $key of Illuminate\View\View::with(). ( Ignorable by Annotation )

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

84
        return view('admin.post.edit')->with(/** @scrutinizer ignore-type */ $data);
Loading history...
Bug Best Practice introduced by
The expression return view('admin.post.edit')->with($data) returns the type Illuminate\View\View which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
85
    }
86
87
    /**
88
     * Update the specified resource in storage.
89
     *
90
     * @param \App\Http\Requests\UpdatePostRequest $request
91
     * @param int                                  $id
92
     *
93
     * @return \Illuminate\Http\Response
94
     */
95
    public function update(UpdatePostRequest $request, $id)
96
    {
97
        $post = Post::findOrFail($id);
98
        $post->fill($request->postFillData());
99
        $post->save();
100
        $post->syncTags($request->get('tags', []));
101
102
        return redirect()
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect()->back(...success.post-updated')) also could return the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
103
            ->back()
104
            ->withSuccess(trans('messages.success.post-updated'));
105
    }
106
107
    /**
108
     * Remove the specified resource from storage.
109
     *
110
     * @param \App\Http\Requests\DestroyPostRequest $request
111
     * @param int                                   $id
112
     *
113
     * @return \Illuminate\Http\Response
114
     */
115
    public function destroy(DestroyPostRequest $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

115
    public function destroy(/** @scrutinizer ignore-unused */ DestroyPostRequest $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...
116
    {
117
        $post = Post::findOrFail($id);
118
        $post->tags()->detach();
119
        $post->delete();
120
121
        return redirect()
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect()->route...success.post-deleted')) also could return the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
122
            ->route('admin.posts')
123
            ->withSuccess(trans('messages.success.post-deleted'));
124
    }
125
}
126