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

WorkflowTrait::insertWithWorkflow()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 29
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 15
nc 2
nop 2
dl 0
loc 29
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
    namespace Bantenprov\VueWorkflow\Http\Traits;
4
    use Bantenprov\VueWorkflow\Models\WorkflowType;
5
    use Bantenprov\VueWorkflow\Models\History;
6
    use Bantenprov\VueWorkflow\Models\TransitionState;
7
    use Bantenprov\VueWorkflow\Models\Transition;
8
    
9
10
    /**
11
     * Workflow trait
12
     */
13
    trait WorkflowTrait
14
    {        
0 ignored issues
show
Coding Style introduced by
Opening trait brace must be on a line by itself
Loading history...
15
16
        public function insertWithWorkflow($class, $datas)
17
        {
18
            // if(in_array('label|asc',$request)){
0 ignored issues
show
Unused Code Comprehensibility introduced by
82% 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...
19
            //     return 'work!!';                
20
            // }
21
            //dd($request);
22
            //$class->create($datas);
0 ignored issues
show
Unused Code Comprehensibility introduced by
86% 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...
23
            $workflow = WorkflowType::where('content_type', class_basename($class));
24
            
25
            if($workflow->count() > 0){
26
                $get = $class->create($datas);
27
                //$class->find($get->id)->delete();
0 ignored issues
show
Unused Code Comprehensibility introduced by
77% 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...
28
                
29
                $transition = Transition::where('name','propose-to-propose')->where('workflow_id',$workflow->first()->workflow_id)->first();
30
                
31
                $this->storeHistory(
32
                    class_basename($class),
33
                    $get->id,
34
                    $workflow->first()->workflow_id,
35
                    $transition->id,
36
                    $transition->from,
37
                    $transition->to,
38
                    \Auth::user()->id
39
                );     
40
            }else{
41
                $class->create($datas);
42
            }
43
            
44
        }
45
46
        public function storeHistory($content_type, $content_id, $workflow_id, $workflow_transition_id, $from_state, $to_state, $user_id, $message = '')
47
        {
48
            $save['content_type']           = $content_type;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$save was never initialized. Although not strictly required by PHP, it is generally a good practice to add $save = 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...
49
            $save['content_id']             = $content_id;
50
            $save['workflow_id']            = $workflow_id;
51
            $save['workflow_transition_id'] = $workflow_transition_id;
52
            $save['from_state']             = $from_state;
53
            $save['to_state']               = $to_state;
54
            $save['user_id']                = $user_id;
55
            $save['message']                = $message;
56
57
            $history = History::create($save);
58
59
            $this->storeTransitionState($history->to_state ,$history->content_id, $history->id);
60
        }
61
62
        public function storeTransitionState($current_state, $content_id, $history_id)
63
        {
64
            $save['current_state'] = $current_state;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$save was never initialized. Although not strictly required by PHP, it is generally a good practice to add $save = 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...
65
            $save['content_id'] = $content_id;
66
            $save['history_id'] = $history_id;
67
68
            TransitionState::create($save);
69
        }
70
71
        public function updateWithWorkflow($class, $content_id, $request)
72
        {
73
            $check = History::where('content_id', $content_id);
74
75
            $response = [];
76
77
            if($check->count() > 1){
78
                $response['message'] = "Cant update, current state must be propose !";
79
            }else{
80
                $class->update($request);
81
                $response['message'] = "Update success";
82
            }
83
84
            return $response;
85
        }
86
87
        // public function workflowExecute($op, $datas)
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...
88
        // {
89
90
        // }
91
92
93
    }
94