Completed
Push — master ( eb23e0...5fed5a )
by ARCANEDEV
12s queued 11s
created

CanImpersonate::isSamePerson()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 3.1406

Importance

Changes 0
Metric Value
dl 0
loc 8
c 0
b 0
f 0
cc 3
nc 2
nop 1
ccs 3
cts 4
cp 0.75
crap 3.1406
rs 10
1
<?php namespace Arcanedev\LaravelImpersonator\Traits;
2
3
use Arcanedev\LaravelImpersonator\Contracts\Impersonatable;
4
use Illuminate\Database\Eloquent\Model;
5
6
/**
7
 * Trait     HasImpersonation
8
 *
9
 * @package  Arcanedev\LaravelImpersonator\Traits
10
 * @author   ARCANEDEV <[email protected]>
11
 */
12
trait CanImpersonate
13
{
14
    /* -----------------------------------------------------------------
15
     |  Main Methods
16
     | -----------------------------------------------------------------
17
     */
18
19
    /**
20
     * Impersonate the given user.
21
     *
22
     * @param   \Arcanedev\LaravelImpersonator\Contracts\Impersonatable|mixed  $impersonated
23
     *
24
     * @return  bool
25
     */
26 6
    public function impersonate(Impersonatable $impersonated)
27
    {
28
        /** @var  \Arcanedev\LaravelImpersonator\Contracts\Impersonatable  $this */
29 6
        return impersonator()->start($this, $impersonated);
30
    }
31
32
    /**
33
     * Stop the impersonation.
34
     *
35
     * @return  bool
36
     */
37 6
    public function stopImpersonation()
38
    {
39 6
        return $this->isImpersonated() ? impersonator()->stop() : false;
40
    }
41
42
    /* -----------------------------------------------------------------
43
     |  Check Methods
44
     | -----------------------------------------------------------------
45
     */
46
47
    /**
48
     * Check if the current modal can impersonate other models.
49
     *
50
     * @return  bool
51
     */
52
    abstract public function canImpersonate();
53
54
    /**
55
     * Check if the current model can be impersonated.
56
     *
57
     * @return  bool
58
     */
59
    abstract public function canBeImpersonated();
60
61
    /**
62
     * Check if impersonation is ongoing.
63
     *
64
     * @return  bool
65
     */
66 6
    public function isImpersonated()
67
    {
68 6
        return impersonator()->isImpersonating();
69
    }
70
71
    /**
72
     * Check if the two persons are the same.
73
     *
74
     * @param  \Arcanedev\LaravelImpersonator\Contracts\Impersonatable|mixed  $impersonated
75
     *
76
     * @return bool
77
     */
78 22
    public function isSamePerson($impersonated)
79
    {
80 22
        if ($this instanceof Model && $impersonated instanceof Model) {
81 22
            return $this->is($impersonated);
82
        }
83
84
        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...
85
    }
86
}
87