Completed
Push — master ( a99ce7...3e17cc )
by Joao
02:13
created

Creation   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 3
Bugs 1 Features 1
Metric Value
wmc 11
c 3
b 1
f 1
lcom 0
cbo 0
dl 0
loc 43
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
C bootCreation() 0 34 11
1
<?php namespace jlourenco\support\Traits;
2
3
use Cartalyst\Sentinel\Laravel\Facades\Sentinel;
4
5
trait Creation {
6
7
    /**
8
     * Boot the creation trait for a model.
9
     *
10
     * @return void
11
     */
12
    public static function bootCreation()
13
    {
14
15
        // create a event to happen on deleting
16
        static::deleting(function($table)  {
17
            if (class_exists('Cartalyst\Sentinel\Laravel\Facades\Sentinel'))
18
                $table->deleted_by = Sentinel::getUser()->id;
19
            else
20
                $table->deleted_by = Auth::user()->id;
21
        });
22
23
        // create a event to happen on saving
24
        static::saving(function($table)  {
25
26
            if (class_exists('Cartalyst\Sentinel\Laravel\Facades\Sentinel'))
27
            {
28
                if (Sentinel::check())
29
                    $table->modified_by = Sentinel::getUser()->id;
30
31
                if (Sentinel::check() && ($table->created_by == null || !($table->created_by > 0)))
32
                    $table->created_by = Sentinel::getUser()->id;
33
            }
34
            else
35
            {
36
                if (!Auth::guest())
37
                    $table->modified_by = Auth::user()->id;
38
39
                if (!Auth::guest() && ($table->created_by == null || !($table->created_by > 0)))
40
                    $table->created_by = Auth::user()->id;
41
            }
42
43
        });
44
45
    }
46
47
}