CategoriesController::destroy()   B
last analyzed

Complexity

Conditions 5
Paths 12

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 11
nc 12
nop 2
1
<?php
2
3
namespace App\Http\Controllers\Backend;
4
5
use App\Models\Category;
6
use Illuminate\Http\Request;
7
use App\Http\Controllers\Controller;
8
use Lloople\Notificator\Notificator;
9
use App\Http\Resources\CategoryResource;
10
use App\Http\Requests\CategoryFormRequest;
11
use App\ViewModels\CategoryDetailViewModel;
12
13
class CategoriesController extends Controller
14
{
15
    private $pagination = 50;
16
17
    /**
18
     * Display a listing of the resource.
19
     *
20
     * @param \Illuminate\Http\Request $request
21
     * @return \Illuminate\Http\Response
22
     */
23
    public function index(Request $request)
0 ignored issues
show
Unused Code introduced by
The parameter $request 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...
24
    {
25
        return view('backend.categories.index');
26
    }
27
28
    /**
29
     * Show the form for creating a new resource.
30
     *
31
     * @return \Illuminate\Http\Response
32
     */
33
    public function create()
34
    {
35
        return view('backend.categories.edit', ['view' => new CategoryDetailViewModel(new Category())]);
36
    }
37
38
    /**
39
     * Store a newly created resource in storage.
40
     *
41
     * @param  \Illuminate\Http\Request $request
42
     *
43
     * @return \Illuminate\Http\RedirectResponse
44
     */
45 View Code Duplication
    public function store(CategoryFormRequest $request)
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...
46
    {
47
        $category = new Category();
48
        $category->name = $request->name;
0 ignored issues
show
Bug introduced by
The property name does not seem to exist in App\Http\Requests\CategoryFormRequest.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
49
        $category->slug = str_slug($request->name);
0 ignored issues
show
Documentation introduced by
The property name does not exist on object<App\Http\Requests\CategoryFormRequest>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
50
        $category->active = $request->has('active');
51
52
        $category->save();
53
54
        Notificator::success('Category created successfully');
55
56
        return redirect()->route('backend.categories.edit', $category);
0 ignored issues
show
Documentation introduced by
$category is of type object<App\Models\Category>, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
57
    }
58
59
    /**
60
     * Show the form for editing the specified resource.
61
     *
62
     * @param \App\Models\Category $category
63
     *
64
     * @return void
65
     */
66
    public function edit(Category $category)
67
    {
68
        return view('backend.categories.edit', ['view' => new CategoryDetailViewModel($category)]);
69
    }
70
71
    /**
72
     * Update the specified resource in storage.
73
     *
74
     * @param  \Illuminate\Http\Request $request
75
     * @param  \App\Models\Category $category
76
     *
77
     * @return \Illuminate\Http\RedirectResponse
78
     */
79 View Code Duplication
    public function update(CategoryFormRequest $request, Category $category)
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...
80
    {
81
        $category->name = $request->name;
0 ignored issues
show
Bug introduced by
The property name does not seem to exist in App\Http\Requests\CategoryFormRequest.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
82
        $category->slug = str_slug($request->name);
0 ignored issues
show
Documentation introduced by
The property name does not exist on object<App\Http\Requests\CategoryFormRequest>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
83
        $category->active = $request->has('active');
84
85
        $category->save();
86
87
        Notificator::success('Category edited successfully');
88
89
        return redirect()->route('backend.categories.edit', $category);
0 ignored issues
show
Documentation introduced by
$category is of type object<App\Models\Category>, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
90
    }
91
92
    /**
93
     * Remove the specified resource from storage.
94
     *
95
     * @param \Illuminate\Http\Request $request
96
     * @param  \App\Models\Category $category
97
     *
98
     * @return array
99
     * @throws \Exception
100
     */
101
    public function destroy(Request $request, Category $category)
102
    {
103
        if ($canDelete = $category->posts->count() === 0) {
104
            $category->delete();
105
        }
106
107
        $message = $canDelete
108
            ? 'Category deleted successfully.'
109
            : 'Cannot delete this category because it has posts.';
110
111
        if ($request->ajax()) {
112
            return ['result' => $canDelete, 'message' => $message];
113
        }
114
115
        $notificationType = $canDelete ? 'success' : 'error';
116
117
        Notificator::$notificationType($message);
118
119
        return redirect()->route('backend.categories.index');
120
    }
121
122 View Code Duplication
    public function resource(Request $request)
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...
123
    {
124
        $categories = Category::select('*');
125
126
        if ($request->has('q')) {
127
            $categories->where('name', 'like', "%{$request->q}%");
128
        }
129
130
        return CategoryResource::collection($categories->orderBy('name')->paginate($this->pagination));
131
    }
132
}
133