Completed
Push — master ( f606cd...f6df63 )
by
unknown
03:49 queued 02:25
created

StateController::update()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 43
Code Lines 29

Duplication

Lines 14
Ratio 32.56 %

Importance

Changes 0
Metric Value
cc 4
eloc 29
nc 6
nop 2
dl 14
loc 43
rs 8.5806
c 0
b 0
f 0
1
<?php
2
3
    namespace Bantenprov\VueWorkflow\Http\Controllers;
4
5
    use App\Http\Controllers\Controller;
6
    use Illuminate\Http\Request;
7
    use Bantenprov\VueWorkflow\Facades\VueWorkflow;
8
    use Bantenprov\VueWorkflow\Models\State;
9
    use Bantenprov\VueWorkflow\Models\Workflow;
10
11
    use Validator;
12
13
    /**
14
     * [Class] StateController
15
     */
16
    class StateController{
17
18
        protected $stateModel;
19
        protected $workflowModel;
20
21
        /**
22
         * [Function] __construct
23
         */
24
        public function __construct(State $state, Workflow $workflow)
25
        {
26
            $this->stateModel = $state;
27
            $this->workflowModel = $workflow;
28
        }
29
30
31
        /**
32
         * [Function] index
33
         * @param Request $req
0 ignored issues
show
Bug introduced by
There is no parameter named $req. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
34
         * 
35
         * @return json
36
         */
37 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...
38
        {
39
            //dd(response()->json(['message' => 'success', 'status' => true]));
0 ignored issues
show
Unused Code Comprehensibility introduced by
64% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
40
            // $response;
41
42
            // $param = explode('|',$req->get('sort'));
0 ignored issues
show
Unused Code Comprehensibility introduced by
65% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
43
44
            // if($req->get('filter') != ''){
0 ignored issues
show
Unused Code Comprehensibility introduced by
69% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
45
            //     $search = "%{$req->get('filter')}%";
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
46
            //     $response = $this->stateModel->where('name','like',$search)->orderBy($param[0], $param[1])->paginate(10);
0 ignored issues
show
Unused Code Comprehensibility introduced by
76% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
47
            // }else{
48
            //     if($req->get('sort') == ''){
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
49
            //         $response = $this->stateModel->paginate(10);
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
50
            //     }else{
51
            //         $response = $this->stateModel->orderBy($param[0], $param[1])->paginate(10);     
0 ignored issues
show
Unused Code Comprehensibility introduced by
72% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
52
            //     }                
53
            // }
54
55
            if (request()->has('sort')) {
56
                list($sortCol, $sortDir) = explode('|', request()->sort);
57
    
58
                $query = $this->stateModel->orderBy($sortCol, $sortDir);
59
            } else {
60
                $query = $this->stateModel->orderBy('id', 'asc');
61
            }
62
    
63
            if ($request->exists('filter')) {
64
                $query->where(function($q) use($request) {
65
                    $value = "%{$request->filter}%";
66
                    $q->where('label', 'like', $value)
67
                        ->orWhere('description', 'like', $value);
68
                });
69
            }
70
    
71
            $perPage = request()->has('per_page') ? (int) request()->per_page : null;
72
            $response = $query->paginate($perPage);
73
            
74
            foreach($response as $workflow){            
75
                array_set($response->data, 'workflow_id', $workflow->workflow->label);           
76
            }
77
78
            
79
80
            return response()->json($response);
81
            
82
        }
83
84
        /**
85
         * [Function] create
86
         * @param 
87
         * 
88
         * @return json
89
         */
90
        public function create(){
91
92
            $workflows = $this->stateModel->all();
0 ignored issues
show
Unused Code introduced by
$workflows 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...
93
            //dd($workflows);
94
            // $response = [];
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
95
            // foreach($workflows as $workflow){
0 ignored issues
show
Unused Code Comprehensibility introduced by
70% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
96
            //     $response[] = [
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
97
            //         'id' => $workflow->workflow->id,
0 ignored issues
show
Unused Code Comprehensibility introduced by
55% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
98
            //         'label' => $workflow->workflow->label
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
99
            //     ];
100
                
101
            // }
102
            $workflows =  $this->workflowModel->all();
103
104
            $response = $workflows;
105
            return response()->json($response);
106
107
        }
108
109
        /**
110
         * [Function] store
111
         * @param Request $req
112
         * 
113
         * @return json
114
         */
115
        public function store(Request $req)
116
        {
117
            $request['label']           = $req->label;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$request was never initialized. Although not strictly required by PHP, it is generally a good practice to add $request = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
118
            $request['name']            = $req->name;
119
            $request['description']     = $req->description;
120
            $request['workflow_id']     = $req->workflow_id;
121
            $request['status']          = 1;            
122
123
            $check = $this->stateModel->where('name',$req->name)->whereNull('deleted_at')->count();
124
125
            $validator = Validator::make($req->all(),[
126
                'label'         => 'required',
127
                'name'          => 'required',
128
                'description'   => 'required',
129
                'workflow_id'   => 'required'
130
            ]);
131
132 View Code Duplication
            if($validator->fails()){
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...
133
                if($check > 0){
134
                    $response['message']    = 'failed state allready exist !';
0 ignored issues
show
Coding Style Comprehensibility introduced by
$response was never initialized. Although not strictly required by PHP, it is generally a good practice to add $response = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
135
                    $response['status']     = false;
136
                }else{
137
                    $response['message']    = 'success add new state';
0 ignored issues
show
Coding Style Comprehensibility introduced by
$response was never initialized. Although not strictly required by PHP, it is generally a good practice to add $response = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
138
                    $response['status']     = true;
139
                    $this->stateModel->create($request);
140
                }                
141
            }else{
142
                $response['message']    = 'success add new state';
0 ignored issues
show
Coding Style Comprehensibility introduced by
$response was never initialized. Although not strictly required by PHP, it is generally a good practice to add $response = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
143
                $response['status']     = true;
144
                $this->stateModel->create($request);
145
            }
146
147
            
148
149
            return response()->json($response);
150
        }
151
        
152
        /**
153
         * [Function] edit
154
         * @param $id
155
         * 
156
         * @return json
157
         */
158
        public function edit($id)
159
        {
160
            $response = $this->stateModel->findOrFail($id);
161
            $response['status'] = true;
162
            return response()->json($response);
163
        }
164
        
165
        /**
166
         * [Function] update
167
         * @param Request $req
168
         * @param $id
169
         * 
170
         * @return json
171
         */
172
        public function update(Request $req,$id)
173
        {
174
            $request['label']           = $req->label;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$request was never initialized. Although not strictly required by PHP, it is generally a good practice to add $request = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
175
            $request['name']            = $req->name;
176
            $request['description']     = $req->description;
177
            $request['status']          = 1;            
178
            
179
            $check = $this->stateModel->where('name',$req->name)->whereNull('deleted_at')->count();
180
181
            if($req->old_name == $req->name){
182
                $validator = Validator::make($req->all(),[
183
                    'label'         => 'required',
184
                    'name'          => 'required',
185
                    'description'   => 'required',
186
                ]);
187
            }else{
188
                $validator = Validator::make($req->all(),[
189
                    'label'         => 'required',
190
                    'name'          => 'required|unique:workflow_state,name',
191
                    'description'   => 'required',
192
                ]);
193
            }
194
            
195
196 View Code Duplication
            if($validator->fails()){
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...
197
                if($check > 0){
198
                    $response['message']    = 'failed state allready exist !';
0 ignored issues
show
Coding Style Comprehensibility introduced by
$response was never initialized. Although not strictly required by PHP, it is generally a good practice to add $response = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
199
                    $response['status']     = false;
200
                }else{
201
                    $response['message']    = 'success add new state';
0 ignored issues
show
Coding Style Comprehensibility introduced by
$response was never initialized. Although not strictly required by PHP, it is generally a good practice to add $response = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
202
                    $response['status']     = true;
203
                    $this->stateModel->findOrFail($id)->update($request);
204
                }                
205
            }else{
206
                $response['message']    = 'success add new state';
0 ignored issues
show
Coding Style Comprehensibility introduced by
$response was never initialized. Although not strictly required by PHP, it is generally a good practice to add $response = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
207
                $response['status']     = true;
208
                $this->stateModel->findOrFail($id)->update($request);
209
            }
210
211
            
212
213
            return response()->json($response);
214
        }
215
        
216
        /**
217
         * [Function] destroy
218
         * @param Request $req
0 ignored issues
show
Bug introduced by
There is no parameter named $req. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
219
         * 
220
         * @return json
221
         */
222
        public function destroy($id)
223
        {
224
            $state = $this->stateModel->findOrFail($id)->delete();
0 ignored issues
show
Unused Code introduced by
$state 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...
225
226
            return response()->json(['status' => true]);
227
        }
228
229
    }