FilterTrait   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 85%

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 10
c 2
b 1
f 0
lcom 0
cbo 1
dl 0
loc 69
ccs 17
cts 20
cp 0.85
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A filterAssignTo() 0 6 3
A filterTags() 0 9 2
A filterTitleOrBody() 0 9 2
A filterCreatedBy() 0 6 3
1
<?php
2
3
/*
4
 * This file is part of the Tinyissue package.
5
 *
6
 * (c) Mohamed Alsharaf <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Tinyissue\Model\Traits\Project;
13
14
use Illuminate\Database\Eloquent;
15
use Illuminate\Database\Eloquent\Relations;
16
17
/**
18
 * FilterTrait is trait class containing the methods for filtering database queries of the Project model.
19
 *
20
 * @author Mohamed Alsharaf <[email protected]>
21
 *
22
 * @property static $this
23
 */
24
trait FilterTrait
25
{
26
    /**
27
     * Filter by assign to.
28
     *
29
     * @param Relations\HasMany $query
30
     * @param int               $userId
31
     *
32
     * @return void
33
     */
34 6
    public function filterAssignTo(Relations\HasMany $query, $userId)
35
    {
36 6
        if (!empty($userId) && $userId > 0) {
37 2
            $query->where('assigned_to', '=', (int) $userId);
38
        }
39 6
    }
40
41
    /**
42
     * Filter by tag.
43
     *
44
     * @param Relations\HasMany $query
45
     * @param string            $tags
46
     *
47
     * @return void
48
     */
49 5
    public function filterTags(Relations\HasMany $query, $tags)
50
    {
51 5
        if (!empty($tags)) {
52
            $tagIds = array_map('trim', explode(',', $tags));
53
            $query->whereHas('tags', function (Eloquent\Builder $query) use ($tagIds) {
54
                $query->whereIn('id', $tagIds);
55
            });
56
        }
57 5
    }
58
59
    /**
60
     * Filter the title or body by keyword.
61
     *
62
     * @param Relations\HasMany $query
63
     * @param string            $keyword
64
     *
65
     * @return void
66
     */
67 6
    public function filterTitleOrBody(Relations\HasMany $query, $keyword)
68
    {
69 6
        if (!empty($keyword)) {
70 2
            $query->where(function (Eloquent\Builder $query) use ($keyword) {
71 2
                $query->where('title', 'like', '%' . $keyword . '%');
72 2
                $query->orWhere('body', 'like', '%' . $keyword . '%');
73 2
            });
74
        }
75 6
    }
76
77
    /**
78
     * Filter by created by.
79
     *
80
     * @param Relations\HasMany $query
81
     * @param int               $userId
82
     * @param bool              $enabled
83
     *
84
     * @return void
85
     */
86 2
    public function filterCreatedBy(Relations\HasMany $query, $userId, $enabled = false)
87
    {
88 2
        if (true === $enabled && $userId > 0) {
89
            $query->where('created_by', '=', $userId);
90
        }
91 2
    }
92
}
93