WorkflowProcessController   A
last analyzed

Complexity

Total Complexity 22

Size/Duplication

Total Lines 230
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 22
lcom 1
cbo 1
dl 0
loc 230
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A getLastHistory() 0 4 1
C availableState() 0 80 12
B getAllHistoryThisContent() 0 52 5
B changeState() 0 40 3
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
    /**
34
     * WorkflowProcessController constructor.
35
     * @param State $state
36
     * @param Workflow $workflow
37
     * @param WorkflowType $workflowType
38
     * @param TransitionState $transitionState
39
     * @param Transition $transition
40
     * @param History $history
41
     */
42
    public function __construct(State $state, Workflow $workflow, WorkflowType $workflowType, TransitionState $transitionState, Transition $transition, History $history){
43
44
        $this->wokflowModel = $workflow;
45
        $this->stateModel = $state;
46
        $this->historyModel = $history;
47
        $this->transitionStateModel = $transitionState;
48
        $this->transitionModel = $transition;
49
        $this->workflowTypeModel = $workflowType;
50
51
    }
52
53
    public function getLastHistory()
54
    {
55
56
    }
57
58
    /**
59
     * @param $content_type
60
     * @param $content_id
61
     * @return \Illuminate\Http\JsonResponse
62
     */
63
    public function availableState($content_type,$content_id)
64
    {
65
66
        $workflow = WorkflowType::where('content_type', $content_type)->first();
67
68
        if(!$workflow){
69
            return response()->json([
70
                'status' => false
71
                ]);
72
        }
73
74
        $history = $this->historyModel->where('workflow_id',$workflow->workflow_id)->where('content_id',$content_id)->orderBy('created_at','desc')->first();
75
76
77
        if(!$history){
78
            return response()->json([
79
                'status' => false
80
                ]);
81
        }
82
83
84
85
86
        $check_history = $this->historyModel->where('workflow_id',$workflow->workflow_id)->where('content_id',$content_id)->count();
87
88
89
        $transition_state = TransitionState::where('history_id',$history->id)->orderBy('created_at','desc')->first();
90
91
        $transitions = Transition::where('from',$transition_state->current_state)->get();
92
93
        $states = State::where('workflow_id',$history->workflow_id)->get();
94
95
        $current_state = State::find($transition_state->current_state);
96
97
        $state_response = [];
98
99
        foreach($transitions as $transition)
100
        {
101
102
            foreach($states as $state)
103
            {
104
                if($state->id == $transition->to && $state->id != $transition_state->current_state){
105
                    if(!empty($transition->vueGuard->permission_id)){
106
                        $permission = \App\Permission::find($transition->vueGuard->permission_id);
107
                        if( (\Auth::user()->hasPermission($permission->name)) ){
108
109
                                array_push($state_response,$state);
110
                        }
111
                        array_set($state,'permission',$transition->vueGuard->permission_id);
112
113
                    }else{
114
                        array_set($state,'permission',0);
115
                    }
116
                }
117
            }
118
        }
119
        if($check_history == 1){
120
            return response()->json([
121
                'status' => true,
122
                'state' => $state_response,
123
                'transition_state' => $transition_state,
124
                'current_state' => $current_state,
125
                'current_history' => $history,
126
                'user' => '-'
127
                ]);
128
        }elseif($check_history > 1){
129
            return response()->json([
130
                'status' => true,
131
                'state' => $state_response,
132
                'transition_state' => $transition_state,
133
                'current_state' => $current_state,
134
                'current_history' => $history,
135
                'user' => (empty($history->user)) ? '-' : $history->user->name
136
                ]);
137
        }
138
        return response()->json([
139
            'status' => false,
140
            ]);
141
142
    }
143
144
    /**
145
     * @param Request $req
146
     * @param $content_type
147
     * @param $content_id
148
     * @return \Illuminate\Http\JsonResponse
149
     */
150
    public function getAllHistoryThisContent(Request $req, $content_type, $content_id){
151
152
        $response = array();
0 ignored issues
show
Unused Code introduced by
$response 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...
153
154
        $param = explode('|',$req->get('sort'));
155
156
        $workflow = WorkflowType::where('content_type', $content_type)->first();
157
158
        $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...
159
160
        if($req->get('filter') != ''){
161
            $search = "%{$req->get('filter')}%";
162
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
            ->where('content_type','like',$search)
170
            ->orderBy($param[0], $param[1])
171
            ->paginate(10);
172
        }else{
173
            if($req->get('sort') == ''){
174
                $response = $this->historyModel
175
                ->where('workflow_id',$workflow->workflow_id)
176
                ->where('content_id',$content_id)
177
                ->with('fromState')
178
                ->with('toState')
179
                ->with('user')
180
                ->paginate(10);
181
            }else{
182
                $response = $this->historyModel
183
                ->where('workflow_id',$workflow->workflow_id)
184
                ->where('content_id',$content_id)
185
                ->with('fromState')
186
                ->with('toState')
187
                ->with('user')
188
                ->orderBy($param[0], $param[1])
189
                ->paginate(10);
190
            }
191
        }
192
193
        foreach($response as $data){
194
            if(empty($data->message)){
195
                $data->message = "Start";
196
            }
197
        }
198
199
        return response()->json($response);
200
201
    }
202
203
    /**
204
     * @param Request $request
205
     * @param $content_id
206
     * @return \Illuminate\Http\JsonResponse
207
     */
208
    public function changeState(Request $request, $content_id)
209
    {
210
        $from   = $this->stateModel->find($request->from_state)->name;
211
        $to     = $this->stateModel->find($request->to_state)->name;
212
        $func = $from.'-to-'.$to;
213
214
        $call_func = "guard__".str_replace('-', '_', $func);
215
        $checkCondition = $this->$call_func($content_id);
216
217
        //return response()->json($checkCondition);
0 ignored issues
show
Unused Code Comprehensibility introduced by
73% 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...
218
219
        if($checkCondition['error'] == true){
220
            $response['message'] = $checkCondition['message'];
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...
221
            return response()->json($response);
222
        }
223
224
        $validator = Validator::make($request->all(), [
225
            'message' => 'required',
226
        ]);
227
228
        if($validator->fails()){
229
            $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...
230
            $response['message'] = 'reason is required';
231
        }else{
232
            $this->storeHistory(
233
                $request->content_type,
234
                $content_id,
235
                $request->workflow_id,
236
                $request->workflow_transition_id,
237
                $request->from_state,
238
                $request->to_state,
239
                \Auth::user()->id,
240
                $request->message
241
            );
242
            $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...
243
            $response['message'] = $checkCondition['message'];
244
        }
245
246
        return response()->json($response);
247
    }
248
249
250
}
251