Completed
Push — master ( 3a2fd6...5b4baf )
by ARCANEDEV
04:23
created

PostsController   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 243
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 7

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 14
c 0
b 0
f 0
lcom 2
cbo 7
dl 0
loc 243
ccs 0
cts 85
cp 0
rs 10

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A index() 0 15 3
A trash() 0 4 1
A create() 0 13 1
A store() 0 10 1
A show() 0 11 1
A edit() 0 13 1
A update() 0 10 1
A publish() 0 6 1
A restore() 0 6 1
A delete() 0 6 1
A transNotification() 0 10 1
1
<?php namespace Arcanesoft\Blog\Http\Controllers\Admin;
2
3
use Arcanedev\LaravelApiHelper\Traits\JsonResponses;
4
use Arcanesoft\Blog\Http\Requests\Admin\Posts\CreatePostRequest;
5
use Arcanesoft\Blog\Http\Requests\Admin\Posts\UpdatePostRequest;
6
use Arcanesoft\Blog\Models\Category;
7
use Arcanesoft\Blog\Models\Post;
8
use Arcanesoft\Blog\Models\Tag;
9
use Arcanesoft\Blog\Policies\PostsPolicy;
10
use Illuminate\Support\Facades\Log;
11
12
/**
13
 * Class     PostsController
14
 *
15
 * @package  Arcanesoft\Blog\Http\Controllers\Admin
16
 * @author   ARCANEDEV <[email protected]>
17
 */
18
class PostsController extends Controller
19
{
20
    /* -----------------------------------------------------------------
21
     |  Traits
22
     | -----------------------------------------------------------------
23
     */
24
25
    use JsonResponses;
26
27
    /* -----------------------------------------------------------------
28
     |  Properties
29
     | -----------------------------------------------------------------
30
     */
31
32
    /**
33
     * The post model.
34
     *
35
     * @var \Arcanesoft\Blog\Models\Post
36
     */
37
    private $post;
38
39
    /* -----------------------------------------------------------------
40
     |  Constructor
41
     | -----------------------------------------------------------------
42
     */
43
44
    /**
45
     * PostsController constructor.
46
     *
47
     * @param  \Arcanesoft\Blog\Models\Post  $post
48
     */
49
    public function __construct(Post $post)
50
    {
51
        parent::__construct();
52
53
        $this->post = $post;
54
55
        $this->setCurrentPage('blog-posts');
56
        $this->addBreadcrumbRoute(trans('blog::posts.titles.posts'), 'admin::blog.posts.index');
57
    }
58
59
    /* -----------------------------------------------------------------
60
     |  Main Methods
61
     | -----------------------------------------------------------------
62
     */
63
64
    /**
65
     * List the posts.
66
     *
67
     * @param  bool  $trashed
68
     *
69
     * @return \Illuminate\View\View
70
     */
71
    public function index($trashed = false)
72
    {
73
        $this->authorize(PostsPolicy::PERMISSION_LIST);
74
75
        $posts = $this->post->with(['author', 'category']);
76
        $posts = $trashed
77
            ? $posts->onlyTrashed()->paginate(30)
78
            : $posts->paginate(30);
0 ignored issues
show
Bug introduced by
The method paginate does only exist in Illuminate\Database\Eloquent\Builder, but not in Illuminate\Database\Eloquent\Model.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
79
80
        $title = trans('blog::posts.titles.posts-list');
81
        $this->setTitle($title . ($trashed ? ' - '.trans('core::generals.trashed') : ''));
82
        $this->addBreadcrumb($title);
83
84
        return $this->view('admin.posts.list', compact('trashed', 'posts'));
85
    }
86
87
    /**
88
     * List the trashed posts.
89
     *
90
     * @return \Illuminate\View\View
91
     */
92
    public function trash()
93
    {
94
        return $this->index(true);
95
    }
96
97
    /**
98
     * Create a post.
99
     *
100
     * @return \Illuminate\View\View
101
     */
102
    public function create()
103
    {
104
        $this->authorize(PostsPolicy::PERMISSION_CREATE);
105
106
        $categories = Category::getSelectOptions();
107
        $tags       = Tag::getSelectOptions();
108
        $statuses   = Post::getStatuses();
109
110
        $this->setTitle($title = trans('blog::posts.titles.create-post'));
111
        $this->addBreadcrumb($title);
112
113
        return $this->view('admin.posts.create', compact('categories', 'tags', 'statuses'));
114
    }
115
116
    /**
117
     * Store the post.
118
     *
119
     * @param  \Arcanesoft\Blog\Http\Requests\Admin\Posts\CreatePostRequest  $request
120
     * @param  \Arcanesoft\Blog\Models\Post                                  $post
121
     *
122
     * @return \Illuminate\Http\RedirectResponse
123
     */
124
    public function store(CreatePostRequest $request, Post $post)
125
    {
126
        $this->authorize(PostsPolicy::PERMISSION_CREATE);
127
128
        $post->createOne($request->getValidatedInputs());
129
130
        $this->transNotification('created', ['title' => $post->title], $post->toArray());
131
132
        return redirect()->route('admin::blog.posts.show', [$post]);
133
    }
134
135
    /**
136
     * Show a post.
137
     *
138
     * @param  \Arcanesoft\Blog\Models\Post  $post
139
     *
140
     * @return \Illuminate\View\View
141
     */
142
    public function show(Post $post)
143
    {
144
        $this->authorize(PostsPolicy::PERMISSION_SHOW);
145
146
        $post = $post->load(['author', 'category', 'tags']);
147
148
        $this->setTitle('Blog - Posts');
149
        $this->addBreadcrumb("Post - {$post->title}");
150
151
        return $this->view('admin.posts.show', compact('post'));
152
    }
153
154
    /**
155
     * Edit a post.
156
     *
157
     * @param  \Arcanesoft\Blog\Models\Post  $post
158
     *
159
     * @return \Illuminate\View\View
160
     */
161
    public function edit(Post $post)
162
    {
163
        $this->authorize(PostsPolicy::PERMISSION_UPDATE);
164
165
        $this->setTitle('Blog - Posts');
166
        $this->addBreadcrumb('Edit post');
167
168
        $categories = Category::getSelectOptions();
169
        $tags       = Tag::getSelectOptions();
170
        $statuses   = Post::getStatuses();
171
172
        return $this->view('admin.posts.edit', compact('post', 'categories', 'tags', 'statuses'));
173
    }
174
175
    /**
176
     * Update the post.
177
     *
178
     * @param  \Arcanesoft\Blog\Http\Requests\Admin\Posts\UpdatePostRequest  $request
179
     * @param  \Arcanesoft\Blog\Models\Post                                  $post
180
     *
181
     * @return \Illuminate\Http\RedirectResponse
182
     */
183
    public function update(UpdatePostRequest $request, Post $post)
184
    {
185
        $this->authorize(PostsPolicy::PERMISSION_UPDATE);
186
187
        $post->updateOne($request->getValidatedInputs());
188
189
        $this->transNotification('updated', ['title' => $post->title], $post->toArray());
190
191
        return redirect()->route('admin::blog.posts.show', [$post]);
192
    }
193
194
    /**
195
     * Publish/Unpublish a post.
196
     *
197
     * @param  \Arcanesoft\Blog\Models\Post  $post
198
     *
199
     * @return \Illuminate\Http\JsonResponse
200
     */
201
    public function publish(Post $post)
202
    {
203
        $this->authorize(PostsPolicy::PERMISSION_UPDATE);
204
205
        // TODO: Complete the implementation
206
    }
207
208
    /**
209
     * Restore a trashed post.
210
     *
211
     * @param  \Arcanesoft\Blog\Models\Post  $post
212
     *
213
     * @return \Illuminate\Http\JsonResponse
214
     */
215
    public function restore(Post $post)
216
    {
217
        $this->authorize(PostsPolicy::PERMISSION_UPDATE);
218
219
        // TODO: Complete the implementation
220
    }
221
222
    /**
223
     * Delete a post.
224
     *
225
     * @param  \Arcanesoft\Blog\Models\Post  $post
226
     *
227
     * @return \Illuminate\Http\JsonResponse
228
     */
229
    public function delete(Post $post)
230
    {
231
        $this->authorize(PostsPolicy::PERMISSION_DELETE);
232
233
        // TODO: Complete the implementation
234
    }
235
236
    /* -----------------------------------------------------------------
237
     |  Other Methods
238
     | -----------------------------------------------------------------
239
     */
240
241
    /**
242
     * Notify with translation.
243
     *
244
     * @param  string  $action
245
     * @param  array   $replace
246
     * @param  array   $context
247
     *
248
     * @return string
249
     */
250
    protected function transNotification($action, array $replace = [], array $context = [])
251
    {
252
        $title   = trans("auth::posts.messages.{$action}.title");
253
        $message = trans("auth::posts.messages.{$action}.message", $replace);
254
255
        Log::info($message, $context);
256
        $this->notifySuccess($message, $title);
257
258
        return $message;
259
    }
260
}
261