Completed
Push — develop ( 688bc1...4964b4 )
by Mohamed
07:24
created

QueryTrait::loadPermissions()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 9.4285
cc 2
eloc 4
nc 2
nop 0
crap 2
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\User;
13
14
use Illuminate\Database\Eloquent;
15
use Illuminate\Database\Eloquent\Relations;
16
use Tinyissue\Model\Project;
17
use Tinyissue\Model\Role;
18
19
/**
20
 * QueryTrait is trait class containing the database queries methods for the User model.
21
 *
22
 * @author Mohamed Alsharaf <[email protected]>
23
 *
24
 * @property int                 $id
25
 * @property Eloquent\Collection $permission
26
 *
27
 * @method   Relations\HasMany projects($status = Project::STATUS_OPEN)
28
 * @method   Relations\HasMany permissions()
29
 */
30
trait QueryTrait
31
{
32
    /**
33
     * Returns public users.
34
     *
35
     * @return Eloquent\Collection
36
     */
37 4
    public function activeUsers()
38
    {
39 4
        return $this->with('role')
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...
40 4
            ->where('private', '=', false)
41 4
            ->orderBy('firstname', 'ASC')->get();
42
    }
43
44
    /**
45
     * Returns user projects with activities details eager loaded.
46
     *
47
     * @param int $status
48
     *
49
     * @return Relations\HasMany
50
     */
51 10
    public function projectsWidthActivities($status = Project::STATUS_OPEN)
52
    {
53 10
        return $this->projects($status)
54 10
            ->with([
55
                'activities' => function (Relations\Relation $query) {
56 2
                    $query->with('activity', 'issue', 'user', 'assignTo', 'comment', 'note');
57 2
                    $query->orderBy('created_at', 'DESC');
58 10
                },
59
            ]);
60
    }
61
62
    /**
63
     * Returns projects with issues details eager loaded.
64
     *
65
     * @param int $status
66
     *
67
     * @return Relations\HasMany
68
     */
69 1
    public function projectsWidthIssues($status = Project::STATUS_OPEN)
70
    {
71 1
        $assignedOrCreate = $this->isUser()? 'created_by' : 'assigned_to';
0 ignored issues
show
Bug introduced by
It seems like isUser() 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...
72
73
        return $this
74 1
            ->projects($status)
75 1
            ->with([
76 View Code Duplication
                'issues' => function (Relations\Relation $query) use ($status, $assignedOrCreate) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
77 1
                    $query->with('updatedBy');
78 1
                    $query->where($assignedOrCreate, '=', $this->id);
79 1
                    if ($status === Project::STATUS_OPEN) {
80 1
                        $query->where('status', '=', Project\Issue::STATUS_OPEN);
81
                    }
82 1
                },
83
                'issues.user'          => function () {},
84
                'issues.countComments' => function () {},
85
            ]);
86
    }
87
88
    /**
89
     * Returns collection of issues grouped by tags.
90
     *
91
     * @param $tagIds
92
     *
93
     * @return mixed
94
     */
95
    public function issuesGroupByTags($tagIds)
96
    {
97
        $assignedOrCreate = $this->isUser()? 'issuesCreatedBy' : 'issues';
0 ignored issues
show
Bug introduced by
It seems like isUser() 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...
98
        $issues = $this->$assignedOrCreate()
99
            ->with('user', 'tags')
100
            ->where('status', '=', Project\Issue::STATUS_OPEN)
101
            ->whereIn('projects_issues_tags.tag_id', $tagIds)
102
            ->join('projects_issues_tags', 'issue_id', '=', 'id')
103
            ->orderBy('id')
104
            ->get()
105
            ->groupBy(function (Project\Issue $issue) {
106
                return $issue->getStatusTag()->name;
107
            });
108
109
        return $issues;
110
    }
111
112
    /**
113
     * Load user permissions.
114
     *
115
     * @return Eloquent\Collection
116
     */
117 60
    protected function loadPermissions()
118
    {
119 60
        if (null === $this->permission) {
120 60
            $this->permission = $this->permissions()->with('permission')->get();
121
        }
122
123 60
        return $this->permission;
124
    }
125
}
126