JwtAuth::getUser()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 4
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 4
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php namespace Sofa\Revisionable\Adapters;
2
3
4
use Sofa\Revisionable\UserProvider;
5
use Tymon\JWTAuth\JWTAuth as JWT;
6
7 View Code Duplication
class JwtAuth implements UserProvider
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
8
{
9
10
    /**
11
     * Auth provider instance.
12
     *
13
     * @var \Tymon\JWTAuth\JWTAuth
14
     */
15
    protected $provider;
16
17
    /**
18
     * Field from the user to be saved as author of the action.
19
     *
20
     * @var string
21
     */
22
    protected $field;
23
24
    /**
25
     * Create adapter instance for Illuminate Guard.
26
     *
27
     * @param \Tymon\JWTAuth\JWTAuth $provider
28
     * @param string $field
29
     */
30
    public function __construct(JWT $provider, $field = null)
31
    {
32
        $this->provider = $provider;
33
        $this->field = $field;
34
    }
35
36
    /**
37
     * Get identifier of the currently logged in user.
38
     *
39
     * @return string|null
40
     */
41
    public function getUser()
42
    {
43
        return $this->getUserFieldValue();
44
    }
45
46
    /**
47
     * Get value from the user to be saved as the author.
48
     *
49
     * @return string|null
50
     */
51
    protected function getUserFieldValue()
52
    {
53
        if ($user = $this->provider->parseToken()->toUser()) {
54
            return ($field = $this->field) ? (string)$user->{$field} : $this->provider->getIdentifier();
55
        }
56
    }
57
}