Completed
Push — master ( c47414...381b4d )
by Nicolas
02:55
created

PostController::destroy()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
rs 9.4285
cc 1
eloc 5
nc 1
nop 1
1
<?php namespace Modules\Blog\Http\Controllers\Admin;
2
3
use Modules\Blog\Entities\Post;
4
use Modules\Blog\Entities\Status;
5
use Modules\Blog\Http\Requests\CreatePostRequest;
6
use Modules\Blog\Http\Requests\UpdatePostRequest;
7
use Modules\Blog\Repositories\CategoryRepository;
8
use Modules\Blog\Repositories\PostRepository;
9
use Modules\Core\Http\Controllers\Admin\AdminBaseController;
10
use Modules\Media\Repositories\FileRepository;
11
12
class PostController extends AdminBaseController
13
{
14
    /**
15
     * @var PostRepository
16
     */
17
    private $post;
18
    /**
19
     * @var CategoryRepository
20
     */
21
    private $category;
22
    /**
23
     * @var FileRepository
24
     */
25
    private $file;
26
    /**
27
     * @var Status
28
     */
29
    private $status;
30
31
    public function __construct(
32
        PostRepository $post,
33
        CategoryRepository $category,
34
        FileRepository $file,
35
        Status $status
36
    ) {
37
        parent::__construct();
38
39
        $this->post = $post;
40
        $this->category = $category;
41
        $this->file = $file;
42
        $this->status = $status;
43
    }
44
45
    /**
46
     * Display a listing of the resource.
47
     *
48
     * @return \Illuminate\View\View
49
     */
50
    public function index()
51
    {
52
        $posts = $this->post->all();
53
54
        return view('blog::admin.posts.index', compact('posts'));
55
    }
56
57
    /**
58
     * Show the form for creating a new resource.
59
     *
60
     * @return \Illuminate\View\View
61
     */
62
    public function create()
63
    {
64
        $categories = $this->category->allTranslatedIn(app()->getLocale());
65
        $statuses = $this->status->lists();
66
        $this->assetPipeline->requireJs('ckeditor.js');
67
68
        return view('blog::admin.posts.create', compact('categories', 'statuses'));
69
    }
70
71
    /**
72
     * Store a newly created resource in storage.
73
     *
74
     * @param CreatePostRequest $request
75
     * @return \Illuminate\Http\RedirectResponse
76
     */
77
    public function store(CreatePostRequest $request)
78
    {
79
        $this->post->create($request->all());
80
81
        flash(trans('blog::messages.post created'));
82
83
        return redirect()->route('admin.blog.post.index');
84
    }
85
86
    /**
87
     * Show the form for editing the specified resource.
88
     *
89
     * @param Post $post
90
     * @return \Illuminate\View\View
91
     */
92
    public function edit(Post $post)
93
    {
94
        $thumbnail = $this->file->findFileByZoneForEntity('thumbnail', $post);
95
        $categories = $this->category->allTranslatedIn(app()->getLocale());
96
        $statuses = $this->status->lists();
97
        $this->assetPipeline->requireJs('ckeditor.js');
98
99
        return view('blog::admin.posts.edit', compact('post', 'categories', 'thumbnail', 'statuses'));
100
    }
101
102
    /**
103
     * Update the specified resource in storage.
104
     *
105
     * @param Post $post
106
     * @param UpdatePostRequest $request
107
     * @return \Illuminate\Http\RedirectResponse
108
     */
109
    public function update(Post $post, UpdatePostRequest $request)
110
    {
111
        $this->post->update($post, $request->all());
112
113
        flash(trans('blog::messages.post updated'));
114
115
        return redirect()->route('admin.blog.post.index');
116
    }
117
118
    /**
119
     * Remove the specified resource from storage.
120
     *
121
     * @param  Post $post
122
     * @return \Illuminate\Http\RedirectResponse
123
     */
124
    public function destroy(Post $post)
125
    {
126
        $post->tags()->detach();
127
128
        $this->post->destroy($post);
129
130
        flash(trans('blog::messages.post deleted'));
131
132
        return redirect()->route('admin.blog.post.index');
133
    }
134
}
135