Completed
Push — master ( c69a04...dc1454 )
by
unknown
15:08 queued 13:13
created

ApiKey::handle()   C

Complexity

Conditions 8
Paths 7

Size

Total Lines 70
Code Lines 52

Duplication

Lines 42
Ratio 60 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 42
loc 70
rs 6.4909
cc 8
eloc 52
nc 7
nop 2

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace App\Http\Middleware;
4
5
use Closure;
6
use Redirect;
7
use Validator;
8
use App\ApiKeys;
9
use Bantenprov\Workflow\Models\History;
10
11
class ApiKey
12
{
13
    /**
14
     * Handle an incoming request.
15
     *
16
     * @param  \Illuminate\Http\Request  $request
17
     * @param  \Closure  $next
18
     * @return mixed
19
     */
20
    public function handle($request, Closure $next)
21
    {
22 View Code Duplication
        if($request->header('apikey') == '')
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...
23
        {
24
            return response()->json([
25
                'error' => true,
26
                'status' => 200,
27
                'title' => 'Error',
28
                'type' => 'error',
29
                'message' => 'apikey not found',
30
                'result' => []
31
            ]);
32
        }
33
        $check = ApiKeys::where('api_key', $request->header('apikey'))->first();
34 View Code Duplication
        if(count($check) == 0)
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...
35
        {
36
            return response()->json([
37
                'error' => true,
38
                'status' => 200,
39
                'title' => 'Error',
40
                'type' => 'error',
41
                'message' => 'invalid apikey',
42
                'result' => []
43
            ]);
44
        }
45
46
        if(count($check) != 0){
47
          $history = History::with('getApiKeys')
48
                             ->with('getWorkflow')
49
                             ->with('getStateFrom')
50
                             ->with('getStateTo')
51
                             ->with('getUserName')
52
                             ->where('content_id', $check->id)->get();
53
54
         foreach ($history as $value) {
55
           $workstateto = $value->getStateTo->label;
56
         }
57
         if($workstateto == 'Propose'){
58
             return response()->json([
59
                 'error' => true,
60
                 'status' => 200,
61
                 'title' => 'Error',
62
                 'type' => 'error',
63
                 'message' => 'invalid apikey',
64
                 'result' => []
65
             ]);
66
         }
67 View Code Duplication
         elseif($workstateto == 'Request'){
0 ignored issues
show
Bug introduced by
The variable $workstateto does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
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...
68
             return response()->json([
69
                 'error' => true,
70
                 'status' => 200,
71
                 'title' => 'Error',
72
                 'type' => 'error',
73
                 'message' => 'invalid apikey',
74
                 'result' => []
75
             ]);
76
         }
77 View Code Duplication
         elseif($workstateto == 'Rejected'){
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...
78
             return response()->json([
79
                 'error' => true,
80
                 'status' => 200,
81
                 'title' => 'Error',
82
                 'type' => 'error',
83
                 'message' => 'invalid apikey',
84
                 'result' => []
85
             ]);
86
         }
87
        }
88
        return $next($request);
89
    }
90
}
91