Completed
Push — develop-3.0 ( 4fe777...24fc5d )
by Mohamed
09:15
created

Fetcher::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 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\Repository\User;
13
14
use Illuminate\Database\Eloquent;
15
use Illuminate\Database\Eloquent\Relations;
16
use Tinyissue\Contracts\Model\UserInterface;
17
use Tinyissue\Model\Permission;
18
use Tinyissue\Model\Project;
19
use Tinyissue\Model\User;
20
use Tinyissue\Repository\Repository;
21
22
class Fetcher extends Repository
23
{
24
    /**
25
     * @var User
26
     */
27
    protected $model;
28
29
    public function __construct(UserInterface $model)
30
    {
31
        $this->model = $model;
0 ignored issues
show
Documentation Bug introduced by
$model is of type object<Tinyissue\Contracts\Model\UserInterface>, but the property $model was declared to be of type object<Tinyissue\Model\User>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
32
    }
33
34
    /**
35
     * Returns projects the user member of.
36
     *
37
     * @return Eloquent\Collection
38
     */
39
    public function getProjects()
40
    {
41
        return $this->model->projects()->get();
42
    }
43
44
    public function getProjectsWithSettings()
45
    {
46
        return $this->model->projects()->with('projectUsers')->get();
47
    }
48
49
    /**
50
     * Returns public users.
51
     *
52
     * @return Eloquent\Collection
53
     */
54
    public function getActiveUsers()
55
    {
56
        return $this->model->with('role')->private()->orderBy('firstname')->get();
57
    }
58
59
    /**
60
     * Returns user projects with activities details eager loaded.
61
     *
62
     * @param int $status
63
     * @done
64
     *
65
     * @return Relations\HasMany
66
     */
67
    public function getProjectsWithRecentActivities($status = Project::STATUS_OPEN)
68
    {
69
        return $this->model->projects($status)->with('recentActivities')->get();
0 ignored issues
show
Unused Code introduced by
The call to User::projects() has too many arguments starting with $status.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
70
    }
71
72
    /**
73
     * Returns projects with issues details eager loaded.
74
     *
75
     * @return Relations\HasMany
76
     */
77
    public function getProjectsWithRecentIssues()
78
    {
79
        return $this->model
0 ignored issues
show
Bug introduced by
The method active() does not exist on Tinyissue\Model\User. Did you maybe mean isActive()?

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...
80
            ->active()
81
            ->with('issues.user', 'issues.countComments')
82
            ->with([
83
                'openIssuesWithUpdater' => function (Relations\Relation $query) {
84
                    $assignedOrCreate = $this->model->isUser() ? 'createdBy' : 'assignedTo';
85
                    $query->$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...
86
                },
87
            ])
88
            ->orderBy('name')
89
            ->get();
90
    }
91
92
    public function getIssuesGroupByTags(Eloquent\Collection $tagIds, $projectId = null)
93
    {
94
        $tagIds = $tagIds->pluck('id')->all();
95
96
        $assignedOrCreate = $this->model->isUser() ? 'issuesCreatedBy' : 'issues';
97
        $issues           = $this->model->$assignedOrCreate()
98
            ->with('user', 'tags')
99
            ->open()
100
            ->forProject($projectId)
101
            ->join('projects_issues_tags', 'issue_id', '=', 'id')
102
            ->whereIn('projects_issues_tags.tag_id', $tagIds)
103
            ->orderBy('id')
104
            ->get()
105
            ->groupBy(function (Project\Issue $issue) {
106
                return $issue->getStatusTag()->name;
0 ignored issues
show
Documentation Bug introduced by
The method getStatusTag does not exist on object<Tinyissue\Model\Project\Issue>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
107
            })
108
//            ->get()
109
        ;
110
111
        return $issues;
112
    }
113
114
    /**
115
     * Returns all projects with open issue count.
116
     *
117
     * @param int $status
118
     *
119
     * @return Builder|Relations\BelongsToMany
120
     */
121
    public function getProjectsWithOpenIssuesCount($status = Project::STATUS_OPEN)
122
    {
123 View Code Duplication
        if ($this->model->isManager() || $this->model->isAdmin()) {
1 ignored issue
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...
124
            return app()->make(Project::class)->getProjectsWithOpenIssuesCount($status, Project::PRIVATE_ALL);
125
        }
126
127
        return $this->model->projects()->status($status)->with('openIssuesCount')->get();
128
    }
129
}
130