QueryTrait::projectsWidthIssues()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 18
ccs 9
cts 9
cp 1
rs 9.4285
cc 3
eloc 12
nc 2
nop 1
crap 3
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
18
/**
19
 * QueryTrait is trait class containing the database queries methods for the User model.
20
 *
21
 * @author Mohamed Alsharaf <[email protected]>
22
 *
23
 * @property static $this
24
 */
25
trait QueryTrait
26
{
27
    /**
28
     * Returns public users.
29
     *
30
     * @return Eloquent\Collection
31
     */
32 5
    public function activeUsers()
33
    {
34 5
        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...
35 5
            ->where('private', '=', false)
36 5
            ->orderBy('firstname', 'ASC')->get();
37
    }
38
39
    /**
40
     * Returns user projects with activities details eager loaded.
41
     *
42
     * @param int $status
43
     *
44
     * @return Relations\HasMany
45
     */
46 10
    public function projectsWidthActivities($status = Project::STATUS_OPEN)
47
    {
48 10
        return $this->projects($status)
0 ignored issues
show
Bug introduced by
The method projects() does not exist on Tinyissue\Model\Traits\User\QueryTrait. Did you maybe mean projectsWidthActivities()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
49 10
            ->with([
50
                'activities' => function (Relations\Relation $query) {
51 2
                    $query->with('activity', 'issue', 'user', 'assignTo', 'comment', 'note');
52 2
                    $query->orderBy('users_activity.created_at', 'DESC');
53
54
                    // For logged users with role User, show issues that are created by them in internal projects
55
                    // of issue create by any for other project statuses
56 2
                    if ($this->getLoggedUser()->isUser()) {
57 2
                        $query->join('projects_issues', 'projects_issues.id', '=', 'item_id');
58 2
                        $query->join('projects', 'projects.id', '=', 'parent_id');
59
                        $query->where(function (Eloquent\Builder $query) {
60
                            $query->where(function (Eloquent\Builder $query) {
61 2
                                $query->where('created_by', '=', $this->getLoggedUser()->id);
62 2
                                $query->where('private', '=', Project::INTERNAL_YES);
63 2
                            });
64 2
                            $query->orWhere('private', '<>', Project::INTERNAL_YES);
65 2
                        });
66
                    }
67 10
                },
68
            ]);
69
    }
70
71
    /**
72
     * Returns projects with issues details eager loaded.
73
     *
74
     * @param int $status
75
     *
76
     * @return Relations\HasMany
77
     */
78 1
    public function projectsWidthIssues($status = Project::STATUS_OPEN)
79
    {
80 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...
81
82
        return $this
0 ignored issues
show
Bug introduced by
The method projects() does not exist on Tinyissue\Model\Traits\User\QueryTrait. Did you maybe mean projectsWidthActivities()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
83 1
            ->projects($status)
84 1
            ->with([
85
                '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...
86 1
                    $query->with('updatedBy');
87 1
                    $query->where($assignedOrCreate, '=', $this->id);
0 ignored issues
show
Bug introduced by
The property id does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
88 1
                    if ($status === Project::STATUS_OPEN) {
89 1
                        $query->where('status', '=', Project\Issue::STATUS_OPEN);
90
                    }
91 1
                },
92
                'issues.user'          => function () {},
93
                'issues.countComments' => function () {},
94
            ]);
95
    }
96
97
    /**
98
     * Returns collection of issues grouped by tags.
99
     *
100
     * @param $tagIds
101
     * @param int $projectId
102
     *
103
     * @return mixed
104
     */
105 1
    public function issuesGroupByTags($tagIds, $projectId = null)
106
    {
107 1
        $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...
108 1
        $issues           = $this->$assignedOrCreate()
109 1
            ->with('user', 'tags')
110 1
            ->where('status', '=', Project\Issue::STATUS_OPEN)
111 1
            ->whereIn('projects_issues_tags.tag_id', $tagIds)
112 1
            ->join('projects_issues_tags', 'issue_id', '=', 'id')
113 1
            ->orderBy('id');
114
115
        // Limit by project id
116 1
        if ($projectId > 0) {
117 1
            $issues->where('project_id', '=', $projectId);
118
        }
119
120 1
        $issues = $issues->get()->groupBy(function (Project\Issue $issue) {
121 1
            return $issue->getStatusTag()->name;
122 1
        });
123
124 1
        return $issues;
125
    }
126
127
    /**
128
     * Load user permissions.
129
     *
130
     * @return Eloquent\Collection
131
     */
132 65
    protected function loadPermissions()
133
    {
134 65
        if (null === $this->permission) {
135 65
            $this->permission = $this->permissions()->with('permission')->get();
0 ignored issues
show
Bug introduced by
The property permission does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
Bug introduced by
The method permissions() does not exist on Tinyissue\Model\Traits\User\QueryTrait. Did you maybe mean loadPermissions()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
136
        }
137
138 65
        return $this->permission;
139
    }
140
141
    abstract public function getLoggedUser();
142
}
143