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

ApiKey   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 80
Duplicated Lines 52.5 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
c 1
b 0
f 0
lcom 0
cbo 1
dl 42
loc 80
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
C handle() 42 70 8

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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