CabController::update()   A
last analyzed

Complexity

Conditions 2
Paths 5

Size

Total Lines 13
Code Lines 10

Duplication

Lines 13
Ratio 100 %

Importance

Changes 0
Metric Value
cc 2
eloc 10
nc 5
nop 2
dl 13
loc 13
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace App\Itil\Controllers;
4
5
use App\Itil\Controllers\BaseServiceDeskController;
6
use Exception;
7
use App\Itil\Models\Common\Cab;
8
use Datatable;
9
use App\User;
10
use Illuminate\Http\Request;
11
use App\Itil\Model\Common\CabVote;
12
13 View Code Duplication
class CabController extends BaseServiceDeskController {
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...
14
15
    public function __construct() {
16
        $this->middleware('auth');
17
    }
18
19
    public function index() {
20
        try {
21
            return view('itil::cab.index');
22
        } catch (Exception $ex) {
23
            return redirect()->back()->with('fails', $ex->getMessage());
24
        }
25
    }
26
27
    public function getCab() {
28
        $cabs = new Cab();
29
        return Datatable::Collection($cabs->select('name', 'head', 'id')->get())
0 ignored issues
show
Documentation Bug introduced by
The method select does not exist on object<App\Itil\Models\Common\Cab>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
30
                        ->showColumns('name')
31
                        ->addColumn('head', function($model) {
32
                            $users = new User();
33
                            $head = "--";
34
                            $user = $users->find($model->head);
0 ignored issues
show
Documentation Bug introduced by
The method find does not exist on object<App\User>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
35
                            if ($user) {
36
                                $head = $user->email;
37
                            }
38
                            return $head;
39
                        })
40
                        ->addColumn('action', function($model) {
41
                            return "<a href=" . url('service-desk/cabs/' . $model->id . '/edit') . " class='btn btn-info'>Edit</a>";
42
                        })
43
                        ->orderColumns('name', 'head')
44
                        ->searchColumns('name', 'head')
45
                        ->make();
46
    }
47
48
    public function edit($id) {
49
        try {
50
            $cabs = new Cab();
51
            $agents = User::where('role', 'agent')->orWhere('role', 'admin')->lists('email', 'id')->toArray();
52
            $cab = $cabs->find($id);
0 ignored issues
show
Documentation Bug introduced by
The method find does not exist on object<App\Itil\Models\Common\Cab>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
53
            return view('itil::cab.edit', compact('cab', 'agents'));
54
        } catch (Exception $ex) {
55
            return redirect()->back()->with('fails', $ex->getMessage());
56
        }
57
    }
58
59
    public function create() {
60
        try {
61
            $agents = User::where('role', 'agent')->orWhere('role', 'admin')->lists('email', 'id')->toArray();
62
            return view('itil::cab.create', compact('agents'));
63
        } catch (Exception $ex) {
64
            return redirect()->back()->with('fails', $ex->getMessage());
65
        }
66
    }
67
68
    public function store(Request $request) {
69
        $this->validate($request, [
70
            'name' => 'required',
71
        ]);
72
        try {
73
            $cabs = new Cab();
74
            $cabs->fill($request->input())->save();
0 ignored issues
show
Bug introduced by
It seems like $request->input() targeting Illuminate\Http\Request::input() can also be of type string; however, Illuminate\Database\Eloquent\Model::fill() does only seem to accept array, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
75
            return redirect()->back()->with('success', 'Saved Successfully');
76
        } catch (Exception $ex) {
77
            return redirect()->back()->with('fails', $ex->getMessage());
78
        }
79
    }
80
81
    public function update($id, Request $request) {
82
        $this->validate($request, [
83
            'name' => 'required',
84
        ]);
85
        try {
86
            $cabs = new Cab();
87
            $cab = $cabs->find($id);
0 ignored issues
show
Documentation Bug introduced by
The method find does not exist on object<App\Itil\Models\Common\Cab>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
88
            $cab->fill($request->input())->save();
89
            return redirect()->back()->with('success', 'Saved Successfully');
90
        } catch (Exception $ex) {
91
            return redirect()->back()->with('fails', $ex->getMessage());
92
        }
93
    }
94
95
    public function vote($cabid, $owner) {
96
        try {
97
            $check = \App\Itil\Controllers\UtilityController::checkCabUser($cabid);
98
            if (!$check) {
99
                return redirect('/service-desk/problems')->with('fails', 'Unautherized');
100
            }
101
            $authid = \Auth::user()->id;
102
            $votes = new CabVote();
103
            $vote = $votes->where('cab_id', $cabid)->where('user_id', $authid)->where('owner', $owner)->first();
104
            if ($vote) {
105
                return redirect('/service-desk/problems')->with('fails', 'Already voted');
106
            }
107
            return view('itil::cab.vote', compact('cabid', 'owner'));
108
        } catch (Exception $ex) {
109
            dd($ex);
110
            return redirect('/service-desk/problems')->with('fails', $ex->getMessage());
111
        }
112
    }
113
114
    public function postVote($cabid, $owner, Request $request) {
115
        try {
116
            $check = \App\Itil\Controllers\UtilityController::checkCabUser($cabid);
117
            if (!$check) {
118
                return redirect('/service-desk/problems')->with('fails', 'Unautherized');
119
            }
120
            $authid = \Auth::user()->id;
121
            $votes = new CabVote();
122
            $vote = $votes->where('cab_id', $cabid)->where('user_id', $authid)->where('owner', $owner)->first();
123
            if ($vote) {
124
                return redirect('/service-desk/problems')->with('fails', 'Already voted');
125
            }
126
            $votes->create([
127
                'cab_id' => $cabid,
128
                'user_id' => $authid,
129
                'comment' => $request->input('comment'),
130
                'vote' => $request->input('vote'),
131
                'owner' => $owner,
132
            ]);
133
            return redirect('/service-desk/problems')->with('success', 'Voted successfully');
134
        } catch (Exception $ex) {
135
            return redirect('/service-desk/problems')->with('fails', $ex->getMessage());
136
        }
137
    }
138
139
    public function showVotes($id, $owner) {
140
        try {
141
            $vote = new CabVote();
142
            $votes = $vote->where('cab_id', $id)->where('owner', $owner)->get();
143
            return view('itil::cab.show-vote', compact('votes'));
144
        } catch (Exception $ex) {
145
            return redirect()->back()->with('fails', $ex->getMessage());
146
        }
147
    }
148
149
    public static function votedUser($userid) {
150
        $users = new \App\User();
151
        $user = $users->find($userid);
0 ignored issues
show
Documentation Bug introduced by
The method find does not exist on object<App\User>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
152
        $name = "";
153
        if ($user){
154
            $name = $user->first_name." ".$user->last_name;
155
            if($name==" "){
156
                $name = $user->user_name;
157
            }
158
        }
159
        return ucfirst($name);
160
    }
161
    
162
    public static function checkVote($vote){
163
        $value = "";
0 ignored issues
show
Unused Code introduced by
$value is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
164
        if($vote==1){
165
            $value = "Voted for proceed";
166
        }else{
167
            $value = "Voted for not proceed";
168
        }
169
        return $value;
170
    }
171
172
}
173