1 | <?php namespace Arcanedev\LaravelImpersonator\Traits; |
||
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) |
|
31 | |||
32 | /** |
||
33 | * Stop the impersonation. |
||
34 | * |
||
35 | * @return bool |
||
36 | */ |
||
37 | 6 | public function stopImpersonation() |
|
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() |
|
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) |
|
86 | } |
||
87 |
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
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. 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.