Completed
Push — master ( 098c1d...89670e )
by Jacob
02:08
created

Generator   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
c 1
b 0
f 0
lcom 1
cbo 3
dl 0
loc 70
ccs 32
cts 32
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 18 4
A getDefaultClaims() 0 11 3
A getForcedClaims() 0 8 1
A isBadClaim() 0 4 1
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016 Canis.io
4
 * @license   MIT
5
 */
6
namespace Canis\Lumen\Jwt\Adapters\Lcobucci;
7
8
use Lcobucci\JWT\ValidationData;
9
use Lcobucci\JWT\Builder;
10
use Lcobucci\JWT\Signer\Hmac\Sha256;
11
use Lcobucci\JWT\Parser;
12
use Lcobucci\JWT\Token;
13
use Canis\Lumen\Jwt\Contracts\Generator as GeneratorContract;
14
15
class Generator
16
    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...
17
    implements GeneratorContract
0 ignored issues
show
Coding Style introduced by
The implements keyword must be on the same line as the class name
Loading history...
18
{
19
    /**
20
     * Generates the token
21
     * @param  array $claims
22
     * @return Token
23
     */
24 11
    final public function __invoke(array $claims)
25
    {
26 11
        $signer = new Sha256();
27 11
        $builder = new Builder();
28 11
        $claims = array_merge($this->getDefaultClaims(), $claims, $this->getForcedClaims());
29 11
        if (!$this->checkRequiredClaims(array_keys($claims))) {
30 1
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type documented by Canis\Lumen\Jwt\Adapters...cci\Generator::__invoke of type Lcobucci\JWT\Token.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
31 10
        };
32 10
        foreach ($claims as $claim => $value) {
33 10
            if ($this->isBadClaim($claim)) {
34 1
                continue;
35
            }
36 10
            $builder->set($claim, $value);
37 10
        }
38 10
        $builder->setId(substr(hash('sha256', serialize($claims) . openssl_random_pseudo_bytes(20)), 0, 16), true);
39 10
        $builder->sign($signer, $this->config['secret']);
40 10
        return $builder->getToken();
41
    }
42
43
    /**
44
     * Default claims (can be overriden)
45
     *
46
     * @return array
47
     */
48 11
    protected function getDefaultClaims()
49
    {
50 11
        $default = [];
51 11
        if (!empty($this->config['issuer'])) {
52 11
            $default['iss'] = $this->config['issuer'];
53 11
        }
54 11
        if (!empty($this->config['audience'])) {
55 2
            $default['aud'] = $this->config['audience'];
56 2
        }
57 11
        return $default;
58
    }
59
60
    /**
61
     * Forced claims
62
     *
63
     * @return array
64
     */
65 11
    private function getForcedClaims()
66
    {
67 1
        return [
68 11
            'iat' => time(),
69 11
            'nbf' => time() + $this->config['nbfOffset'],
70 11
            'exp' => time() + $this->config['expOffset']
71 11
        ];
72
    }
73
74
    /**
75
     * Checks if claim is bad
76
     *
77
     * @param  string  $claim
78
     * @return boolean
79
     */
80 10
    private function isBadClaim($claim)
81
    {
82 10
        return in_array($claim, ['jti']);
83
    }
84
}
85