NamshiSymmetric   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 82.35%

Importance

Changes 0
Metric Value
wmc 6
c 0
b 0
f 0
lcom 1
cbo 3
dl 0
loc 54
ccs 14
cts 17
cp 0.8235
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A encode() 0 10 2
A decode() 0 14 3
1
<?php
2
3
/**
4
 * This file is part of the BEAR.JwtAuthModule package.
5
 *
6
 * @license http://opensource.org/licenses/MIT MIT
7
 */
8
namespace BEAR\JwtAuth\Encoder;
9
10
use BEAR\JwtAuth\Annotation\Algo;
11
use BEAR\JwtAuth\Annotation\Secret;
12
use BEAR\JwtAuth\Exception\InvalidTokenException;
13
use BEAR\JwtAuth\Exception\JwtException;
14
use Namshi\JOSE\JWS;
15
16
class NamshiSymmetric implements JwtEncoderInterface
17
{
18
    /**
19
     * @var JWS
20
     */
21
    private $jws;
22
23
    /**
24
     * @var string
25
     */
26
    private $algo;
27
28
    /**
29
     * @var string
30
     */
31
    private $secret;
32
33
    /**
34
     * @Algo("algo")
35
     * @Secret("secret")
36
     */
37 6
    public function __construct(string $algo, string $secret)
38
    {
39 6
        $this->jws = new JWS(['typ' => 'JWT', 'alg' => $algo]);
40 6
        $this->algo = $algo;
41 6
        $this->secret = $secret;
42 6
    }
43
44 2
    public function encode(array $payload) : string
45
    {
46
        try {
47 2
            $this->jws->setPayload($payload)->sign($this->secret);
48
49 2
            return (string) $this->jws->getTokenString();
50
        } catch (Exception $e) {
0 ignored issues
show
Bug introduced by
The class BEAR\JwtAuth\Encoder\Exception 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...
51
            throw new JwtException($e->getMessage());
52
        }
53
    }
54
55 3
    public function decode(string $token) : array
56
    {
57
        try {
58 3
            $jws = $this->jws->load($token, false);
59 1
        } catch (\InvalidArgumentException $e) {
60 1
            throw new InvalidTokenException($e->getMessage());
61
        }
62
63 2
        if (!$jws->verify($this->secret, $this->algo)) {
64
            throw new InvalidTokenException('Invalid Token');
65
        }
66
67 2
        return (array) $jws->getPayload();
68
    }
69
}
70