CanImpersonate   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 90%

Importance

Changes 0
Metric Value
dl 0
loc 75
c 0
b 0
f 0
ccs 9
cts 10
cp 0.9
rs 10
wmc 7
lcom 0
cbo 2

6 Methods

Rating   Name   Duplication   Size   Complexity  
A impersonate() 0 5 1
A stopImpersonation() 0 4 2
canImpersonate() 0 1 ?
canBeImpersonated() 0 1 ?
A isImpersonated() 0 4 1
A isSamePerson() 0 8 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Arcanedev\LaravelImpersonator\Traits;
6
7
use Arcanedev\LaravelImpersonator\Contracts\Impersonatable;
8
use Illuminate\Database\Eloquent\Model;
9
10
/**
11
 * Trait     CanImpersonate
12
 *
13
 * @author   ARCANEDEV <[email protected]>
14
 */
15
trait CanImpersonate
16
{
17
    /* -----------------------------------------------------------------
18
     |  Main Methods
19
     | -----------------------------------------------------------------
20
     */
21
22
    /**
23
     * Impersonate the given user.
24
     *
25
     * @param   \Arcanedev\LaravelImpersonator\Contracts\Impersonatable|mixed  $impersonated
26
     *
27
     * @return  bool
28
     */
29 12
    public function impersonate(Impersonatable $impersonated)
30
    {
31
        /** @var  \Arcanedev\LaravelImpersonator\Contracts\Impersonatable  $this */
32 12
        return impersonator()->start($this, $impersonated);
33
    }
34
35
    /**
36
     * Stop the impersonation.
37
     *
38
     * @return  bool
39
     */
40 12
    public function stopImpersonation()
41
    {
42 12
        return $this->isImpersonated() ? impersonator()->stop() : false;
43
    }
44
45
    /* -----------------------------------------------------------------
46
     |  Check Methods
47
     | -----------------------------------------------------------------
48
     */
49
50
    /**
51
     * Check if the current modal can impersonate other models.
52
     *
53
     * @return  bool
54
     */
55
    abstract public function canImpersonate();
56
57
    /**
58
     * Check if the current model can be impersonated.
59
     *
60
     * @return  bool
61
     */
62
    abstract public function canBeImpersonated();
63
64
    /**
65
     * Check if impersonation is ongoing.
66
     *
67
     * @return  bool
68
     */
69 12
    public function isImpersonated()
70
    {
71 12
        return impersonator()->isImpersonating();
72
    }
73
74
    /**
75
     * Check if the two persons are the same.
76
     *
77
     * @param  \Arcanedev\LaravelImpersonator\Contracts\Impersonatable|mixed  $impersonated
78
     *
79
     * @return bool
80
     */
81 44
    public function isSamePerson($impersonated)
82
    {
83 44
        if ($this instanceof Model && $impersonated instanceof Model) {
84 44
            return $this->is($impersonated);
85
        }
86
87
        return $this->getAuthIdentifier() == $impersonated->getAuthIdentifier();
0 ignored issues
show
Bug introduced by
It seems like getAuthIdentifier() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
88
    }
89
}
90