Test Failed
Push — main ( 2b582c...894963 )
by Rafael
05:38
created

User   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 4
eloc 21
dl 0
loc 40
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A saveToken() 0 4 1
A getToken() 0 3 1
A createTable() 0 10 1
A createAdmin() 0 8 1
1
<?php
2
3
namespace CoreModules\Admin\Model;
4
5
use Alxarafe\Base\Model\Model;
6
use Illuminate\Database\Capsule\Manager as DB;
7
use Illuminate\Database\Schema\Blueprint;
8
9
class User extends Model
10
{
11
    protected $table = 'users';
12
    protected $fillable = ['name', 'email', 'password', 'token', 'is_admin'];
13
    protected $hidden = ['password', 'token'];
14
15
    public static function createTable(): void
16
    {
17
        DB::schema()->create((new static())->table, function (Blueprint $table) {
18
            $table->increments('id');
19
            $table->string('name');
20
            $table->string('email')->unique();
21
            $table->string('password');
22
            $table->string('token')->nullable();
23
            $table->boolean('is_admin')->default(false);
24
            $table->timestamps();
25
        });
26
    }
27
28
    public static function createAdmin(): bool
29
    {
30
        return (null !== User::create([
31
                'name' => 'admin',
32
                'email' => '[email protected]',
33
                'password' => password_hash('password', PASSWORD_ARGON2ID),
34
                'token' => null,
35
                'is_admin' => true,
36
            ])
37
        );
38
    }
39
40
    public function saveToken($token)
41
    {
42
        $this->attributes['token'] = $token;
43
        return $this->save();
44
    }
45
46
    public function getToken()
47
    {
48
        return $this->attributes['token'];
49
    }
50
}
51