Completed
Branch master (beb88f)
by Mohamed
05:24
created

Card::getShortModifiedAttribute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
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
    public function category()
70
    {
71
        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
    public function getShortModifiedAttribute(): string
88
    {
89
        return (string)$this->updated_at->format('F d, Y');
90
    }
91
92
    /**
93
     * Get the options for generating the slug.
94
     */
95
    public function getSlugOptions(): SlugOptions
96
    {
97
        return SlugOptions::create()
98
            ->generateSlugsFrom('title')
99
            ->saveSlugsTo('slug')
100
            ->slugsShouldBeNoLongerThan(static::SLUG_SIZE);
101
    }
102
103
    /**
104
     * Get collection of validation rules for model attributes
105
     *
106
     * @return array
107
     */
108
    protected function getRules(): array
109
    {
110
        return [
111
            'title' => 'required|max:255|min:5',
112
            'category_id' => 'required',
113
            'slug' => [
114
                'required',
115
                // Ensure slug unique except when editing
116
                Rule::unique($this->table, 'slug')->ignore($this),
117
                'max:' . static::SLUG_SIZE,
118
            ]
119
        ];
120
    }
121
122
    /**
123
     * Score to filter by active cards
124
     *
125
     * @param Builder $query
126
     * @return Builder
127
     */
128
    public function scopeActive(Builder $query): Builder
129
    {
130
        return $query->where('active', '=', static::ACTIVE);
131
    }
132
133
    /**
134
     * Score to filter by LIKE search
135
     *
136
     * @param Builder $query
137
     * @param string $keyword
138
     * @return Builder
139
     */
140 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...
141
    {
142
        $query->where('title', 'LIKE', '%' . $keyword . '%');
143
        $query->orWhere('content', 'LIKE', '%' . $keyword . '%');
144
145
        return $query;
146
    }
147
148
    /**
149
     * Update card view counter by 1 and insert record in card view table
150
     *
151
     * @param string $ip
152
     */
153
    public function incrementViews(string $ip): void
154
    {
155
        try {
156
            // Insert record in card view table
157
            $cardView = new CardView([
158
                'card_id' => $this->id,
159
                'ip' => $ip,
160
            ]);
161
            $cardView->save();
162
163
            // Increase the view counter by 1
164
            $this->views = $this->views + 1;
165
            $this->save();
166
        } catch (\Exception $exception) {
167
            // We ignore any error as this is just logging of views!
168
        }
169
    }
170
}
171