Completed
Pull Request — master (#106)
by Lloric Mayuga
01:14
created

Impersonate::getCanImpersonate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Lab404\Impersonate\Models;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Illuminate\Support\Collection;
7
use Lab404\Impersonate\Services\ImpersonateManager;
8
9
trait Impersonate
10
{
11
    /**
12
     * Return true or false if the user can impersonate an other user.
13
     *
14
     * @param void
15
     * @return  bool
16
     */
17
    public function canImpersonate()
18
    {
19
        return true;
20
    }
21
22
    /**
23
     * Return true or false if the user can be impersonate.
24
     *
25
     * @param void
26
     * @return  bool
27
     */
28
    public function canBeImpersonated()
29
    {
30
        return true;
31
    }
32
33
    /**
34
     * Impersonate the given user.
35
     *
36
     * @param Model       $user
37
     * @param string|null $guardName
38
     * @return  bool
39
     */
40
    public function impersonate(Model $user, $guardName = null)
41
    {
42
        return app(ImpersonateManager::class)->take($this, $user, $guardName);
43
    }
44
45
    /**
46
     * Check if the current user is impersonated.
47
     *
48
     * @param void
49
     * @return  bool
50
     */
51
    public function isImpersonated()
52
    {
53
        return app(ImpersonateManager::class)->isImpersonating();
54
    }
55
56
    /**
57
     * Leave the current impersonation.
58
     *
59
     * @param void
60
     * @return  bool
61
     */
62
    public function leaveImpersonation()
63
    {
64
        if ($this->isImpersonated()) {
65
            return app(ImpersonateManager::class)->leave();
66
        }
67
    }
68
69
    public static function getCanImpersonate(): Collection
70
    {
71
         return collect(static::cursor())->filter(function ($model){
72
            return $model->canBeImpersonated();
73
        });
74
    }
75
}
76