TokenTrait::injectToken()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
namespace App\Containers\Authentication\Traits;
4
5
use App\Containers\Authentication\Tasks\ApiGenerateTokenFromObjectTask;
6
use Illuminate\Support\Facades\App;
7
8
/**
9
 * Class TokenTrait.
10
 *
11
 * To be `used` by the `User` Model.
12
 *
13
 * @author Mahmoud Zalt <[email protected]>
14
 */
15
trait TokenTrait
16
{
17
18
    /**
19
     * inject a token in the user model itself.
20
     * if no token provided generate token first.
21
     *
22
     * @param null $token
23
     *
24
     * @return $this
25
     */
26
    public function injectToken($token)
27
    {
28
        // `$this->token` will be attached on the User where this trait is used.
29
        $this->token = $token;
0 ignored issues
show
Bug introduced by
The property token does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
30
31
        return $this;
32
    }
33
34
    /**
35
     * When calling this function, the token will be added on the model.
36
     *
37
     * @return  $this
38
     */
39
    public function withToken()
40
    {
41
        return $this->injectToken(App::make(ApiGenerateTokenFromObjectTask::class)->run($this));
42
    }
43
}
44