Completed
Push — master ( 7a1c5d...542831 )
by Jacob
02:09
created

Processor::__invoke()   D

Complexity

Conditions 10
Paths 30

Size

Total Lines 31
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 26
CRAP Score 10

Importance

Changes 3
Bugs 1 Features 0
Metric Value
c 3
b 1
f 0
dl 0
loc 31
ccs 26
cts 26
cp 1
rs 4.8196
cc 10
eloc 21
nc 30
nop 2
crap 10

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
 * @copyright Copyright (c) 2016 Canis.io
4
 * @license   MIT
5
 */
6
namespace Canis\Lumen\Jwt\Adapters\Lcobucci;
7
8
use Canis\Lumen\Jwt\Token;
9
use Canis\Lumen\Jwt\Contracts\Processor as ProcessorContract;
10
use Lcobucci\JWT\ValidationData;
11
use Lcobucci\JWT\Signer\Hmac\Sha256;
12
use Lcobucci\JWT\Parser;
13
14
class Processor
15
    extends HelperBase
0 ignored issues
show
Coding Style introduced by
The extends keyword must be on the same line as the class name
Loading history...
Coding Style introduced by
Expected 0 spaces between "HelperBase" and comma; 1 found
Loading history...
16
    implements ProcessorContract
0 ignored issues
show
Coding Style introduced by
The implements keyword must be on the same line as the class name
Loading history...
17
{
18
    /**
19
     * @inheritdoc
20
     */
21 16
    final public function __invoke($tokenString, $validateClaims = [])
22
    {
23 16
        $token = (new Parser())->parse((string) $tokenString);
24 16
        $signer = new Sha256();
25 16
        if (!$token->verify($signer, $this->config['secret'])) {
26 1
            return false;
27
        }
28 15
        if (!$this->checkRequiredClaims(array_keys($token->getClaims()))) {
29 1
            return false;
30 6
        };
31 14
        $data = new ValidationData();
32 14
        if (isset($this->config['issuer'])) {
33 14
            $data->setIssuer($this->config['issuer']);
34 14
        }
35 14
        if (isset($this->config['audience'])) {
36 1
            $data->setAudience($this->config['audience']);
37 1
        }
38 14
        if (!$token->validate($data)) {
39 2
            return false;
40
        }
41 12
        $claims = $token->getClaims();
42 12
        foreach ($claims as $key => $value) {
43 12
            $claims[$key] = $value->getValue();
44 12
        }
45 12
        foreach ($validateClaims as $claim => $value) {
46 10
            if (!isset($claims[$claim]) || $claims[$claim] !== $value) {
47 3
                return false;
48
            }
49 9
        }
50 9
        return new Token((string) $token, $claims);
51
    }
52
}
53