Code Duplication    Length = 122-122 lines in 2 locations

src/Usman/Guardian/Http/Controllers/Capabilities.php 1 location

@@ 10-131 (lines=122) @@
7
use Usman\Guardian\Validators\Exceptions\ValidationException;
8
use Usman\Guardian\Repositories\Interfaces\CapabilityRepositoryInterface;
9
10
class Capabilities extends Base {
11
12
    /**
13
     * The repository instance
14
     * 
15
     * @var Usman\Guardian\Repositories\CapabilityRepository
16
     */
17
    protected $capability;
18
19
    /**
20
     * The validator instance
21
     * 
22
     * @var Usman\Guardian\Validators\CapabilityValidator
23
     */
24
    protected $validator;
25
26
    /**
27
     * Creates a new instance of Capabilities controller.
28
     * 
29
     * @param CapabilityRepositoryInterface $capability
30
     * @param CapabilityValidator           $validator
31
     */
32
    public function __construct(CapabilityRepositoryInterface $capability, CapabilityValidator $validator)
33
    {
34
        $this->capability = $capability;
35
        $this->validator = $validator;
36
    }
37
38
    /**
39
     * Shows the index of capabilities.
40
     * 
41
     * @return Response
42
     */
43
    public function listCapability()
44
    {
45
        $capabilities = $this->capability->getByPageWith('roles');
46
        return View::make('guardian::partials.capability.list')->with('capabilities',$capabilities);
47
    }
48
49
    /**
50
     * Shows the new capability form.
51
     *
52
     * @return  Response
53
     */
54
    public function addCapability()
55
    {
56
        return View::make('guardian::partials.capability.add');
57
58
    }
59
60
    /**
61
     * Saves the newly created capability.
62
     * 
63
     * @return Response
64
     */
65
    public function createCapability()
66
    {   
67
        try
68
        {
69
            $this->validator->setFields(Input::all())->validate('create');
70
            $id = $this->capability->create(Input::all());
71
            if(Input::has('roles'))
72
            {
73
                $this->capability->attach($id,Input::get('roles'),'roles');
74
            }
75
            return Redirect::back()->withSuccess('Capability Created Successfully!');
76
        }
77
        catch(ValidationException $e)
78
        {
79
            $errors = $e->getValidationErrors();
80
            return Redirect::back()->withErrors($errors)->withInput();
81
        }
82
    }
83
84
    /**
85
     * Shows the edit capability form.
86
     * 
87
     * @param  int $id
88
     * @return Response
89
     */
90
    public function editCapability($id)
91
    {
92
        $capability = $this->capability->findByIdWith($id,'roles');
93
        return View::make('guardian::partials.capability.edit')->with('capability',$capability);
94
    }
95
96
    /**
97
     * Updates the capability in database.
98
     * 
99
     * @param  int $id
100
     * @return Response
101
     */
102
    public function updateCapability($id)
103
    {
104
        try
105
        {
106
            $this->validator->addRule('update','capability','required|alpha_dash|unique:capabilities,capability,'.$id);
107
            $this->validator->setFields(Input::all())->validate('update');
108
            $this->capability->update($id,Input::all());
109
            $this->capability->attach($id,Input::get('roles',[]),'roles');
110
            return Redirect::route('capability.list',Input::get('ref'))->withSuccess('Capability Updated Successfully!');
111
        }
112
        catch(ValidationException $e)
113
        {
114
            $errors = $e->getValidationErrors();
115
            return Redirect::back()->withErrors($errors)->withInput();
116
        }
117
    }
118
119
    /**
120
     * Deletes a capability from database.
121
     * 
122
     * @param  int $id
123
     * @return Response
124
     */
125
    public function deleteCapability($id)
126
    {
127
        $this->capability->deleteWith($id,['roles']);
128
        return Redirect::back()->withSuccess('Deleted Successfully!');
129
    }
130
    
131
}

src/Usman/Guardian/Http/Controllers/Roles.php 1 location

@@ 10-131 (lines=122) @@
7
use Usman\Guardian\Validators\Exceptions\ValidationException;
8
use Usman\Guardian\Repositories\Interfaces\RoleRepositoryInterface;
9
10
class Roles extends Base {
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;
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
}