Card   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 158
Duplicated Lines 4.43 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 64.71%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 8
dl 7
loc 158
ccs 22
cts 34
cp 0.6471
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A category() 0 4 1
A cardViews() 0 4 1
A getShortModifiedAttribute() 0 8 2
A getSlugOptions() 0 7 1
A getRules() 0 13 1
A scopeActive() 0 4 1
A scopeSearch() 7 7 1
A incrementViews() 0 17 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
namespace Moo\FlashCard\Entity;
4
5
use Illuminate\Database\Eloquent\Builder;
6
use Illuminate\Database\Eloquent\Model;
7
use Illuminate\Validation\Rule;
8
use Moo\FlashCard\Traits\Validatable;
9
use Spatie\Sluggable\HasSlug;
10
use Spatie\Sluggable\SlugOptions;
11
12
/**
13
 * Card is the entity class that represents a record from the database.
14
 *
15
 * @author Mohamed Alsharaf <[email protected]>
16
 */
17
class Card extends Model
18
{
19
    use HasSlug, Validatable;
20
21
    /**
22
     * Constant for card active status
23
     *
24
     * @var bool
25
     */
26
    const ACTIVE = true;
27
    /**
28
     * Constant for card inactive status
29
     *
30
     * @var int
31
     */
32
    const INACTIVE = false;
33
34
    /**
35
     * Max size of slug
36
     *
37
     * @var int
38
     */
39
    const SLUG_SIZE = 100;
40
41
    /**
42
     * Timestamp enabled.
43
     *
44
     * @var bool
45
     */
46
    public $timestamps = true;
47
    /**
48
     * Name of database table.
49
     *
50
     * @var string
51
     */
52
    protected $table = 'card';
53
    /**
54
     * List of allowed columns to be used in $this->fill().
55
     *
56
     * @var array
57
     */
58
    protected $fillable = ['title', 'slug', 'active', 'content', 'meta_description', 'category_id'];
59
60
    protected $appends = ['short_modified'];
61
62
    protected $casts = [
63
        'active' => 'boolean',
64
    ];
65
66
    /**
67
     * Get the category this card belong to
68
     */
69 2
    public function category()
70
    {
71 2
        return $this->belongsTo(Category::class);
72
    }
73
74
    /**
75
     * Get collection of card views
76
     */
77
    public function cardViews()
78
    {
79
        return $this->hasMany(CardView::class);
80
    }
81
82
    /**
83
     * Get formatted version of updated_at attribute
84
     *
85
     * @return string
86
     */
87 5
    public function getShortModifiedAttribute(): string
88
    {
89 5
        if (!is_null($this->updated_at)) {
90 5
            return (string) $this->updated_at->format('F d, Y');
91
        }
92
93
        return '';
94
    }
95
96
    /**
97
     * Get the options for generating the slug.
98
     */
99 11
    public function getSlugOptions(): SlugOptions
100
    {
101 11
        return SlugOptions::create()
102 11
            ->generateSlugsFrom('title')
103 11
            ->saveSlugsTo('slug')
104 11
            ->slugsShouldBeNoLongerThan(static::SLUG_SIZE);
105
    }
106
107
    /**
108
     * Get collection of validation rules for model attributes
109
     *
110
     * @return array
111
     */
112 11
    protected function getRules(): array
113
    {
114
        return [
115 11
            'title'       => 'required|max:255|min:5',
116 11
            'category_id' => 'required',
117
            'slug'        => [
118 11
                'required',
119
                // Ensure slug unique except when editing
120 11
                Rule::unique($this->table, 'slug')->ignore($this),
121 11
                'max:' . static::SLUG_SIZE,
122
            ],
123
        ];
124
    }
125
126
    /**
127
     * Score to filter by active cards
128
     *
129
     * @param  Builder $query
130
     * @return Builder
131
     */
132 6
    public function scopeActive(Builder $query): Builder
133
    {
134 6
        return $query->where('active', '=', static::ACTIVE);
135
    }
136
137
    /**
138
     * Score to filter by LIKE search
139
     *
140
     * @param  Builder $query
141
     * @param  string  $keyword
142
     * @return Builder
143
     */
144 3 View Code Duplication
    public function scopeSearch(Builder $query, string $keyword): Builder
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...
145
    {
146 3
        $query->where('title', 'LIKE', '%' . $keyword . '%');
147 3
        $query->orWhere('content', 'LIKE', '%' . $keyword . '%');
148
149 3
        return $query;
150
    }
151
152
    /**
153
     * Update card view counter by 1 and insert record in card view table
154
     *
155
     * @param string $ip
156
     */
157
    public function incrementViews(string $ip): void
158
    {
159
        try {
160
            // Insert record in card view table
161
            $cardView = new CardView([
162
                'card_id' => $this->id,
163
                'ip'      => $ip,
164
            ]);
165
            $cardView->save();
166
167
            // Increase the view counter by 1
168
            $this->views = $this->views + 1;
169
            $this->save();
170
        } catch (\Exception $exception) {
171
            // We ignore any error as this is just logging of views!
172
        }
173
    }
174
}
175