Completed
Push — master ( b877df...b3f08b )
by Christopher
01:17
created

PostController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Chriscreates\Blog\Controllers;
4
5
use Artesaos\SEOTools\Facades\OpenGraph;
6
use Artesaos\SEOTools\Facades\SEOMeta;
7
use Chriscreates\Blog\Post;
8
use Chriscreates\Blog\Requests\ValidatePostRequest;
9
use Illuminate\Http\Request;
10
use Illuminate\Support\Facades\Storage;
11
12
class PostController extends Controller
13
{
14
    /**
15
      * Instantiate a new controller instance.
16
      *
17
      * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
18
      */
19
    public function __construct()
20
    {
21
        $this->middleware('auth')->except('show');
22
    }
23
24
    /**
25
     * Display a listing of the resource.
26
     *
27
     * @return \Illuminate\Http\Response
28
     */
29
    public function index()
30
    {
31
        $posts = Post::withoutGlobalScope('tags')
32
        ->orderBy('created_at', 'desc')
33
        ->paginate(10);
34
35
        return view('admin.posts.posts', compact('posts'));
36
    }
37
38
    /**
39
     * Show the form for creating a new resource.
40
     *
41
     * @return \Illuminate\Http\Response
42
     */
43
    public function create()
44
    {
45
        return view('admin.posts.create-post');
46
    }
47
48
    /**
49
     * Store a newly created resource in storage.
50
     *
51
     * @param  \App\Http\Requests\ValidatePostRequest  $request
52
     * @return \Illuminate\Http\Response
53
     */
54 View Code Duplication
    public function store(ValidatePostRequest $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...
55
    {
56
        $post = Post::create($request->only([
57
            'title',
58
            'sub_title',
59
            'slug',
60
            'user_id',
61
            'excerpt',
62
            'content',
63
            'allow_comments',
64
            'allow_guest_comments',
65
        ]));
66
67
        return redirect()
68
        ->route('posts.edit', ['post' => $post->id])
69
        ->with('success', 'Post created.');
70
    }
71
72
    /**
73
     * Display the specified resource.
74
     *
75
     * @param  \Chriscreates\Blog\Post  $post
76
     * @return \Illuminate\Http\Response
77
     */
78
    public function show(Post $post)
79
    {
80
        $related_posts = Post::relatedByPostTags($post)
81
        ->take(3)
82
        ->get();
83
84
        SEOMeta::setTitle($post->title)
85
        ->setDescription($post->excerpt);
86
87
        OpenGraph::setTitle($post->title)
88
        ->setDescription($post->excerpt);
89
90
        return view('posts.show', compact(['post', 'related_posts']));
91
    }
92
93
    /**
94
     * Show the form for editing the specified resource.
95
     *
96
     * @param  \Chriscreates\Blog\Post  $post
97
     * @return \Illuminate\Http\Response
98
     */
99
    public function edit(Post $post)
100
    {
101
        return view('admin.posts.edit-post', compact('post'));
102
    }
103
104
    /**
105
     * Update the specified resource in storage.
106
     *
107
     * @param  \App\Http\Requests\ValidatePostRequest  $request
108
     * @param  \Chriscreates\Blog\Post  $post
109
     * @return \Illuminate\Http\Response
110
     */
111 View Code Duplication
    public function update(ValidatePostRequest $request, Post $post)
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...
112
    {
113
        $post->update($request->only([
114
            'title',
115
            'sub_title',
116
            'slug',
117
            'user_id',
118
            'excerpt',
119
            'content',
120
            'allow_comments',
121
            'allow_guest_comments',
122
        ]));
123
124
        return redirect()
125
        ->route('posts.edit', ['post' => $post->id])
126
        ->with('success', 'Post updated.');
127
    }
128
129
    /**
130
     * Remove the specified resource from storage.
131
     *
132
     * @param  \Chriscreates\Blog\Post  $post
133
     * @return \Illuminate\Http\Response
134
     */
135
    public function destroy(Post $post)
136
    {
137
        $post->delete();
138
139
        return redirect()
140
        ->route('posts.index')
141
        ->with('success', 'Post deleted.');
142
    }
143
}
144