Completed
Push — master ( 459c90...29a4ac )
by ARCANEDEV
03:28
created

UserImpersonator::start()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
1
<?php namespace Arcanesoft\Auth\Helpers;
2
3
use Arcanesoft\Contracts\Auth\Models\User;
4
5
/**
6
 * Class     UserImpersonator
7
 *
8
 * @package  Arcanesoft\Auth\Helpers
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
    public static function start(User $user)
25
    {
26
        if ( ! $user->isMember()) return false;
27
28
        session()->put('impersonate', $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
        return true;
31
    }
32
33
    /**
34
     * Stop the user impersonation.
35
     */
36
    public static function stop()
37
    {
38
        session()->forget('impersonate');
39
    }
40
41
    /**
42
     * Check if the impersonation is ongoing.
43
     *
44
     * @return bool
45
     */
46
    public static function isImpersonating()
47
    {
48
        return session()->has('impersonate');
49
    }
50
51
    /**
52
     * Check if the impersonation is enabled.
53
     *
54
     * @return bool
55
     */
56
    public static function isEnabled()
57
    {
58
        return config('arcanesoft.auth.impersonation.enabled');
59
    }
60
}
61