Completed
Branch master (beb88f)
by Mohamed
05:11 queued 03:39
created

Category::children()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Moo\FlashCard\Entity;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Moo\FlashCard\Traits\Validatable;
7
use Illuminate\Database\Eloquent\Builder;
8
9
/**
10
 * Category is the entity class that represents a record from the database.
11
 *
12
 * @author Mohamed Alsharaf <[email protected]>
13
 */
14
class Category extends Model
15
{
16
    use  Validatable;
17
18
    /**
19
     * Constant for card active status
20
     *
21
     * @var bool
22
     */
23
    const ACTIVE = true;
24
    /**
25
     * Constant for card inactive status
26
     *
27
     * @var int
28
     */
29
    const INACTIVE = false;
30
31
    /**
32
     * Timestamp enabled.
33
     *
34
     * @var bool
35
     */
36
    public $timestamps = true;
37
38
    /**
39
     * Name of database table.
40
     *
41
     * @var string
42
     */
43
    protected $table = 'card_category';
44
45
    /**
46
     * List of allowed columns to be used in $this->fill().
47
     *
48
     * @var array
49
     */
50
    protected $fillable = ['title', 'description', 'parent', 'active', 'color'];
51
52
    /**
53
     * Get collection of cards in the category.
54
     */
55
    public function cards()
56
    {
57
        return $this->hasMany(Card::class);
58
    }
59
60
    /**
61
     * Get collection of children categories
62
     */
63
    public function children()
64
    {
65
        return $this->hasMany(Category::class, 'parent');
0 ignored issues
show
Coding Style introduced by
As per coding style, self should be used for accessing local static members.

This check looks for accesses to local static members using the fully qualified name instead of self::.

<?php

class Certificate {
    const TRIPLEDES_CBC = 'ASDFGHJKL';

    private $key;

    public function __construct()
    {
        $this->key = Certificate::TRIPLEDES_CBC;
    }
}

While this is perfectly valid, the fully qualified name of Certificate::TRIPLEDES_CBC could just as well be replaced by self::TRIPLEDES_CBC. Referencing local members with self:: assured the access will still work when the class is renamed, makes it perfectly clear that the member is in fact local and will usually be shorter.

Loading history...
66
    }
67
68
    /**
69
     * Get the parent category
70
     */
71
    public function parent()
72
    {
73
        return $this->belongsTo(Category::class, 'parent');
0 ignored issues
show
Coding Style introduced by
As per coding style, self should be used for accessing local static members.

This check looks for accesses to local static members using the fully qualified name instead of self::.

<?php

class Certificate {
    const TRIPLEDES_CBC = 'ASDFGHJKL';

    private $key;

    public function __construct()
    {
        $this->key = Certificate::TRIPLEDES_CBC;
    }
}

While this is perfectly valid, the fully qualified name of Certificate::TRIPLEDES_CBC could just as well be replaced by self::TRIPLEDES_CBC. Referencing local members with self:: assured the access will still work when the class is renamed, makes it perfectly clear that the member is in fact local and will usually be shorter.

Loading history...
74
    }
75
76
    /**
77
     * Get number of cards in a category
78
     *
79
     * @return int
80
     */
81
    public function countCards()
82
    {
83
        return $this->cards()->count();
84
    }
85
86
    /**
87
     * Get collection of validation rules for model attributes
88
     *
89
     * @return array
90
     */
91
    protected function getRules(): array
92
    {
93
        return [
94
            'title' => 'required|max:255|min:5',
95
        ];
96
    }
97
98
    /**
99
     * Scope to filter by active categories
100
     *
101
     * @param Builder $query
102
     * @return Builder
103
     */
104
    public function scopeActive(Builder $query): Builder
105
    {
106
        return $query->where('active', '=', static::ACTIVE);
107
    }
108
109
    /**
110
     * Score to filter by LIKE search
111
     *
112
     * @param Builder $query
113
     * @param string $keyword
114
     * @return Builder
115
     */
116 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...
117
    {
118
        $query->where('title', 'LIKE', '%' . $keyword . '%');
119
        $query->orWhere('description', 'LIKE', '%' . $keyword . '%');
120
121
        return $query;
122
    }
123
}
124