Completed
Push — develop ( 544b7b...428c41 )
by Mohamed
07:52
created

FilterTrait   A

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 0 Features 0
Metric Value
wmc 10
c 2
b 0
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
use Illuminate\Database\Query;
17
use Tinyissue\Model\Project;
18
19
/**
20
 * FilterTrait is trait class containing the methods for filtering database queries of the Project model.
21
 *
22
 * @author Mohamed Alsharaf <[email protected]>
23
 *
24
 * @property int           $id
25
 *
26
 * @method   Query\Builder where($column, $operator = null, $value = null, $boolean = 'and')
27
 * @method   Query\Builder join($table, $one, $operator = null, $two = null, $type = 'inner', $where = false)
28
 */
29
trait FilterTrait
30
{
31
    /**
32
     * Filter by assign to.
33
     *
34
     * @param Relations\HasMany $query
35
     * @param int               $userId
36
     *
37
     * @return void
38
     */
39 6
    public function filterAssignTo(Relations\HasMany $query, $userId)
40
    {
41 6
        if (!empty($userId) && $userId > 0) {
42 2
            $query->where('assigned_to', '=', (int) $userId);
43
        }
44 6
    }
45
46
    /**
47
     * Filter by tag.
48
     *
49
     * @param Relations\HasMany $query
50
     * @param string            $tags
51
     *
52
     * @return void
53
     */
54 5
    public function filterTags(Relations\HasMany $query, $tags)
55
    {
56 5
        if (!empty($tags)) {
57
            $tagIds = array_map('trim', explode(',', $tags));
58
            $query->whereHas('tags', function (Eloquent\Builder $query) use ($tagIds) {
59
                $query->whereIn('id', $tagIds);
60
            });
61
        }
62 5
    }
63
64
    /**
65
     * Filter the title or body by keyword.
66
     *
67
     * @param Relations\HasMany $query
68
     * @param string            $keyword
69
     *
70
     * @return void
71
     */
72 6
    public function filterTitleOrBody(Relations\HasMany $query, $keyword)
73
    {
74 6
        if (!empty($keyword)) {
75 2
            $query->where(function (Eloquent\Builder $query) use ($keyword) {
76 2
                $query->where('title', 'like', '%' . $keyword . '%');
77 2
                $query->orWhere('body', 'like', '%' . $keyword . '%');
78 2
            });
79
        }
80 6
    }
81
82
    /**
83
     * Filter by created by.
84
     *
85
     * @param Relations\HasMany $query
86
     * @param int               $userId
87
     * @param bool              $enabled
88
     *
89
     * @return void
90
     */
91 2
    public function filterCreatedBy(Relations\HasMany $query, $userId, $enabled = false)
92
    {
93 2
        if (true === $enabled && $userId > 0) {
94
            $query->where('created_by', '=', $userId);
95
        }
96 2
    }
97
}
98