EntityLockTrait   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 107
Duplicated Lines 15.89 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 17
loc 107
ccs 0
cts 49
cp 0
rs 10
c 0
b 0
f 0
wmc 8
lcom 1
cbo 1

5 Methods

Rating   Name   Duplication   Size   Complexity  
B lockIn() 0 29 2
A getEntityLockModelInstance() 8 8 2
A withoutEntityLock() 0 5 1
A isLocked() 0 5 1
A entityLock() 9 9 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/*
3
 * This file is part of the Laravel Platfourm package.
4
 *
5
 * (c) Avtandil Kikabidze aka LONGMAN <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Longman\Platfourm\Database\Eloquent\EntityLock;
12
13
use Auth;
14
use Carbon\Carbon;
15
use Longman\Platfourm\Database\Eloquent\EntityLock\EntityLock;
16
17
trait EntityLockTrait
18
{
19
20
    protected $entityLock = true;
21
22
    /**
23
     * Boot the trait for the model.
24
     *
25
     * @return void
26
     */
27
/*    public static function bootActionLogTrait()
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
28
    {
29
30
        static::created(function ($model) {
31
            if (!empty($model->actionLogging)) {
32
                static::storeActionLog($model, 'create');
33
            }
34
        });
35
36
        static::updated(function ($model) {
37
            if (!empty($model->actionLogging)) {
38
                static::storeActionLog($model, 'update');
39
            }
40
        });
41
42
        static::deleted(function ($model) {
43
            if (!empty($model->actionLogging)) {
44
                static::storeActionLog($model, 'delete');
45
            }
46
        });
47
48
        if (method_exists(static::class, 'restored')) {
49
            static::restored(function ($model) {
50
                if (!empty($model->actionLogging)) {
51
                    static::storeActionLog($model, 'restore');
52
                }
53
            });
54
        }
55
    }*/
56
57
    public function lockIn()
58
    {
59
        $entityLock = static::getEntityLockModelInstance();
60
61
        $model = $this;
62
63
        $user_id = Auth::user() ? Auth::user()->id : '';
64
65
        $lang = app()->getLocale();
66
67
        $className = get_class($model);
0 ignored issues
show
Unused Code introduced by
$className is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
68
69
        $data = [
70
            'user_id' => $user_id,
71
            'lang' => $lang,
72
            'entity' => static::getModelClassName($model),
73
            'scope' => app()->getScope(),
74
            'action' => $action,
0 ignored issues
show
Bug introduced by
The variable $action does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
75
            'before_data' => $before_data,
0 ignored issues
show
Bug introduced by
The variable $before_data does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
76
            'after_data' => $after_data,
0 ignored issues
show
Bug introduced by
The variable $after_data does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
77
            'request_data' => app('request')->all(),
78
            'url' => app('request')->fullUrl(),
79
            'referer' => app('request')->server->get('HTTP_REFERER'),
80
            'ip_address' => app('request')->ip(),
81
            'user_agent' => app('request')->server->get('HTTP_USER_AGENT'),
82
        ];
83
84
        $entityLock->create($data);
85
    }
86
87 View Code Duplication
    protected static function getEntityLockModelInstance()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
88
    {
89
        $model = EntityLock::class;
90
        if (class_exists(\App\Models\EntityLock::class)) {
91
            $model = \App\Models\EntityLock::class;
92
        }
93
        return new $model;
94
    }
95
96
97
    public function withoutEntityLock()
98
    {
99
        $this->entityLock = false;
100
        return $this;
101
    }
102
103
    public function isLocked()
104
    {
105
        $date = Carbon::now()->subMinutes(5);
106
        return $this->entityLock()->where('locked_at', '>=', $date->toDateTimeString());
107
    }
108
109
    /**
110
     * Polymorphic relationship. Second parameter to morphOne/morphMany
111
     * should be the same as the prefix for the *_id/*_type fields.
112
     */
113 View Code Duplication
    public function entityLock()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
114
    {
115
        $model = EntityLock::class;
116
        if (class_exists(\App\Models\EntityLock::class)) {
117
            $model = \App\Models\EntityLock::class;
118
        }
119
120
        return $this->morphOne($model, 'entity');
0 ignored issues
show
Bug introduced by
It seems like morphOne() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
121
    }
122
123
}
124