Roles   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 122
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 4
dl 122
loc 122
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 5 5 1
A listRole() 5 5 1
A addRole() 4 4 1
A createRole() 18 18 3
A editRole() 5 5 1
A updateRole() 16 16 2
A deleteRole() 6 6 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php namespace Usman\Guardian\Http\Controllers;
2
3
use View;
4
use Input;
5
use Redirect;
6
use Usman\Guardian\Validators\RoleValidator;
7
use Usman\Guardian\Validators\Exceptions\ValidationException;
8
use Usman\Guardian\Repositories\Interfaces\RoleRepositoryInterface;
9
10 View Code Duplication
class Roles extends Base {
0 ignored issues
show
Duplication introduced by
This class 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...
11
12
    /**
13
     * The role repository instance.
14
     * 
15
     * @var Usman\Guardian\Repositories\RoleRepository
16
     */
17
    protected $role;
18
19
    /**
20
     * The validator instance.
21
     * 
22
     * @var RoleValidator
23
     */
24
    protected $validator;
25
26
    /**
27
     * Creates a new instance of the Roles controller.
28
     * 
29
     * @param RoleRepositoryInterface $role
30
     * @param RoleValidator           $validator
31
     */
32
    public function __construct(RoleRepositoryInterface $role, RoleValidator $validator)
33
    {
34
        $this->role = $role;
0 ignored issues
show
Documentation Bug introduced by
It seems like $role of type object<Usman\Guardian\Re...oleRepositoryInterface> is incompatible with the declared type object<Usman\Guardian\Ht...itories\RoleRepository> of property $role.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
35
        $this->validator = $validator;
36
    }
37
38
    /**
39
     * Shows an index of the roles.
40
     * 
41
     * @return Response
42
     */
43
    public function listRole()
44
    {
45
        $roles = $this->role->getByPageWith('capabilities');
46
        return View::make('guardian::partials.role.list')->with('roles',$roles);
47
    }
48
49
    /**
50
     * Shows the add role form.
51
     *
52
     * @return  Response
53
     */
54
    public function addRole()
55
    {
56
        return View::make('guardian::partials.role.add');
57
    }
58
59
    /**
60
     * Saves the new role in the database.
61
     * 
62
     * @return Response
63
     */
64
    public function createRole()
65
    {
66
        try
67
        {
68
            $this->validator->setFields(Input::all())->validate('create');
69
            $id = $this->role->create(Input::all());
70
            if(Input::has('capabilities'))
71
            {
72
                $this->role->attach($id,Input::get('capabilities'),'capabilities');
73
            }       
74
            return Redirect::back()->withSuccess('Role Added Successfully!');
75
        }
76
        catch(ValidationException $e)
77
        {
78
            $errors = $e->getValidationErrors();
79
            return Redirect::back()->withErrors($errors)->withInput();
80
        }
81
    }
82
83
    /**
84
     * Shows the edit role form.
85
     * 
86
     * @param int $id
87
     * @return Response
88
     */
89
    public function editRole($id)
90
    {
91
        $role = $this->role->findByIdWith($id,'capabilities');
92
        return View::make('guardian::partials.role.edit')->with('role',$role);
93
    }
94
95
    /**
96
     * Updates the role record in the database.
97
     * 
98
     * @param  int $id
99
     * @return Response
100
     */
101
    public function updateRole($id)
102
    {
103
        try
104
        {
105
            $this->validator->addRule('update','role_name','required|alpha|unique:roles,role_name,'.$id);
106
            $this->validator->setFields(Input::all())->validate('update');
107
            $this->role->update($id,Input::all());
108
            $this->role->attach($id,Input::get('capabilities',[]),'capabilities');
109
            return Redirect::route('role.list',Input::get('ref'))->withSuccess('Role Updated Successfully!');
110
        }
111
        catch(ValidationException $e)
112
        {
113
            $errors = $e->getValidationErrors();
114
            return Redirect::back()->withErrors($errors)->withInput();
115
        }
116
    }
117
118
    /**
119
     * Deletes a role from the database.
120
     * 
121
     * @param int $id   
122
     * @return Response
123
     */
124
    public function deleteRole($id)
125
    {
126
        $this->role->deleteWith($id,['users','capabilities']);
127
        return Redirect::back()->withSuccess('Deleted Successfully!');
128
        
129
    }
130
131
}