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

WorkflowController::index()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 35
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 12
nc 3
nop 1
dl 0
loc 35
rs 8.8571
c 0
b 0
f 0
1
<?php namespace Bantenprov\VueWorkflow\Http\Controllers;
2
3
use App\Http\Controllers\Controller;
4
use Illuminate\Http\Request;
5
use Bantenprov\VueWorkflow\Facades\VueWorkflow;
6
use Bantenprov\VueWorkflow\Models\Workflow;
7
use Bantenprov\VueWorkflow\Models\WorkflowType;
8
use Bantenprov\VueWorkflow\Models\Transition;
9
10
use Validator;
11
12
use Bantenprov\VueWorkflow\Http\Traits\WorkflowTrait;
13
14
/**
15
 * The VueWorkflowController class.
16
 *
17
 * @package Bantenprov\VueWorkflow
18
 * @author  bantenprov <[email protected]>
19
 */
20
class WorkflowController extends Controller
21
{
22
    use WorkflowTrait;
23
    
24
    protected $workflowModel;
25
    protected $workflowTypeModel;
26
27
    public function __construct(Request $request, Workflow $workflow, WorkflowType $workflowType){
28
        $this->workflowModel        = $workflow;
29
        $this->workflowTypeModel    = $workflowType; 
30
31
        //dd($request->path());
0 ignored issues
show
Unused Code Comprehensibility introduced by
78% 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...
32
    }
33
34
    /**
35
     * [Function] index
36
     * @param Request $req
37
     * 
38
     * @return json
39
     */
40
    public function index(Request $req)
41
    {            
42
        // $transition = Transition::where('name','propose-to-propose')->where('workflow_id','19')->first();
0 ignored issues
show
Unused Code Comprehensibility introduced by
68% 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
        // dd($transition->id);
0 ignored issues
show
Unused Code Comprehensibility introduced by
63% 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...
44
        //dd($this->getRequest($req->input('sort')));  
0 ignored issues
show
Unused Code Comprehensibility introduced by
80% 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
        
46
        //dd(class_basename($this->workflowTypeModel));
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...
47
        //dd(class_basename(get_class($this)));
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...
48
        //dd(get_declared_classes());
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...
49
        // foreach (get_declared_classes() as $value) {
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
        //     //Illuminate\Database\Eloquent\Model
51
        //     if(get_parent_class($value) == "Illuminate\Database\Eloquent\Model"){
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...
52
        //         echo $value."<br>";
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...
53
        //     }
54
        //     //echo $value."<br>";
55
            
56
        // }
57
        // dd();
58
        $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...
59
60
        $param = explode('|',$req->get('sort'));
61
62
        if($req->get('filter') != ''){
63
            $search = "%{$req->get('filter')}%";
64
            $response = $this->workflowModel->where('name','like',$search)->orderBy($param[0], $param[1])->paginate(10);
65
        }else{
66
            if($req->get('sort') == ''){
67
                $response = $this->workflowModel->paginate(10);
68
            }else{
69
                $response = $this->workflowModel->orderBy($param[0], $param[1])->paginate(10);     
70
            }                
71
        }
72
73
        return response()->json($response);
74
    }
75
76
    /**
77
     * [Function] create
78
     * @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...
79
     * 
80
     * @return json
81
     */
82
    public function create()
83
    {
84
        $content_type = [
85
            ['id' => 1,'label' => 'Posting'], 
86
            ['id' => 2,'label' => 'Posting1'],
87
            ['id' => 3,'label' => 'SopOnline'],
88
            ['id' => 4,'label' => 'Workflow'],
89
            ['id' => 5,'label' => 'Pendaftaran'],
90
        ];
91
        
92
        return response()->json($content_type);
93
    }
94
95
    /**
96
     * [Function] store
97
     * @param Request $req
98
     * 
99
     * @return json
100
     */
101
    public function store(Request $req)
102
    {
103
104
        // $request['content_id']      = 1;
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...
105
        // $request['content_type']    = $req->content_type;
0 ignored issues
show
Unused Code Comprehensibility introduced by
59% 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...
106
        $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...
107
        $request['name']            = $req->name;
108
109
        $validator = Validator::make($req->all(),[
110
            'label'             => 'required',
111
            'name'              => 'required'
112
        ]);
113
114 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...
115
            $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...
116
            $response['status']     = false;               
117
        }else{
118
            $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...
119
            $response['status']     = true;                
120
        }
121
122
        //$this->insertWithWorkflow($this->workflowModel, $request);
0 ignored issues
show
Unused Code Comprehensibility introduced by
75% 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...
123
        $this->workflowModel->create($request);
124
        return response()->json($response);
125
    }
126
127
    /**
128
     * [Function] show
129
     * @param $id
130
     * 
131
     * @return json
132
     */
133
    public function show($id)
134
    {
135
        $check = $this->workflowTypeModel->where('workflow_id',$id)->count();
136
        
137
        if($check > 0){
138
            $response = $this->workflowModel->findOrFail($id);
139
            $response['workflow_type'] = ['label' =>$this->workflowModel->findOrFail($id)->getWorkflowType->workflow_type];
140
            $response['content_type'] = ['label' => $this->workflowModel->findOrFail($id)->getWorkflowType->content_type];
141
            $response['status'] = true;
142
        }else{
143
            $response = $this->workflowModel->findOrFail($id);
144
            $response['workflow_type'] = '-';
145
            $response['content_type'] = '-';
146
            $response['status'] = true;
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
                
161
        $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...
162
        $response['workflow']   = $this->workflowModel->findOrFail($id);
163
        
164
        return response()->json($response);
165
    }
166
167
    /**
168
     * [Function] update
169
     * @param $id
170
     * 
171
     * @return json
172
     */
173
    public function update($id, Request $request)
174
    {      
175
        
176
        $validator = Validator::make($request->all(), [
177
            'label' => 'required',
178
            'name'  => 'required'
179
        ]);
180
181
        if($validator->fails()){
182
183
            $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...
184
            $response['message']    = 'failed update ';
185
186
        }else{
187
188
            $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...
189
            $response['message']    = 'update success';
190
            $this->workflowModel->findOrFail($id)->update($request->all());
191
192
        }
193
                
194
        
195
        
196
        return response()->json($response);
197
    }
198
    
199
200
    /**
201
     * [Function] storeWorkflow
202
     * @param Request $req
203
     * 
204
     * @return json
205
     */
206
    public function storeWorkflow(Request $req, $id)
207
    {
208
        $workflow['workflow_id']    = $id;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$workflow was never initialized. Although not strictly required by PHP, it is generally a good practice to add $workflow = 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...
209
        $workflow['workflow_type']  = $req->workflow_type;
210
        $workflow['content_type']   = $req->content_type;
211
212
        $this->workflowTypeModel->create($workflow);
213
214
        return response()->json(['status' => true, 'message' => 'Success register workflow']);
215
    }
216
217
    /**
218
     * [Function] destroy
219
     * @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...
220
     * 
221
     * @return json
222
     */
223
    public function destroy($id)
224
    {
225
        $state = $this->workflowModel->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...
226
227
        return response()->json(['status' => true]);
228
    }
229
}
230