Code Duplication    Length = 50-51 lines in 4 locations

src/Adapters/Guard.php 1 location

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

src/Adapters/JwtAuth.php 1 location

@@ 7-57 (lines=51) @@
4
use Sofa\Revisionable\UserProvider;
5
use Tymon\JWTAuth\JWTAuth as JWT;
6
7
class JwtAuth implements UserProvider
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
}

src/Adapters/Sentinel.php 1 location

@@ 6-55 (lines=50) @@
3
use Sofa\Revisionable\UserProvider;
4
use Cartalyst\Sentinel\Sentinel as SentinelProvider;
5
6
class Sentinel implements UserProvider
7
{
8
    /**
9
     * Auth provider instance.
10
     *
11
     * @var \Cartalyst\Sentinel\Sentinel
12
     */
13
    protected $provider;
14
15
    /**
16
     * Field from the user to be saved as author of the action.
17
     *
18
     * @var string
19
     */
20
    protected $field;
21
22
    /**
23
     * Create adapter instance for Sentinel.
24
     *
25
     * @param SentinelProvider $provider
26
     * @param string $field
27
     */
28
    public function __construct(SentinelProvider $provider, $field = null)
29
    {
30
        $this->provider = $provider;
31
        $this->field    = $field;
32
    }
33
34
    /**
35
     * Get identifier of the currently logged in user.
36
     *
37
     * @return string|null
38
     */
39
    public function getUser()
40
    {
41
        return $this->getUserFieldValue();
42
    }
43
44
    /**
45
     * Get value from the user to be saved as the author.
46
     *
47
     * @return string|null
48
     */
49
    protected function getUserFieldValue()
50
    {
51
        if ($user = $this->provider->getUser()) {
52
            return ($field = $this->field) ? (string) $user->{$field} : $user->getLogin();
53
        }
54
    }
55
}
56

src/Adapters/Sentry.php 1 location

@@ 6-55 (lines=50) @@
3
use Sofa\Revisionable\UserProvider;
4
use Cartalyst\Sentry\Sentry as SentryProvider;
5
6
class Sentry implements UserProvider
7
{
8
    /**
9
     * Auth provider instance.
10
     *
11
     * @var \Cartalyst\Sentry\Sentry
12
     */
13
    protected $provider;
14
    
15
    /**
16
     * Field from the user to be saved as author of the action.
17
     *
18
     * @var string
19
     */
20
    protected $field;
21
    
22
    /**
23
     * Create adapter instance for Sentry.
24
     *
25
     * @param SentryProvider $provider
26
     * @param string $field
27
     */
28
    public function __construct(SentryProvider $provider, $field = null)
29
    {
30
        $this->provider = $provider;
31
        $this->field    = $field;
32
    }
33
34
    /**
35
     * Get identifier of the currently logged in user.
36
     *
37
     * @return string|null
38
     */
39
    public function getUser()
40
    {
41
        return $this->getUserFieldValue();
42
    }
43
44
    /**
45
     * Get value from the user to be saved as the author.
46
     *
47
     * @return string|null
48
     */
49
    protected function getUserFieldValue()
50
    {
51
        if ($user = $this->provider->getUser()) {
52
            return ($field = $this->field) ? (string) $user->{$field} : $user->getLogin();
53
        }
54
    }
55
}
56