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

WorkflowProcessController::availableState()   C

Complexity

Conditions 13
Paths 23

Size

Total Lines 80
Code Lines 44

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 13
eloc 44
nc 23
nop 2
dl 0
loc 80
rs 5.0618
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
// ganti name class dg WorkflowTransitionController
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
use Bantenprov\VueWorkflow\Models\WorkflowType;
11
use Bantenprov\VueWorkflow\Models\TransitionState;
12
use Bantenprov\VueWorkflow\Models\Transition;
13
use Bantenprov\VueWorkflow\Models\History;
14
15
use Bantenprov\VueWorkflow\Http\Traits\WorkflowTrait;
16
17
use App\Http\Controllers\Traits\WorkflowConditionTrait;
18
19
use Validator;
20
21
class WorkflowProcessController extends Controller
22
{
23
    use WorkflowTrait;
24
    use WorkflowConditionTrait;
25
26
    protected $wokflowModel;
27
    protected $stateModel;
28
    protected $historyModel;
29
    protected $transitionStateModel;
30
    protected $transitionModel;
31
    protected $workflowTypeModel;
32
33
    public function __construct(State $state, Workflow $workflow, WorkflowType $workflowType, TransitionState $transitionState, Transition $transition, History $history){        
34
35
        $this->wokflowModel = $workflow;
36
        $this->stateModel = $state;
37
        // $this->historyModel = config('vue-workflow.WORKFLOW_HISTORY');
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...
38
        $this->historyModel = $history;
39
        $this->transitionStateModel = $transitionState;
40
        $this->transitionModel = $transition;
41
        $this->workflowTypeModel = $workflowType;
42
43
    }
44
45
    public function getLastHistory()
46
    {
47
48
    }
49
50
    public function availableState($content_type,$content_id)
51
    {        
52
                
53
        $workflow = WorkflowType::where('content_type', $content_type)->first();
54
55
        if(!$workflow){
56
            return response()->json([
57
                'status' => false
58
                ]);
59
        }
60
61
        $history = $this->historyModel->where('workflow_id',$workflow->workflow_id)->where('content_id',$content_id)->orderBy('created_at','desc')->first();
62
63
        if(!$history){
64
            return response()->json([
65
                'status' => false
66
                ]);
67
        }
68
69
70
71
72
        $check_history = $this->historyModel->where('workflow_id',$workflow->workflow_id)->where('content_id',$content_id)->count();
73
74
75
        $transition_state = TransitionState::where('history_id',$history->id)->orderBy('created_at','desc')->first();
76
77
        $transitions = Transition::where('from',$transition_state->current_state)->get();
78
        
79
        $states = State::where('workflow_id',$history->workflow_id)->get();
80
81
        $current_state = State::find($transition_state->current_state);
82
83
        $state_response = [];
84
85
        foreach($transitions as $transition)
86
        {
87
            
88
            foreach($states as $state)
89
            {
90
                if($state->id == $transition->to && $state->id != $transition_state->current_state){
91
                    if(!empty($transition->vueGuard->permission_id)){
92
                        $permission = \App\Permission::find($transition->vueGuard->permission_id);
93
                        if( (\Auth::user()->hasPermission($permission->name)) ){
94
                            if($this->availableStateCondition()){
95
                                array_push($state_response,$state);
96
                            }                            
97
                        }
98
                        array_set($state,'permission',$transition->vueGuard->permission_id);
99
                        
100
                    }else{
101
                        array_set($state,'permission',0);
102
                    }                    
103
                }
104
            }
105
        }
106
        if($check_history == 1){
107
            return response()->json([
108
                'status' => true,
109
                'state' => $state_response,
110
                'transition_state' => $transition_state,
111
                'current_state' => $current_state,
112
                'current_history' => $history,
113
                'user' => '-'
114
                ]);
115
        }elseif($check_history > 1){
116
            return response()->json([
117
                'status' => true,
118
                'state' => $state_response,
119
                'transition_state' => $transition_state,
120
                'current_state' => $current_state,
121
                'current_history' => $history,
122
                'user' => (empty($history->user)) ? '-' : $history->user->name
123
                ]);
124
        }
125
        return response()->json([
126
            'status' => false,
127
            ]);
128
129
    }
130
131
    public function getAllHistoryThisContent(Request $req, $content_type, $content_id){
132
133
        $response;
0 ignored issues
show
Bug introduced by
The variable $response seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
134
135
        $param = explode('|',$req->get('sort'));
136
137
        $workflow = WorkflowType::where('content_type', $content_type)->first();
138
139
        $histories = $this->historyModel->where('workflow_id',$workflow->workflow_id)->where('content_id',$content_id)->orderBy('created_at','desc')->paginate(10);
0 ignored issues
show
Unused Code introduced by
$histories 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...
140
141
        if($req->get('filter') != ''){
142
            $search = "%{$req->get('filter')}%";
143
144
            $response = $this->historyModel
145
            ->where('workflow_id',$workflow->workflow_id)
146
            ->where('content_id',$content_id)
147
            ->with('fromState')
148
            ->with('toState')
149
            ->with('user')
150
            ->where('content_type','like',$search)
151
            ->orderBy($param[0], $param[1])
152
            ->paginate(10);
153
        }else{
154
            if($req->get('sort') == ''){
155
                $response = $this->historyModel
156
                ->where('workflow_id',$workflow->workflow_id)
157
                ->where('content_id',$content_id)
158
                ->with('fromState')
159
                ->with('toState')
160
                ->with('user')
161
                ->paginate(10);
162
            }else{
163
                $response = $this->historyModel
164
                ->where('workflow_id',$workflow->workflow_id)
165
                ->where('content_id',$content_id)
166
                ->with('fromState')
167
                ->with('toState')
168
                ->with('user')
169
                ->orderBy($param[0], $param[1])
170
                ->paginate(10);
171
            }
172
        }
173
174
        foreach($response as $data){
175
            if(empty($data->message)){
176
                $data->message = "Start";
177
            }
178
        }
179
180
        return response()->json($response);
181
182
    }
183
184
    public function changeState(Request $request, $content_id)
185
    {
186
        $validator = Validator::make($request->all(), [
187
            'message' => 'required',
188
        ]);
189
190
        if($validator->fails()){
191
            $response['status'] = false;
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...
192
            $response['message'] = 'reason is required';
193
        }else{
194
            $this->storeHistory(
195
                $request->content_type,
196
                $content_id,
197
                $request->workflow_id,
198
                $request->workflow_transition_id,
199
                $request->from_state,
200
                $request->to_state,
201
                \Auth::user()->id,
202
                $request->message
203
            );
204
            $response['status'] = true;
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...
205
            $response['message'] = 'success';
206
        }        
207
208
        return response()->json($response);
209
    }    
210
211
212
213
214
}
215