FilterTrait::filterCreatedBy()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 3.1406

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
ccs 3
cts 4
cp 0.75
rs 9.4285
cc 3
eloc 3
nc 2
nop 3
crap 3.1406
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