CategoryController::updateCategory()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.6666
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use Illuminate\Http\Request;
6
use App\Category;
7
use App\Account;
8
9
class CategoryController extends Controller
10
{
11
    use SearchTrait;
12
13
    public function __construct()
14
    {
15
        $this->middleware('auth');
16
    }
17
18 View Code Duplication
    public function showCategories(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...
19
    {
20
        $categories = Category::orderBy('name', 'asc');
21
22
        $results = $this->search($categories, $request->input('type'), $request->input('search'));
23
24
        if (false === is_null($results)) {
25
            $request->session()->flash(
26
                'results',
27
                trans_choice(
28
                    'categories.message.search',
29
                    $results,
30
                    ['number' => $results]
31
                )
32
            );
33
            $request->session()->flash('search', $request->input('search'));
34
            $request->session()->flash('type', $request->input('type'));
35
        }
36
37
        return view('category/categories', ['categories' => $categories->paginate(20)]);
38
    }
39
40
    public function addCategory(Request $request)
41
    {
42
        if (false === empty($request->id)) {
43
            return $this->updateCategory($request, $request->id);
44
        } else {
45
            $this->validate($request, [
46
                'name' => 'required|max:100|unique:categories,name',
47
                'icon' => 'required',
48
                'validity' => 'required|integer|min:1|max:1000',
49
            ]);
50
            $category = new Category;
51
            $category->name = mb_convert_case($request->name, MB_CASE_TITLE, 'UTF-8');
0 ignored issues
show
Documentation introduced by
The property name does not exist on object<App\Category>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write 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.");
        }
    }

}

Since the property has write access only, you can use the @property-write 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...
52
            $category->icon = $request->icon;
0 ignored issues
show
Documentation introduced by
The property icon does not exist on object<App\Category>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write 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.");
        }
    }

}

Since the property has write access only, you can use the @property-write 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...
53
            $category->validity = $request->validity;
0 ignored issues
show
Documentation introduced by
The property validity does not exist on object<App\Category>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write 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.");
        }
    }

}

Since the property has write access only, you can use the @property-write 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...
54
            $category->created_by = $request->user()->id;
0 ignored issues
show
Documentation introduced by
The property created_by does not exist on object<App\Category>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write 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.");
        }
    }

}

Since the property has write access only, you can use the @property-write 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...
55
            $category->save();
56
            return redirect()->back()->with(
57
                'status',
58
                trans('categories.message.add', ['category' => $category->name])
0 ignored issues
show
Documentation introduced by
The property name does not exist on object<App\Category>. 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...
59
            );
60
        }
61
    }
62
63
    public function updateCategory(Request $request, $id)
64
    {
65
        $this->validate($request, [
66
            'name' => 'required|max:100',
67
            'icon' => 'required',
68
            'validity' => 'required|digits_between:1,1000',
69
        ]);
70
71
        $category = Category::findOrFail($id);
72
        $category->name = mb_convert_case($request->name, MB_CASE_TITLE, 'UTF-8');
73
        $category->icon = $request->icon;
74
        $category->validity = $request->validity;
75
        $category->update();
76
        return redirect()->back()->with(
77
            'status',
78
            trans('categories.message.update', ['category' => $category->name])
79
        );
80
    }
81
82
    public function removeCategory($id)
83
    {
84
        $category = Category::findOrFail($id);
85
        $name = $category->name;
86
        $category->delete();
87
        return redirect()->back()->with(
88
            'status',
89
            trans('categories.message.delete', ['category' => $name])
90
        );
91
    }
92
93 View Code Duplication
    public function purgeAccounts($id)
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...
94
    {
95
        $category = Category::findOrFail($id);
96
        $accounts = $category->accounts()->where('status', Account::ACCOUNT_DISABLE);
97
        if ($accounts->exists()) {
98
            $accounts->delete();
99
        }
100
        return redirect()->back()->with(
101
            'status',
102
            trans('categories.message.drop', ['category' => $category->name])
103
        );
104
    }
105
106 View Code Duplication
    public function disableAccounts($id)
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...
107
    {
108
        $category = Category::findOrFail($id);
109
        $accounts = $category->accounts()->where('status', Account::ACCOUNT_ENABLE)->get();
110
        foreach ($accounts as $account) {
111
            $account->disable();
112
        }
113
        return redirect()->back()->with(
114
            'status',
115
            trans('categories.message.disable', ['category' => $category->name])
116
        );
117
    }
118
}
119