Completed
Pull Request — master (#3)
by ARCANEDEV
03:48 queued 01:31
created

PostsController::update()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
ccs 0
cts 9
cp 0
cc 1
eloc 7
nc 1
nop 2
crap 2
1
<?php namespace Arcanesoft\Blog\Http\Controllers\Admin;
2
3
use Arcanesoft\Blog\Entities\PostStatus;
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 Illuminate\Support\Facades\Log;
10
11
/**
12
 * Class     PostsController
13
 *
14
 * @package  Arcanesoft\Blog\Http\Controllers\Admin
15
 * @author   ARCANEDEV <[email protected]>
16
 */
17
class PostsController extends Controller
18
{
19
    /* ------------------------------------------------------------------------------------------------
20
     |  Properties
21
     | ------------------------------------------------------------------------------------------------
22
     */
23
    /**
24
     * The post model.
25
     *
26
     * @var \Arcanesoft\Blog\Models\Post
27
     */
28
    private $post;
29
30
    /* ------------------------------------------------------------------------------------------------
31
     |  Constructor
32
     | ------------------------------------------------------------------------------------------------
33
     */
34
    /**
35
     * Instantiate the controller.
36
     *
37
     * @param Post $post
38
     */
39
    public function __construct(Post $post)
40
    {
41
        parent::__construct();
42
43
        $this->post = $post;
44
45
        $this->setCurrentPage('blog-posts');
46
        $this->addBreadcrumbRoute('Posts', 'admin::blog.posts.index');
47
    }
48
49
    /* ------------------------------------------------------------------------------------------------
50
     |  Main Functions
51
     | ------------------------------------------------------------------------------------------------
52
     */
53
    /**
54
     * List the posts.
55
     *
56
     * @param  bool  $trashed
57
     *
58
     * @return \Illuminate\View\View
59
     */
60
    public function index($trashed = false)
61
    {
62
        $this->authorize('blog.posts.list');
0 ignored issues
show
Documentation Bug introduced by
The method authorize does not exist on object<Arcanesoft\Blog\H...\Admin\PostsController>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
63
64
        $posts = $this->post->with(['author', 'category']);
65
        $posts = $trashed
66
            ? $posts->onlyTrashed()->paginate(30)
67
            : $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...
68
69
        $this->setTitle($title = 'List of posts' . ($trashed ? ' - Trashed' : ''));
70
        $this->addBreadcrumb($title);
71
72
        return $this->view('admin.posts.list', compact('trashed', 'posts'));
73
    }
74
75
    /**
76
     * List the trashed posts.
77
     *
78
     * @return \Illuminate\View\View
79
     */
80
    public function trash()
81
    {
82
        return $this->index(true);
83
    }
84
85
    /**
86
     * Create a post.
87
     *
88
     * @return \Illuminate\View\View
89
     */
90
    public function create()
91
    {
92
        $this->authorize('blog.posts.create');
0 ignored issues
show
Documentation Bug introduced by
The method authorize does not exist on object<Arcanesoft\Blog\H...\Admin\PostsController>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
93
94
        $this->setTitle('Blog - Posts');
95
        $this->addBreadcrumb('Create post');
96
97
        $categories = Category::getSelectOptions();
98
        $tags       = Tag::getSelectOptions();
99
        $statuses   = PostStatus::all();
100
101
        return $this->view('admin.posts.create', compact('categories', 'tags', 'statuses'));
102
    }
103
104
    /**
105
     * Store the post.
106
     *
107
     * @param  \Arcanesoft\Blog\Http\Requests\Admin\Posts\CreatePostRequest  $request
108
     * @param  \Arcanesoft\Blog\Models\Post                                    $post
109
     *
110
     * @return \Illuminate\Http\RedirectResponse
111
     */
112
    public function store(CreatePostRequest $request, Post $post)
113
    {
114
        $this->authorize('blog.posts.create');
0 ignored issues
show
Documentation Bug introduced by
The method authorize does not exist on object<Arcanesoft\Blog\H...\Admin\PostsController>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
115
116
        $post->createOne($request->all());
117
118
        $message = "The post {$post->title} was created successfully !";
119
        Log::info($message, $post->toArray());
120
        $this->notifySuccess($message, 'Post created !');
121
122
        return redirect()->route('admin::blog.posts.index');
123
    }
124
125
    /**
126
     * Show a post.
127
     *
128
     * @param  \Arcanesoft\Blog\Models\Post  $post
129
     *
130
     * @return \Illuminate\View\View
131
     */
132
    public function show(Post $post)
133
    {
134
        $this->authorize('blog.posts.show');
0 ignored issues
show
Documentation Bug introduced by
The method authorize does not exist on object<Arcanesoft\Blog\H...\Admin\PostsController>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
135
136
        $post = $post->load(['author', 'category', 'tags']);
137
138
        $this->setTitle('Blog - Posts');
139
        $this->addBreadcrumb("Post - {$post->title}");
140
141
        return $this->view('admin.posts.show', compact('post'));
142
    }
143
144
    /**
145
     * Edit a post.
146
     *
147
     * @param  \Arcanesoft\Blog\Models\Post  $post
148
     *
149
     * @return \Illuminate\View\View
150
     */
151
    public function edit(Post $post)
152
    {
153
        $this->authorize('blog.posts.update');
0 ignored issues
show
Documentation Bug introduced by
The method authorize does not exist on object<Arcanesoft\Blog\H...\Admin\PostsController>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
154
155
        $this->setTitle('Blog - Posts');
156
        $this->addBreadcrumb('Edit post');
157
158
        $categories = Category::getSelectOptions();
159
        $tags       = Tag::getSelectOptions();
160
        $statuses   = PostStatus::all();
161
162
        return $this->view('admin.posts.edit', compact('post', 'categories', 'tags', 'statuses'));
163
    }
164
165
    public function update(UpdatePostRequest $request, Post $post)
166
    {
167
        $this->authorize('blog.posts.update');
0 ignored issues
show
Documentation Bug introduced by
The method authorize does not exist on object<Arcanesoft\Blog\H...\Admin\PostsController>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
168
169
        $post->updateOne($request->all());
170
171
        $message = "The post {$post->title} was updated successfully !";
172
        Log::info($message, $post->toArray());
173
        $this->notifySuccess($message, 'Post updated !');
174
175
        return redirect()->route('admin::blog.posts.show', [$post->id]);
176
    }
177
178
    public function publish(Post $post)
0 ignored issues
show
Unused Code introduced by
The parameter $post is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
179
    {
180
        $this->authorize('blog.posts.update');
0 ignored issues
show
Documentation Bug introduced by
The method authorize does not exist on object<Arcanesoft\Blog\H...\Admin\PostsController>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
181
    }
182
183
    public function restore(Post $post)
0 ignored issues
show
Unused Code introduced by
The parameter $post is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
184
    {
185
        $this->authorize('blog.posts.update');
0 ignored issues
show
Documentation Bug introduced by
The method authorize does not exist on object<Arcanesoft\Blog\H...\Admin\PostsController>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
186
    }
187
188
    public function delete(Post $post)
0 ignored issues
show
Unused Code introduced by
The parameter $post is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
189
    {
190
        $this->authorize('blog.posts.delete');
0 ignored issues
show
Documentation Bug introduced by
The method authorize does not exist on object<Arcanesoft\Blog\H...\Admin\PostsController>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
191
    }
192
}
193