ModelEventLogger::getActionName()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4.0218

Importance

Changes 0
Metric Value
cc 4
nc 4
nop 1
dl 0
loc 16
ccs 8
cts 9
cp 0.8889
crap 4.0218
rs 9.7333
c 0
b 0
f 0
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: Djunehor
5
 * Date: 10/5/2019
6
 * Time: 1:27 AM.
7
 */
8
9
namespace Djunehor\EventRevert;
10
11
use Djunehor\EventRevert\ModelLog as Activity;
12
use Illuminate\Database\Eloquent\Model;
13
14
/**
15
 *  Automatically Log Add, Update, Delete events of Model.
16
 */
17
trait ModelEventLogger
18
{
19
    /**
20
     * Automatically boot with Model, and register Events handler.
21
     */
22 14
    protected static function boot()
23
    {
24 14
        parent::boot();
25
26 14
        foreach (static::getRecordActivityEvents() as $eventName) {
27
            static::$eventName(function (Model $model) use ($eventName) {
28
                try {
29 14
                    $reflect = new \ReflectionClass($model);
30
31 14
                    return Activity::create([
32 14
                        'causer_id'     => auth()->id(),
0 ignored issues
show
Bug introduced by
The method id does only exist in Illuminate\Contracts\Auth\Guard, but not in Illuminate\Contracts\Auth\Factory.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
33 14
                        'causer_type'     => auth()->check() ? get_class(auth()->user()) : null,
0 ignored issues
show
Bug introduced by
The method check does only exist in Illuminate\Contracts\Auth\Guard, but not in Illuminate\Contracts\Auth\Factory.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
Bug introduced by
The method user does only exist in Illuminate\Contracts\Auth\Guard, but not in Illuminate\Contracts\Auth\Factory.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
34 14
                        'subject_id'   => $model->id,
35 14
                        'subject_type' => get_class($model),
36 14
                        'action'      => static::getActionName($eventName),
37 14
                        'description' => ucfirst($eventName).' a '.$reflect->getShortName(),
38 14
                        'old'     => json_encode($model->getOriginal()),
39 14
                        'new'     => json_encode($model->getDirty()),
40 14
                        'route'     => (php_sapi_name() === 'cli' or defined('STDIN')) ? null : request()->url(),
41
                    ]);
42
                } catch (\Exception $e) {
43
                    echo $e->getMessage();
44
                }
45 14
            });
46
        }
47 14
    }
48
49
    /**
50
     * Set the default events to be recorded if the $recordEvents
51
     * property does not exist on the model.
52
     *
53
     * @return array
54
     */
55 14
    protected static function getRecordActivityEvents()
56
    {
57 14
        if (isset(static::$recordEvents)) {
58
            return static::$recordEvents;
59
        }
60
61
        return [
62 14
            'created',
63
            'updated',
64
            'deleted',
65
//			'inserted',
66
        ];
67
    }
68
69
    /**
70
     * Return Suitable action name for Supplied Event.
71
     *
72
     * @param $event
73
     * @return string
74
     */
75 14
    protected static function getActionName($event)
76
    {
77 14
        switch (strtolower($event)) {
78 14
            case 'created':
79 14
                return 'create';
80
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
81 7
            case 'updated':
82 6
                return 'update';
83
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
84 1
            case 'deleted':
85 1
                return 'delete';
86
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
87
            default:
88
                return 'unknown';
89
        }
90
    }
91
}
92