Completed
Push — master ( 91f8d9...64265e )
by Mohamed
09:45 queued 06:57
created

QueryTrait::getGroups()   A

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 0
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
use Illuminate\Database\Query;
16
use Tinyissue\Model\Role;
17
use Tinyissue\Model\Tag as TagModel;
18
19
/**
20
 * QueryTrait is trait class containing the database queries methods for the Tag model.
21
 *
22
 * @author Mohamed Alsharaf <[email protected]>
23
 *
24
 * @method Eloquent\Builder with($relations)
25
 * @method Eloquent\Model   where($column, $operator = null, $value = null, $boolean = 'and')
26
 * @method Query\Builder    whereIn($column, $values, $boolean = 'and', $not = false)
27
 */
28
trait QueryTrait
29
{
30
    /**
31
     * Returns collection of all groups and eager load their tags.
32
     *
33
     * @return Eloquent\Collection
34
     */
35 31
    public function getGroupTags()
36
    {
37 31
        return $this->with([
38
            'tags' => function (Eloquent\Relations\HasMany $query) {
39
                $query->where(function(Eloquent\Builder $query) {
40 31
                    $query->where('role_limit', '<=', auth()->user()->role_id);
0 ignored issues
show
Bug introduced by
Accessing role_id on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
41 31
                    $query->orWhere('role_limit', '=', null);
42 31
                });
43 31
            },
44
        ])
45 31
            ->where('group', '=', true)->orderBy('group', 'DESC')->orderBy('name', 'ASC')->get();
46
    }
47
48
    /**
49
     * Search tags by name.
50
     *
51
     * @param string $term
52
     *
53
     * @return Eloquent\Collection|static[]
54
     */
55
    public function searchTags($term)
56
    {
57
        return $this->with('parent')->where('name', 'like', '%' . $term . '%')->where('parent_id', '<>', 0)->get();
58
    }
59
60
    /**
61
     * Returns tag groups list.
62
     *
63
     * @return array
64
     */
65
    public function groupsDropdown()
66
    {
67 29
        return $this->getGroups()->map(function ($group) {
68 29
            $group->keyname = 'tag:' . $group->id;
69 29
            $group->name = ucwords($group->name);
70
71 29
            return $group;
72 29
        })->lists('name', 'keyname')->all();
73
    }
74
75
    /**
76
     * Returns collection of all groups.
77
     *
78
     * @return Eloquent\Collection
79
     */
80 31
    public function getGroups()
81
    {
82 31
        return $this->where('group', '=', true)->orderBy('name', 'ASC')->get();
83
    }
84
85
    /**
86
     * Return tag by name.
87
     *
88
     * @param string $name
89
     *
90
     * @return static
91
     */
92 7
    public function getTagByName($name)
93
    {
94 7
        return static::where('name', '=', $name)->first();
95
    }
96
97
    /**
98
     * Returns collection of tags in status group.
99
     *
100
     * @return Eloquent\Collection
101
     */
102 6
    public function getStatusTags()
103
    {
104 6
        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...
105
    }
106
107
    /**
108
     * Returns collection of tags in type group.
109
     *
110
     * @return Eloquent\Collection
111
     */
112 2
    public function getTypeTags()
113
    {
114 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...
115
    }
116
117
    /**
118
     * Returns collection of tags in type group.
119
     *
120
     * @return Eloquent\Collection
121
     */
122
    public function getResolutionTags()
123
    {
124
        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...
125
    }
126
}
127