Completed
Push — master ( 486b73...844878 )
by claudio
03:43
created

GetUserAndRefresh::handle()   C

Complexity

Conditions 10
Paths 30

Size

Total Lines 50
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 13.32

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 50
ccs 19
cts 28
cp 0.6786
rs 5.7647
cc 10
eloc 30
nc 30
nop 3
crap 13.32

How to fix   Complexity   

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 plunner\Http\Middleware;
4
5
use Doctrine\Common\Util\Debug;
6
use Log;
7
use Tymon\JWTAuth\Exceptions\JWTException;
8
use Tymon\JWTAuth\Exceptions\TokenExpiredException;
9
10
class GetUserAndRefresh extends BaseMiddleware
11
{
12
    /**
13
     * Handle an incoming request.
14
     * If an user mode is set I don't check custom
15
     *
16
     * @param  \Illuminate\Http\Request  $request
17
     * @param  \Closure  $next
18
     * @param  String $custom custom claims that must be equals (format: key1-ele1;key2-ele2)
19
     * @return mixed
20
     */
21 2
    public function handle($request, \Closure $next, $custom = '')
22
    {
23 2
        $custom = $this->convertToArray($custom);
24 2
        $headers = $request->headers->all();
25 2
        foreach($headers as $header)
26 2
            Log::info('header: '.implode('-',$header));
27
28 2
        if($token = $this->auth->setRequest($request)->getToken()) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
29 2
        }else if ($this->auth->getUserModel()){
30 1
            $token = $this->auth->fromUser($this->auth->getUserModel(), $custom);
31 1
        }else {
32 1
            return $this->respond('tymon.jwt.absent', 'token_not_provided', 401);
33
        }
34
35
        try {
36 1
            $user = $this->auth->authenticate($token, $custom);
37 1
        } catch (TokenExpiredException $e) {
38
            return $this->respond('tymon.jwt.expired', 'token_expired', $e->getStatusCode(), [$e]);
39
        } catch(InvalidClaimException $e) {
0 ignored issues
show
Bug introduced by
The class plunner\Http\Middleware\InvalidClaimException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
40
            return $this->respond('tymon.jwt.invalid', 'claim_invalid', $e->getStatusCode(), [$e]);
41
        } catch (JWTException $e) {
42
            return $this->respond('tymon.jwt.invalid', 'token_invalid', $e->getStatusCode(), [$e]);
43
        }
44
45 1
        if (! $user) {
46
            return $this->respond('tymon.jwt.user_not_found', 'user_not_found', 404);
47
        }
48
49
        /**
50
         * refresh
51
         */
52
53 1
        $response = $next($request);
54
55
56 1
        $this->events->fire('tymon.jwt.valid', $user);
57
58
        try {
59 1
            $newToken = $this->auth->refresh($token, $custom);
60 1
        } catch (TokenExpiredException $e) {
61
            return $this->respond('tymon.jwt.expired', 'token_expired', $e->getStatusCode(), [$e]);
62
        } catch (JWTException $e) {
63
            return $this->respond('tymon.jwt.invalid', 'token_invalid', $e->getStatusCode(), [$e]);
64
        }
65
66
        // send the refreshed token back to the client
67 1
        $response->headers->set('Authorization', 'Bearer ' . $newToken);
68
69 1
        return $response;
70
    }
71
}
72