RoleController::index()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 10

Duplication

Lines 14
Ratio 100 %

Importance

Changes 0
Metric Value
dl 14
loc 14
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 10
nc 2
nop 1
1
<?php
2
3
namespace Mamikon\RoleManager\Controllers;
4
5
use App\Http\Controllers\Controller;
6
use Illuminate\Http\Request;
7
use Mamikon\RoleManager\Models\Permissions;
8
use Mamikon\RoleManager\Models\Roles;
9
use Mamikon\RoleManager\Requests\RoleRequest;
10
11
/**
12
 * Class RoleController
13
 *
14
 * @category Laravel_Package
15
 * @package  Mamikon\RoleManager
16
 * @author   Mamikon Arakelyan <[email protected]>
17
 * @license  https://github.com/mamikon/role-manager/blob/master/LICENSE.md MIT
18
 * @link     https://github.com/mamikon/role-manager
19
 */
20
class RoleController extends Controller
21
{
22
    /**
23
     * Display a listing of the resource.
24
     *
25
     * @param Request $request
26
     *
27
     * @return \Illuminate\Http\Response
28
     */
29 View Code Duplication
    public function index(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...
30
    {
31
        $this->authorize('view_role');
32
        if (!empty($request->term)) {
33
            $term = trim($request->term);
34
            $roles = Roles::where('name', 'like', '%' . $term . '%')
35
                ->orWhere('description', 'like', '%' . $term . '%')
36
                ->paginate(config('roleManager.rolesPerPage'));
37
38
        } else {
39
            $roles = Roles::paginate(config('roleManager.rolesPerPage'));
40
        }
41
        return view('RoleManager::role.index', ['roles' => $roles]);
42
    }
43
44
    /**
45
     * Show the form for creating a new resource.
46
     *
47
     * @return \Illuminate\Http\Response
48
     */
49
    public function create()
50
    {
51
        $this->authorize('create_role');
52
53
        return view(
54
            'RoleManager::role.create',
55
            ['permissions' => Permissions::all()]
56
        );
57
    }
58
59
    /**
60
     * Store a newly created resource in storage.
61
     *
62
     * @param Request|RoleRequest $request
63
     *
64
     * @return \Illuminate\Http\Response
65
     */
66
    public function store(RoleRequest $request)
67
    {
68
        $this->authorize('create_role');
69
70
        $role = Roles::create($request->all());
71 View Code Duplication
        if (!empty($request->permission) AND is_array($request->permission)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
Documentation introduced by
The property permission does not exist on object<Mamikon\RoleManager\Requests\RoleRequest>. 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...
72
            foreach ($request->permission as $permission) {
0 ignored issues
show
Documentation introduced by
The property permission does not exist on object<Mamikon\RoleManager\Requests\RoleRequest>. 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...
73
                $role->permissions()->attach($permission);
74
            }
75
        }
76
        $request->session()->flash('message', 'Role Successfully Created');
77
        return redirect()->route('RoleManager::role.edit', $role->id);
78
    }
79
80
81
    /**
82
     * Show the form for editing the specified resource.
83
     *
84
     * @param Roles $role
85
     *
86
     * @return \Illuminate\Http\Response
87
     */
88
    public function edit(Roles $role)
89
    {
90
        $this->authorize('edit_role');
91
92
        $valueList = $role->permissions()->get()->pluck('id')->toArray();
93
        return view(
94
            'RoleManager::role.edit',
95
            ['role' => $role, 'permissions' => Permissions::all(),
96
                'valueList' => $valueList]
97
        );
98
    }
99
100
    /**
101
     * Update the specified resource in storage.
102
     *
103
     * @param Request|RoleRequest $request
104
     * @param int                 $id
105
     *
106
     * @return \Illuminate\Http\Response
107
     */
108
    public function update(RoleRequest $request, $id)
109
    {
110
        $this->authorize('edit_role');
111
112
        $role = Roles::where('id', $id)->firstOrFail();
113
        $role->update($request->all());
114 View Code Duplication
        if (!empty($request->permission) AND is_array($request->permission)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
Documentation introduced by
The property permission does not exist on object<Mamikon\RoleManager\Requests\RoleRequest>. 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...
115
            $role->permissions()->sync($request->permission);
0 ignored issues
show
Documentation introduced by
The property permission does not exist on object<Mamikon\RoleManager\Requests\RoleRequest>. 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...
116
117
        } else {
118
            $role->permissions()->detach();
119
        }
120
        $request->session()->flash('message', 'Role Updated Successfully');
121
        return redirect()->route('RoleManager::role.edit', $role->id);
122
    }
123
124
    /**
125
     * Remove the specified resource from storage.
126
     *
127
     * @param int $id
128
     *
129
     * @return \Illuminate\Http\Response
130
     */
131 View Code Duplication
    public function destroy($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...
132
    {
133
        $this->authorize('delete_role');
134
135
        Roles::where('id', $id)->delete();
136
        session()->flash('message', 'Role Deleted Successfully');
137
        session()->flash('message-status', 'alert-info');
138
        return redirect()->route('RoleManager::role.index');
139
    }
140
}
141