Capabilities::createCapability()   A
last analyzed

Complexity

Conditions 3
Paths 8

Size

Total Lines 18
Code Lines 10

Duplication

Lines 18
Ratio 100 %

Importance

Changes 0
Metric Value
dl 18
loc 18
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 10
nc 8
nop 0
1
<?php namespace Usman\Guardian\Http\Controllers;
2
3
use View;
4
use Input;
5
use Redirect;
6
use Usman\Guardian\Validators\CapabilityValidator;
7
use Usman\Guardian\Validators\Exceptions\ValidationException;
8
use Usman\Guardian\Repositories\Interfaces\CapabilityRepositoryInterface;
9
10 View Code Duplication
class Capabilities 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 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;
0 ignored issues
show
Documentation Bug introduced by
It seems like $capability of type object<Usman\Guardian\Re...ityRepositoryInterface> is incompatible with the declared type object<Usman\Guardian\Ht...s\CapabilityRepository> of property $capability.

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;
0 ignored issues
show
Documentation Bug introduced by
It seems like $validator of type object<Usman\Guardian\Va...rs\CapabilityValidator> is incompatible with the declared type object<Usman\Guardian\Ht...rs\CapabilityValidator> of property $validator.

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...
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
}