ManagePostsController   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 142
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 13
eloc 23
dl 0
loc 142
rs 10
c 1
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 3 1
A destroy() 0 6 1
A store_post() 0 3 1
A update() 0 5 1
A __construct() 0 8 2
A create_post() 0 3 1
A store() 0 5 1
A index() 0 5 1
A edit_post() 0 3 1
A update_post() 0 3 1
A edit() 0 5 1
A destroy_post() 0 3 1
1
<?php
2
3
namespace WebDevEtc\BlogEtc\Controllers\Admin;
4
5
use App\Http\Controllers\Controller;
6
use Exception;
7
use Illuminate\Contracts\View\Factory;
8
use Illuminate\Http\RedirectResponse;
9
use Illuminate\Routing\Redirector;
10
use Illuminate\View\View;
11
use RuntimeException;
12
use WebDevEtc\BlogEtc\Middleware\UserCanManageBlogPosts;
13
use WebDevEtc\BlogEtc\Models\Post;
14
use WebDevEtc\BlogEtc\Requests\CreateBlogEtcPostRequest;
15
use WebDevEtc\BlogEtc\Requests\DeleteBlogEtcPostRequest;
16
use WebDevEtc\BlogEtc\Requests\UpdateBlogEtcPostRequest;
17
use WebDevEtc\BlogEtc\Services\UploadsService;
18
19
/**
20
 * Class BlogEtcAdminController.
21
 */
22
class ManagePostsController extends Controller
23
{
24
    /**
25
     * @var UploadsService
26
     */
27
    private $uploadsService;
28
29
    /**
30
     * BlogEtcAdminController constructor.
31
     */
32
    public function __construct(UploadsService $uploadsService)
33
    {
34
        $this->middleware(UserCanManageBlogPosts::class);
35
36
        $this->uploadsService = $uploadsService;
37
38
        if (!is_array(config('blogetc'))) {
39
            throw new RuntimeException('The config/blogetc.php does not exist. Publish the vendor files for the BlogEtc package by running the php artisan publish:vendor command');
40
        }
41
    }
42
43
    /**
44
     * View all posts.
45
     *
46
     * @return mixed
47
     */
48
    public function index()
49
    {
50
        $posts = Post::orderBy('posted_at', 'desc')->paginate(10);
51
52
        return view('blogetc_admin::index', ['posts' => $posts]);
53
    }
54
55
    /**
56
     * Show form for creating new post.
57
     *
58
     * @return Factory|View
59
     */
60
    public function create()
61
    {
62
        return view('blogetc_admin::posts.add_post');
63
    }
64
65
    /**
66
     * @deprecated - use create() instead
67
     */
68
    public function create_post()
69
    {
70
        return $this->create();
71
    }
72
73
    /**
74
     * Save a new post.
75
     *
76
     * @throws Exception
77
     *
78
     * @return RedirectResponse|Redirector
79
     */
80
    public function store(CreateBlogEtcPostRequest $request)
81
    {
82
        $editUrl = $this->uploadsService->legacyStorePost($request);
83
84
        return redirect($editUrl);
85
    }
86
87
    /**
88
     * @deprecated use store() instead
89
     */
90
    public function store_post(CreateBlogEtcPostRequest $request)
91
    {
92
        return $this->store($request);
93
    }
94
95
    /**
96
     * Show form to edit post.
97
     *
98
     * @param $blogPostId
99
     *
100
     * @return mixed
101
     */
102
    public function edit($blogPostId)
103
    {
104
        $post = Post::findOrFail($blogPostId);
105
106
        return view('blogetc_admin::posts.edit_post', ['post' => $post]);
107
    }
108
109
    /**
110
     * @deprecated - use edit() instead
111
     */
112
    public function edit_post($blogPostId)
113
    {
114
        return $this->edit($blogPostId);
115
    }
116
117
    /**
118
     * Save changes to a post.
119
     *
120
     * This uses some legacy code. This will get refactored soon into something nicer.
121
     *
122
     * @param $blogPostId
123
     *
124
     * @throws Exception
125
     *
126
     * @return RedirectResponse|Redirector
127
     */
128
    public function update(UpdateBlogEtcPostRequest $request, $blogPostId)
129
    {
130
        $editUrl = $this->uploadsService->legacyUpdatePost($request, $blogPostId);
131
132
        return redirect($editUrl);
133
    }
134
135
    /**
136
     * @deprecated use update() instead
137
     */
138
    public function update_post(UpdateBlogEtcPostRequest $request, $blogPostId)
139
    {
140
        return $this->update($request, $blogPostId);
141
    }
142
143
    /**
144
     * Delete a post.
145
     *
146
     * @param $blogPostId
147
     *
148
     * @return mixed
149
     */
150
    public function destroy(DeleteBlogEtcPostRequest $request, $blogPostId)
151
    {
152
        $deletedPost = $this->uploadsService->legacyDestroyPost($request, $blogPostId);
153
154
        return view('blogetc_admin::posts.deleted_post')
155
            ->withDeletedPost($deletedPost);
156
    }
157
158
    /**
159
     * @deprecated - use destroy() instead
160
     */
161
    public function destroy_post(DeleteBlogEtcPostRequest $request, $blogPostId)
162
    {
163
        return $this->destroy($request, $blogPostId);
164
    }
165
}
166