Completed
Push — develop ( 544b7b...88e84c )
by Mohamed
08:50 queued 47s
created

FilterTrait::filterCreatedBy()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
ccs 0
cts 0
cp 0
rs 9.4285
cc 3
eloc 3
nc 2
nop 3
crap 12
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
    public function filterCreatedBy(Relations\HasMany $query, $userId, $enabled = false)
92
    {
93
        if (true === $enabled && $userId > 0) {
94
            $query->where('created_by', '=', $userId);
95
        }
96
    }
97
}
98