Completed
Push — manage-articles ( 1fa998...1c60ef )
by Fèvre
02:30
created

ArticleController::update()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 2
nop 2
1
<?php
2
namespace Xetaravel\Http\Controllers\Admin\Blog;
3
4
use Illuminate\Http\RedirectResponse;
5
use Illuminate\Http\Request;
6
use Illuminate\View\View;
7
use Xetaravel\Http\Controllers\Admin\Controller;
8
use Xetaravel\Models\Article;
9
use Xetaravel\Models\Category;
10
use Xetaravel\Models\Repositories\ArticleRepository;
11
use Xetaravel\Models\Validators\ArticleValidator;
12
13
class ArticleController extends Controller
14
{
15
    /**
16
     * Show all articles.
17
     *
18
     * @return \Illuminate\View\View
19
     */
20 View Code Duplication
    public function index(): View
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...
21
    {
22
        $articles = Article::with('category', 'user')
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...
23
            ->paginate(config('xetaravel.pagination.blog.article_per_page'));
24
25
        $this->breadcrumbs->addCrumb('Manage Articles', route('admin.blog.article.index'));
0 ignored issues
show
Bug introduced by
The property breadcrumbs does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
26
27
        return view('Admin::Blog.article.index', ['articles' => $articles, 'breadcrumbs' => $this->breadcrumbs]);
28
    }
29
30
    /**
31
     * Show the article create form.
32
     *
33
     * @return \Illuminate\View\View
34
     */
35 View Code Duplication
    public function showCreateForm(): View
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...
36
    {
37
        $categories = Category::pluck('title', 'id');
38
39
        $breadcrumbs = $this->breadcrumbs
0 ignored issues
show
Unused Code introduced by
$breadcrumbs is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
40
            ->addCrumb('Manage Articles', route('admin.blog.article.index'))
41
            ->addCrumb("Create", route('admin.blog.article.create'));
42
43
        return view('Admin::Blog.article.create', ['categories' => $categories, 'breadcrumbs' => $this->breadcrumbs]);
44
    }
45
46
    /**
47
     * Handle an article create request for the application.
48
     *
49
     * @param \Illuminate\Http\Request $request
50
     *
51
     * @return \Illuminate\Http\RedirectResponse
52
     */
53
    public function create(Request $request): RedirectResponse
54
    {
55
        ArticleValidator::create($request->all())->validate();
56
57
        if (ArticleRepository::create($request->all())) {
58
            return redirect()
59
                ->route('admin.blog.article.index')
60
                ->with('success', 'Your article has been created successfully !');
61
        }
62
    }
63
64
    /**
65
     * Show the article update form.
66
     *
67
     * @param string $slug The slug of the article.
68
     * @param int $id The id of the article.
69
     *
70
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\View\View
0 ignored issues
show
Documentation introduced by
Should the return type not be RedirectResponse|View|\I...\Contracts\View\Factory?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
71
     */
72
    public function showUpdateForm(string $slug, int $id)
0 ignored issues
show
Unused Code introduced by
The parameter $slug 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...
73
    {
74
        $article = Article::with('category', 'user', 'comments')
0 ignored issues
show
Bug introduced by
The method where 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...
75
            ->where('id', $id)
76
            ->first();
77
78
        if (is_null($article)) {
79
            return redirect()
80
                ->route('admin.blog.article.index')
81
                ->with('danger', 'This article doesn\'t exist or has been deleted !');
82
        }
83
        $categories = Category::pluck('title', 'id');
84
85
        $breadcrumbs = $this->breadcrumbs
86
            ->addCrumb('Manage Articles', route('admin.blog.article.index'))
87
            ->addCrumb(
88
                "Update : " . e(str_limit($article->title, 30)),
89
                route(
90
                    'admin.blog.article.index',
91
                    ['slug' => $article->category->slug, 'id' => $article->category->id]
92
                )
93
            );
94
95
        return view('Admin::Blog.article.update', compact('article', 'breadcrumbs', 'categories'));
96
    }
97
98
    /**
99
     * Handle an article update request for the application.
100
     *
101
     * @param \Illuminate\Http\Request $request
102
     * @param int $id The id of the article.
103
     *
104
     * @return \Illuminate\Http\RedirectResponse
105
     */
106
    public function update(Request $request, int $id): RedirectResponse
107
    {
108
        ArticleValidator::update($request->all(), $id)->validate();
109
110
        $article = Article::find($id);
111
112
        if (ArticleRepository::update($request->all(), $article)) {
113
            return redirect()
114
                ->route('admin.blog.article.index')
115
                ->with('success', 'Your article has been updated successfully !');
116
        }
117
    }
118
119
    /**
120
     * Handle the delete request for the article.
121
     *
122
     * @param int $id The id of the article to delete.
123
     *
124
     * @return \Illuminate\Http\RedirectResponse
125
     */
126
    public function delete(int $id): RedirectResponse
127
    {
128
        $article = Article::find($id);
129
130
        if (is_null($article)) {
131
            return redirect()
132
                ->route('admin.blog.article.index')
133
                ->with('danger', 'This article doesn\'t exist or has already been deleted !');
134
        }
135
136
        if ($article->delete()) {
137
            return redirect()
138
                ->route('admin.blog.article.index')
139
                ->with('success', 'This article has been deleted successfully !');
140
        }
141
142
        return redirect()
143
            ->route('admin.blog.article.index')
144
            ->with('danger', 'An error occurred while deleting this article !');
145
    }
146
}
147