WorkflowController::store()   B
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 25
Code Lines 14

Duplication

Lines 7
Ratio 28 %

Importance

Changes 0
Metric Value
cc 2
eloc 14
nc 2
nop 1
dl 7
loc 25
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
23
    use WorkflowTrait;
24
    
25
    protected $workflowModel;
26
    protected $workflowTypeModel;
27
28
    /**
29
     * WorkflowController constructor.
30
     * @param Request $request
31
     * @param Workflow $workflow
32
     * @param WorkflowType $workflowType
33
     */
34
    public function __construct(Request $request, Workflow $workflow, WorkflowType $workflowType){
35
        $this->workflowModel        = $workflow;
36
        $this->workflowTypeModel    = $workflowType; 
37
38
        //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...
39
    }
40
41
    /**
42
     * @param Request $req
43
     * @return \Illuminate\Http\JsonResponse
44
     */
45
    public function index(Request $req)
46
    {     
47
             
48
        // $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...
49
        // 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...
50
        //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...
51
        
52
        //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...
53
        //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...
54
        //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...
55
        // 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...
56
        //     //Illuminate\Database\Eloquent\Model
57
        //     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...
58
        //         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...
59
        //     }
60
        //     //echo $value."<br>";
61
            
62
        // }
63
        // dd();
64
        $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...
65
66
        $param = explode('|',$req->get('sort'));
67
68
        if($req->get('filter') != ''){
69
            $search = "%{$req->get('filter')}%";
70
            $response = $this->workflowModel->where('name','like',$search)->orderBy($param[0], $param[1])->paginate(10);
71
        }else{
72
            if($req->get('sort') == ''){
73
                $response = $this->workflowModel->paginate(10);
74
            }else{
75
                $response = $this->workflowModel->orderBy($param[0], $param[1])->paginate(10);     
76
            }                
77
        }
78
79
        return response()->json($response);
80
    }
81
82
    /**
83
     * @return \Illuminate\Http\JsonResponse
84
     */
85
    public function create()
86
    {        
87
        $content_type = config('vue-workflow.content_type');
88
        
89
        return response()->json($content_type);
90
    }
91
92
    /**
93
     * @param Request $req
94
     * @return \Illuminate\Http\JsonResponse
95
     */
96
    public function store(Request $req)
97
    {
98
99
        // $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...
100
        // $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...
101
        $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...
102
        $request['name']            = $req->name;
103
104
        $validator = Validator::make($req->all(),[
105
            'label'             => 'required',
106
            'name'              => 'required'
107
        ]);
108
109 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...
110
            $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...
111
            $response['status']     = false;               
112
        }else{
113
            $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...
114
            $response['status']     = true;                
115
        }
116
117
        //$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...
118
        $this->workflowModel->create($request);
119
        return response()->json($response);
120
    }
121
122
    /**
123
     * @param $id
124
     * @return \Illuminate\Http\JsonResponse
125
     */
126
    public function show($id)
127
    {
128
        $check = $this->workflowTypeModel->where('workflow_id',$id)->count();
129
        
130
        if($check > 0){
131
            $response = $this->workflowModel->findOrFail($id);
132
            $response['workflow_type'] = ['label' =>$this->workflowModel->findOrFail($id)->getWorkflowType->workflow_type];
133
            $response['content_type'] = ['label' => $this->workflowModel->findOrFail($id)->getWorkflowType->content_type];
134
            $response['status'] = true;
135
        }else{
136
            $response = $this->workflowModel->findOrFail($id);
137
            $response['workflow_type'] = '-';
138
            $response['content_type'] = '-';
139
            $response['status'] = true;
140
        }
141
        
142
        return response()->json($response);
143
    }
144
145
    /**
146
     * @param $id
147
     * @return \Illuminate\Http\JsonResponse
148
     */
149 View Code Duplication
    public function edit($id)
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...
150
    {        
151
                
152
        $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...
153
        $response['workflow']   = $this->workflowModel->findOrFail($id);
154
        
155
        return response()->json($response);
156
    }
157
158
    /**
159
     * @param $id
160
     * @param Request $request
161
     * @return \Illuminate\Http\JsonResponse
162
     */
163
    public function update($id, Request $request)
164
    {      
165
        
166
        $validator = Validator::make($request->all(), [
167
            'label' => 'required',
168
            'name'  => 'required'
169
        ]);
170
171
        if($validator->fails()){
172
173
            $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...
174
            $response['message']    = 'failed update ';
175
176
        }else{
177
178
            $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...
179
            $response['message']    = 'update success';
180
            $this->workflowModel->findOrFail($id)->update($request->all());
181
182
        }
183
                
184
        
185
        
186
        return response()->json($response);
187
    }
188
189
190
    /**
191
     * @param Request $req
192
     * @param $id
193
     * @return \Illuminate\Http\JsonResponse
194
     */
195
    public function storeWorkflow(Request $req, $id)
196
    {
197
        $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...
198
        $workflow['workflow_type']  = $req->workflow_type;
199
        $workflow['content_type']   = $req->content_type;
200
201
        $this->workflowTypeModel->create($workflow);
202
203
        return response()->json(['status' => true, 'message' => 'Success register workflow']);
204
    }
205
206
    /**
207
     * @param $id
208
     * @return \Illuminate\Http\JsonResponse
209
     * @throws \Exception
210
     */
211
    public function destroy($id)
212
    {
213
        $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...
214
215
        return response()->json(['status' => true]);
216
    }
217
}
218