Completed
Push — master ( e0683b...eed8e4 )
by ARCANEDEV
09:48 queued 08:04
created

UserImpersonator::isEnabled()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 2
cts 2
cp 1
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php namespace Arcanedev\LaravelAuth\Services;
2
3
use Arcanesoft\Contracts\Auth\Models\User;
4
5
/**
6
 * Class     UserImpersonator
7
 *
8
 * @package  Arcanedev\LaravelAuth\Services
9
 * @author   ARCANEDEV <[email protected]>
10
 */
11
class UserImpersonator
12
{
13
    /* ------------------------------------------------------------------------------------------------
14
     |  Main Functions
15
     | ------------------------------------------------------------------------------------------------
16
     */
17
    /**
18
     * Start the user impersonation.
19
     *
20
     * @param  \Arcanesoft\Contracts\Auth\Models\User  $user
21
     *
22
     * @return bool
23
     */
24 9
    public static function start(User $user)
25
    {
26 9
        if ( ! $user->canBeImpersonated()) return false;
27
28 9
        session()->put(self::getKey(), $user->id);
0 ignored issues
show
Bug introduced by
Accessing id on the interface Arcanesoft\Contracts\Auth\Models\User suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
29
30 9
        return true;
31
    }
32
33
    /**
34
     * Stop the user impersonation.
35
     */
36 9
    public static function stop()
37
    {
38 9
        session()->forget(self::getKey());
39 9
    }
40
41
    /**
42
     * Check if the impersonation is ongoing.
43
     *
44
     * @return bool
45
     */
46 9
    public static function isImpersonating()
47
    {
48 9
        return session()->has(self::getKey());
49
    }
50
51
    /**
52
     * Check if the impersonation is enabled.
53
     *
54
     * @return bool
55
     */
56 18
    public static function isEnabled()
57
    {
58 18
        return config('laravel-auth.impersonation.enabled', false);
59
    }
60
61
    /**
62
     * Get the session key.
63
     *
64
     * @return string
65
     */
66 9
    public static function getKey()
67
    {
68 9
        return config('laravel-auth.impersonation.key', 'impersonate');
69
    }
70
71
    /**
72
     * Get the user id.
73
     *
74
     * @return mixed
75
     */
76 9
    public static function getUserId()
77
    {
78 9
        return session()->get(self::getKey(), null);
79
    }
80
}
81