HasToken::generateToken()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 2
nop 1
1
<?php
2
3
namespace Mayoz\Token;
4
5
use Illuminate\Support\Carbon;
6
7
trait HasToken
8
{
9
    /**
10
     * Get the tokens for the user.
11
     *
12
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
13
     */
14
    public function tokens()
15
    {
16
        return $this->hasMany(config('laravel-tokens.token'));
0 ignored issues
show
Bug introduced by
It seems like hasMany() 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...
17
    }
18
19
    /**
20
     * Generate a new token and returns it.
21
     *
22
     * @param  int  $minute
23
     * @return \Illuminate\Database\Eloquent\Model
24
     */
25
    public function generateToken($minute = null)
26
    {
27
        $expired_at = ((int) $minute > 0)
28
            ? Carbon::now()->addMinutes($minute)
29
            : null;
30
31
        return $this->tokens()->create([
32
            'api_token' => Generator::generate(),
33
            'expired_at' => $expired_at,
34
        ]);
35
    }
36
}
37