AuthProvider   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 4
c 0
b 0
f 0
lcom 1
cbo 3
dl 0
loc 44
ccs 0
cts 17
cp 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A get() 0 22 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\Auth;
9
10
use BEAR\JwtAuth\Encoder\JwtEncoderInterface;
11
use BEAR\JwtAuth\Exception\InvalidTokenException;
12
use BEAR\JwtAuth\Extractor\TokenExtractorInterface;
13
use Ray\Di\Di\Named;
14
use Ray\Di\ProviderInterface;
15
16
class AuthProvider implements ProviderInterface
17
{
18
    /**
19
     * @var TokenExtractorInterface
20
     */
21
    private $tokenExtractor;
22
23
    /**
24
     * @var JwtEncoderInterface
25
     */
26
    private $jwtEncoder;
27
28
    public function __construct(TokenExtractorInterface $tokenExtractor, JwtEncoderInterface $jwtEncoder)
29
    {
30
        $this->tokenExtractor = $tokenExtractor;
31
        $this->jwtEncoder = $jwtEncoder;
32
    }
33
34
    /**
35
     * @return Auth
36
     */
37
    public function get()
38
    {
39
        $auth = new Auth();
40
        $token = $this->tokenExtractor->extract();
41
        if (!$token) {
42
            return $auth;
43
        }
44
45
        try {
46
            $data = $this->jwtEncoder->decode($token);
47
        } catch (InvalidTokenException $e) {
48
            $auth->isLogin = false;
49
50
            return $auth;
51
        }
52
53
        $auth->userid = $data['userid'];
54
        $auth->username = $data['username'];
55
        $auth->isLogin = true;
56
57
        return $auth;
58
    }
59
}
60