QueryTrait::getTagByName()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
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\Tag;
13
14
use Illuminate\Database\Eloquent;
15
16
/**
17
 * QueryTrait is trait class containing the database queries methods for the Tag model.
18
 *
19
 * @author Mohamed Alsharaf <[email protected]>
20
 *
21
 * @property static $this
22
 */
23
trait QueryTrait
24
{
25
    /**
26
     * Returns collection of all groups and eager load their tags.
27
     *
28
     * @return Eloquent\Collection
29
     */
30 34
    public function getGroupTags()
31
    {
32 34
        return $this->with([
0 ignored issues
show
Bug introduced by
It seems like with() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
33
            'tags' => function (Eloquent\Relations\HasMany $query) {
34
                $query->where(function (Eloquent\Builder $query) {
35 34
                    $query->where('role_limit', '<=', $this->getLoggedUser()->role_id);
36 34
                    $query->orWhere('role_limit', '=', null);
37 34
                });
38 34
            },
39
        ])
40 34
            ->where('group', '=', true)->orderBy('group', 'DESC')->orderBy('name', 'ASC')->get();
41
    }
42
43
    /**
44
     * Search tags by name.
45
     *
46
     * @param string $term
47
     *
48
     * @return Eloquent\Collection|static[]
49
     */
50
    public function searchTags($term)
51
    {
52
        return $this->with('parent')->where('name', 'like', '%' . $term . '%')->where('parent_id', '<>', 0)->get();
0 ignored issues
show
Bug introduced by
It seems like with() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
53
    }
54
55
    /**
56
     * Returns tag groups list.
57
     *
58
     * @return array
59
     */
60
    public function groupsDropdown()
61
    {
62 30
        return $this->getGroups()->map(function ($group) {
63 30
            $group->keyname = 'tag:' . $group->id;
64 30
            $group->name = ucwords($group->name);
65
66 30
            return $group;
67 30
        })->lists('name', 'keyname')->all();
68
    }
69
70
    /**
71
     * Returns collection of all groups.
72
     *
73
     * @return Eloquent\Collection
74
     */
75 32
    public function getGroups()
76
    {
77 32
        return $this->where('group', '=', true)->orderBy('name', 'ASC')->get();
0 ignored issues
show
Bug introduced by
It seems like where() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
78
    }
79
80
    /**
81
     * Return tag by name.
82
     *
83
     * @param string $name
84
     *
85
     * @return static
86
     */
87 12
    public function getTagByName($name)
88
    {
89 12
        return static::where('name', '=', $name)->first();
90
    }
91
92
    /**
93
     * Returns collection of tags in status group.
94
     *
95
     * @return Eloquent\Relations\HasMany
96
     */
97 7
    public function getStatusTags()
98
    {
99 7
        return $this->getTagByName('status')->tags();
0 ignored issues
show
Bug introduced by
It seems like tags() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
100
    }
101
102
    /**
103
     * Returns collection of tags in type group.
104
     *
105
     * @return Eloquent\Collection
106
     */
107 2
    public function getTypeTags()
108
    {
109 2
        return $this->getTagByName('type')->tags();
0 ignored issues
show
Bug introduced by
It seems like tags() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
110
    }
111
112
    /**
113
     * Returns collection of tags in type group.
114
     *
115
     * @return Eloquent\Collection
116
     */
117
    public function getResolutionTags()
118
    {
119
        return $this->getTagByName('resolution')->tags();
0 ignored issues
show
Bug introduced by
It seems like tags() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
120
    }
121
122
    abstract public function getLoggedUser();
123
}
124