Creation   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 4
Bugs 1 Features 2
Metric Value
wmc 12
c 4
b 1
f 2
lcom 1
cbo 0
dl 0
loc 50
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
C bootCreation() 0 36 11
A deletedBy() 0 4 1
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
            $table->update(['deleted_by' => $table->deleted_by]);
23
        });
24
25
        // create a event to happen on saving
26
        static::saving(function($table)  {
27
28
            if (class_exists('Cartalyst\Sentinel\Laravel\Facades\Sentinel'))
29
            {
30
                if (Sentinel::check())
31
                    $table->modified_by = Sentinel::getUser()->id;
32
33
                if (Sentinel::check() && ($table->created_by == null || !($table->created_by > 0)))
34
                    $table->created_by = Sentinel::getUser()->id;
35
            }
36
            else
37
            {
38
                if (!Auth::guest())
39
                    $table->modified_by = Auth::user()->id;
40
41
                if (!Auth::guest() && ($table->created_by == null || !($table->created_by > 0)))
42
                    $table->created_by = Auth::user()->id;
43
            }
44
45
        });
46
47
    }
48
49
    public function deletedBy()
50
    {
51
        return Sentinel::createModel()->find($this->deleted_by);
0 ignored issues
show
Bug introduced by
The property deleted_by 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...
52
    }
53
    
54
}