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

TransitionController::index()   B

Complexity

Conditions 5
Paths 16

Size

Total Lines 50
Code Lines 16

Duplication

Lines 50
Ratio 100 %

Importance

Changes 0
Metric Value
cc 5
eloc 16
nc 16
nop 1
dl 50
loc 50
rs 8.6315
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\Models\Transition;
8
use Bantenprov\VueWorkflow\Models\Workflow;
9
use Bantenprov\VueWorkflow\Models\State;
10
use Validator;
11
12
class TransitionController extends Controller
13
{
14
15
    protected $transitionModel;
16
    protected $workflowModel;
17
    protected $stateModel;
18
19
    /**
20
     * [Function] __construct
21
     * @param
22
     * 
23
     * @return 
24
     */
25
    public function __construct(Transition $transition, Workflow $workflow, State $state)
26
    {
27
        $this->transitionModel = $transition;
28
        $this->workflowModel = $workflow;
29
        $this->stateModel = $state;
30
    }
31
32
    /**
33
     * [Function] index
34
     * @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...
35
     * 
36
     * @return json
37
     */
38 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...
39
    {
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->transitionModel->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->transitionModel->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->transitionModel->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
        //     foreach($response as $kegiatan){            
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...
56
        //         array_set($response->data, 'workflow_id', $kegiatan->kegiatan->label);           
0 ignored issues
show
Unused Code Comprehensibility introduced by
62% 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...
57
        //     }
58
59
            // ===================================================
60
61
        if (request()->has('sort')) {
62
            list($sortCol, $sortDir) = explode('|', request()->sort);
63
64
            $query = $this->transitionModel->orderBy($sortCol, $sortDir);
65
        } else {
66
            $query = $this->transitionModel->orderBy('id', 'asc');
67
        }
68
69
        if ($request->exists('filter')) {
70
            $query->where(function($q) use($request) {
71
                $value = "%{$request->filter}%";
72
                $q->where('label', 'like', $value)
73
                    ->orWhere('description', 'like', $value);
74
            });
75
        }
76
77
        $perPage = request()->has('per_page') ? (int) request()->per_page : null;
78
        $response = $query->paginate($perPage);
79
        
80
        foreach($response as $workflow){            
81
            array_set($response->data, 'workflow_id', $workflow->getWorkflow->label);           
82
        }
83
84
        
85
86
        return response()->json($response);
87
    }
88
89
    /**
90
     * [Function] create
91
     * @param 
92
     * 
93
     * @return json
94
     */
95
    public function create()
96
    {        
97
        $response = $this->workflowModel->all();          
98
99
        return response()->json($response);
100
    }
101
102
    /**
103
     * [Function] getWorkflowState
104
     * @param 
105
     * 
106
     * @return json
107
     */
108
    public function getWorkflowState($id)
109
    {
110
        if($id == 'undefined'){
111
            $response = [];  
112
        }else{
113
            $response = $this->stateModel->where('workflow_id',$id)->get();  
114
        }            
115
116
        return response()->json($response);
117
    }
118
119
    /**
120
     * [Function] store
121
     * @param Request $req
122
     * 
123
     * @return json
124
     */
125
    public function store(Request $req)
126
    {
127
        $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...
128
        $request['name']            = strtolower($req->name);
129
        $request['workflow_id']     = $req->workflow_id; 
130
        $request['from']            = $req->from; 
131
        $request['to']              = $req->to;
132
        $request['message']         = $req->message;
133
134
        // $check = $this->transitionModel->where('name',$req->name)->whereNull('deleted_at')->count();
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...
135
136
        $validator = Validator::make($req->all(),[
137
            'name'          => 'required',
138
            'label'         => 'required',            
139
            'message'       => 'required',
140
            'from'          => 'required',
141
            'to'            => 'required',
142
            'workflow_id'   => 'required',
143
        ]);
144
145 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...
146
            //if($check > 0){
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...
147
                $response['message']    = 'failed transition 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...
148
                $response['status']     = false;
149
            // }else{
150
            //     $response['message']    = 'success add new transition';
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% 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...
151
            //     $response['status']     = true;
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% 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...
152
            //     $this->transitionModel->create($request);
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...
153
            // }                
154
        }else{
155
            $response['message']    = 'success add new transition';
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...
156
            $response['status']     = true;
157
            $this->transitionModel->create($request);
158
        }
159
160
        return response()->json($response);
161
    }
162
163
    /**
164
     * [Function] edit
165
     * @param $id
166
     * 
167
     * @return json
168
     */
169
    public function edit($id)
170
    {
171
        $response = $this->transitionModel
172
                        ->with('stateTo')
173
                        ->with('stateFrom')
174
                        ->with('getWorkflow')
175
                        ->findOrFail($id);
176
        $response['status'] = true;
177
        return response()->json($response);
178
    }
179
    
180
    /**
181
     * [Function] update
182
     * @param Request $req
183
     * @param $id
184
     * 
185
     * @return json
186
     */
187
    public function update(Request $req,$id)
188
    {
189
        $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...
190
        $request['name']            = strtolower($req->name);
191
        $request['workflow_id']     = $req->workflow_id; 
192
        $request['from']            = $req->from; 
193
        $request['to']              = $req->to;
194
        $request['message']         = $req->message;          
195
        
196
        $check = $this->transitionModel->where('name',$req->name)->whereNull('deleted_at')->count();
197
198
        if($req->old_name == $req->name){
199
            $validator = Validator::make($req->all(),[
200
                'label'         => 'required',
201
                'name'          => 'required',
202
                'message'       => 'required',
203
                'from'          => 'required',
204
                'to'            => 'required',
205
                'workflow_id'   => 'required',
206
            ]);
207
        }else{
208
            $validator = Validator::make($req->all(),[
209
                'label'         => 'required',
210
                'name'          => 'required|unique:workflow_transition,name',
211
                'message'       => 'required',
212
                'from'          => 'required',
213
                'to'            => 'required',
214
                'workflow_id'   => 'required',
215
            ]);
216
        }
217
        
218
219 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...
220
            if($check > 0){
221
                $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...
222
                $response['status']     = false;
223
            }else{
224
                $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...
225
                $response['status']     = true;
226
                $this->transitionModel->findOrFail($id)->update($request);
227
            }                
228
        }else{
229
            $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...
230
            $response['status']     = true;
231
            $this->transitionModel->findOrFail($id)->update($request);
232
        }
233
234
        
235
236
        return response()->json($response);
237
    }
238
    
239
    /**
240
     * [Function] destroy
241
     * @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...
242
     * 
243
     * @return json
244
     */
245
    public function destroy($id)
246
    {
247
        $state = $this->transitionModel->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...
248
249
        return response()->json(['status' => true]);
250
    }
251
}
252