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

CategoriesController::trash()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 0
cts 4
cp 0
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php namespace Arcanesoft\Blog\Http\Controllers\Admin;
2
3
use Arcanesoft\Blog\Http\Requests\Admin\Categories\CreateCategoryRequest;
4
use Arcanesoft\Blog\Http\Requests\Admin\Categories\UpdateCategoryRequest;
5
use Arcanesoft\Blog\Models\Category;
6
use Illuminate\Support\Facades\Log;
7
8
/**
9
 * Class     CategoriesController
10
 *
11
 * @package  Arcanesoft\Blog\Http\Controllers\Admin
12
 * @author   ARCANEDEV <[email protected]>
13
 */
14
class CategoriesController extends Controller
15
{
16
    /* ------------------------------------------------------------------------------------------------
17
     |  Properties
18
     | ------------------------------------------------------------------------------------------------
19
     */
20
    /**
21
     * The category model.
22
     *
23
     * @var \Arcanesoft\Blog\Models\Category
24
     */
25
    private $category;
26
27
    /* ------------------------------------------------------------------------------------------------
28
     |  Constructor
29
     | ------------------------------------------------------------------------------------------------
30
     */
31
    /**
32
     * Instantiate the controller.
33
     *
34
     * @param  \Arcanesoft\Blog\Models\Category  $category
35
     */
36
    public function __construct(Category $category)
37
    {
38
        parent::__construct();
39
40
        $this->category = $category;
41
42
        $this->setCurrentPage('blog-categories');
43
        $this->addBreadcrumbRoute('Categories', 'admin::blog.categories.index');
44
    }
45
46
    /* ------------------------------------------------------------------------------------------------
47
     |  Main Functions
48
     | ------------------------------------------------------------------------------------------------
49
     */
50
    public function index($trashed = false)
51
    {
52
        $this->authorize('blog.categories.list');
0 ignored issues
show
Documentation Bug introduced by
The method authorize does not exist on object<Arcanesoft\Blog\H...n\CategoriesController>? 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...
53
54
        $categories = $this->category->with(['posts']);
55
        $categories = $trashed
56
            ? $categories->onlyTrashed()->paginate(30)
57
            : $categories->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...
58
59
        $this->setTitle($title = 'Blog - Categories');
60
        $this->addBreadcrumb('List all categories');
61
62
        return $this->view('admin.categories.list', compact('categories', 'trashed'));
63
    }
64
65
    public function trash()
66
    {
67
        return $this->index(true);
68
    }
69
70
    public function create()
71
    {
72
        $this->authorize('blog.categories.create');
0 ignored issues
show
Documentation Bug introduced by
The method authorize does not exist on object<Arcanesoft\Blog\H...n\CategoriesController>? 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...
73
74
        $this->setTitle('Blog - Categories');
75
        $this->addBreadcrumb('Create category');
76
77
        return $this->view('admin.categories.create');
78
    }
79
80
    public function store(CreateCategoryRequest $request, Category $category)
81
    {
82
        $this->authorize('blog.categories.create');
0 ignored issues
show
Documentation Bug introduced by
The method authorize does not exist on object<Arcanesoft\Blog\H...n\CategoriesController>? 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...
83
84
        $category->fill($request->only(['name']));
85
        $category->save();
86
87
        $message = "The category {$category->name} was created successfully !";
88
        Log::info($message, $category->toArray());
89
        $this->notifySuccess($message, 'Category created !');
90
91
        return redirect()->route('admin::blog.categories.index');
92
    }
93
94
    public function show(Category $category)
95
    {
96
        $this->authorize('blog.categories.show');
0 ignored issues
show
Documentation Bug introduced by
The method authorize does not exist on object<Arcanesoft\Blog\H...n\CategoriesController>? 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...
97
98
        $category->load(['posts']);
99
100
        $this->setTitle('Blog - Categories');
101
        $this->addBreadcrumb("Category - {$category->name}");
102
103
        return $this->view('admin.categories.show', compact('category'));
104
    }
105
106
    public function edit(Category $category)
107
    {
108
        $this->authorize('blog.categories.update');
0 ignored issues
show
Documentation Bug introduced by
The method authorize does not exist on object<Arcanesoft\Blog\H...n\CategoriesController>? 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...
109
110
        $this->setTitle('Blog - Categories');
111
        $this->addBreadcrumb('Update category');
112
113
        return $this->view('admin.categories.edit', compact('category'));
114
    }
115
116
    public function update(UpdateCategoryRequest $request, Category $category)
117
    {
118
        $this->authorize('blog.categories.update');
0 ignored issues
show
Documentation Bug introduced by
The method authorize does not exist on object<Arcanesoft\Blog\H...n\CategoriesController>? 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...
119
120
        $category->update($request->only(['name']));
121
122
        $message = "The category {$category->name} was updated successfully !";
123
        Log::info($message, $category->toArray());
124
        $this->notifySuccess($message, 'Category updated !');
125
126
        return redirect()->route('admin::blog.categories.show', [$category]);
127
    }
128
129
    public function restore(Category $category)
130
    {
131
        self::onlyAjax();
132
        $this->authorize('blog.categories.update');
0 ignored issues
show
Documentation Bug introduced by
The method authorize does not exist on object<Arcanesoft\Blog\H...n\CategoriesController>? 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...
133
134
        try {
135
            $category->restore();
136
137
            $message = "The category {$category->name} has been successfully restored !";
138
            Log::info($message, $category->toArray());
139
            $this->notifySuccess($message, 'Category restored !');
140
141
            return response()->json([
142
                'status'  => 'success',
143
                'message' => $message,
144
            ]);
145
        }
146
        catch (\Exception $e) {
147
            return response()->json([
148
                'status'  => 'error',
149
                'message' => $e->getMessage(),
150
            ]);
151
        }
152
    }
153
154
    public function delete(Category $category)
155
    {
156
        self::onlyAjax();
157
        $this->authorize('blog.categories.delete');
0 ignored issues
show
Documentation Bug introduced by
The method authorize does not exist on object<Arcanesoft\Blog\H...n\CategoriesController>? 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...
158
159
        try {
160
            $category->trashed()
161
                ? $category->forceDelete()
162
                : $category->delete();
163
164
            $message = "The category {$category->name} has been successfully deleted !";
165
            Log::info($message, $category->toArray());
166
            $this->notifySuccess($message, 'Category deleted !');
167
168
            return response()->json([
169
                'status'  => 'success',
170
                'message' => $message,
171
            ]);
172
        }
173
        catch(\Exception $e) {
174
            return response()->json([
175
                'status'  => 'error',
176
                'message' => $e->getMessage(),
177
            ]);
178
        }
179
    }
180
}
181