Passed
Push — master ( a391b6...85ac84 )
by Tobias
01:43
created

MigrationHelper::addChangedByField()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 5
1
<?php
2
3
namespace TobyMaxham\Helper\Database;
4
5
use Illuminate\Database\Schema\Blueprint;
6
7
class MigrationHelper
8
{
9
    const REFERENTIAL_ACTION_CASCADE = 'CASCADE';
10
    const REFERENTIAL_ACTION_RESTRICT = 'RESTRICT';
11
    const REFERENTIAL_ACTION_SET_NULL = 'SET NULL';
12
    const REFERENTIAL_ACTION_NO_ACTION = 'NO ACTION';
13
    const REFERENTIAL_ACTION_SET_DEFAULT = 'SET DEFAULT';
14
15
    public static function uuid(Blueprint $table, string $comment = null)
16
    {
17
        $table->char('uuid', 36);
18
        $table->unique('uuid', 'uuid');
19
        if (! is_null($comment)) {
20
            $table->comment = $comment;
0 ignored issues
show
Bug introduced by
The property comment does not seem to exist on Illuminate\Database\Schema\Blueprint.
Loading history...
21
        }
22
    }
23
24
    public static function addSoftDelete(Blueprint $table)
25
    {
26
        $table->softDeletes();
27
        $table->index(['deleted_at'], 'deleted_at');
28
    }
29
30
    public static function dropSoftDelete(Blueprint $table)
31
    {
32
        $table->dropIndex('deleted_at');
33
        $table->dropSoftDeletes();
34
    }
35
36
    public static function addChangedByUserFields(Blueprint $table, array $fields = ['created_by', 'updated_by', 'deleted_by'], string $relation = 'users', string $foreign = 'id', string $onDelete = self::REFERENTIAL_ACTION_CASCADE)
37
    {
38
        if (! isset($fields[0]) || ! is_string($fields[0])) {
39
            $fields[0] = 'created_by';
40
        }
41
        if (! isset($fields[1]) || ! is_string($fields[1])) {
42
            $fields[1] = 'updated_by';
43
        }
44
        if (! isset($fields[2]) || ! is_string($fields[2])) {
45
            $fields[2] = 'deleted_by';
46
        }
47
        self::addCreatedByUser($table, $fields[0], $relation, $foreign, $onDelete);
48
        self::addUpdatedByUser($table, $fields[1], $relation, $foreign, $onDelete);
49
        self::addDeletedByUser($table, $fields[2], $relation, $foreign, $onDelete);
50
    }
51
52
    public static function addCreatedByUser(Blueprint $table, string $field = 'created_by', string $relation = 'users', string $foreign = 'id', string $onDelete = self::REFERENTIAL_ACTION_CASCADE)
53
    {
54
        self::addChangedByField($table, $field, $relation, $foreign, $onDelete);
55
    }
56
57
    public static function addUpdatedByUser(Blueprint $table, string $field = 'updated_by', string $relation = 'users', string $foreign = 'id', string $onDelete = self::REFERENTIAL_ACTION_CASCADE)
58
    {
59
        self::addChangedByField($table, $field, $relation, $foreign, $onDelete);
60
    }
61
62
    public static function addDeletedByUser(Blueprint $table, string $field = 'deleted_by', string $relation = 'users', string $foreign = 'id', string $onDelete = self::REFERENTIAL_ACTION_CASCADE)
63
    {
64
        self::addChangedByField($table, $field, $relation, $foreign, $onDelete);
65
    }
66
67
    public static function addChangedByField(Blueprint $table, string $field, string $relation, string $foreign, string $onDelete)
68
    {
69
        $table->unsignedInteger($field)->nullable();
70
        $table->foreign($field)
71
            ->references($foreign)->on($relation)
72
            ->onDelete($onDelete);
73
    }
74
}
75