Completed
Push — develop-3.0 ( 5ab583...f20237 )
by Mohamed
06:33
created

Fetcher::getActiveUsers()   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 0
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\Model\Project;
17
use Tinyissue\Model\User;
18
use Tinyissue\Repository\Repository;
19
20
class Fetcher extends Repository
21
{
22
    /**
23
     * @var User
24
     */
25
    protected $model;
26
27
    public function __construct(User $model)
28
    {
29
        $this->model = $model;
30
    }
31
32
    /**
33
     * Returns projects the user member of.
34
     *
35
     * @return Eloquent\Collection
36
     */
37
    public function getProjects()
38
    {
39
        return $this->model->projects()->active()->get();
40
    }
41
42
    /**
43
     * Get collection of projects with user details.
44
     *
45
     * @return Eloquent\Collection
46
     */
47
    public function getProjectsWithSettings()
48
    {
49
        return $this->model->projects()->active()->with('projectUsers')->get();
50
    }
51
52
    /**
53
     * Returns public users.
54
     *
55
     * @return Eloquent\Collection
56
     */
57
    public function getActiveUsers()
58
    {
59
        return $this->model->with('role')->notPrivate()->orderBy('firstname')->get();
60
    }
61
62
    /**
63
     * Returns user projects with activities details eager loaded.
64
     *
65
     * @return Eloquent\Collection
66
     */
67
    public function getProjectsWithRecentActivities()
68
    {
69
        return $this->model->projects()->active()->with('recentActivities')->get();
70
    }
71
72
    /**
73
     * Returns projects with issues details eager loaded.
74
     *
75
     * @return Eloquent\Collection
76
     */
77
    public function getProjectsWithRecentIssues()
78
    {
79
        return $this->model
80
            ->projects()
81
            ->active()
82
            ->with('issues.user', 'issues.countComments')
83
            ->with([
84
                'issues' => function (Relations\Relation $query) {
85
                    $query->open()->with('updatedBy');
86
                    $query->assignedOrCreated($this->model);
87
                },
88
            ])
89
            ->orderBy('name')
90
            ->get();
91
    }
92
93
    /**
94
     * Get collection of issues group by a list of tags.
95
     *
96
     * @param Eloquent\Collection $tagIds
97
     * @param null|int            $projectId
98
     *
99
     * @return Eloquent\Collection
100
     */
101
    public function getIssuesGroupByTags(Eloquent\Collection $tagIds, $projectId = null)
102
    {
103
        $tagIds = $tagIds->pluck('id')->all();
104
105
        $assignedOrCreate = $this->model->isUser() ? 'issuesCreatedBy' : 'issues';
106
        $issues           = $this->model->$assignedOrCreate()
107
            ->with('user', 'tags')
108
            ->open()
109
            ->forProject($projectId)
110
            ->join('projects_issues_tags', 'issue_id', '=', 'id')
111
            ->whereIn('projects_issues_tags.tag_id', $tagIds)
112
            ->orderBy('id')
113
            ->get()
114
            ->groupBy(function (Project\Issue $issue) {
115
                return $issue->getStatusTag()->name;
116
            });
117
118
        return $issues;
119
    }
120
121
    /**
122
     * Returns all projects with open issue count.
123
     *
124
     * @param int $status
125
     *
126
     * @return Eloquent\Collection
127
     */
128
    public function getProjectsWithOpenIssuesCount($status = Project::STATUS_OPEN)
129
    {
130
        if ($this->model->isManager() || $this->model->isAdmin()) {
131
            return app()->make(Project::class)->getProjectsWithOpenIssuesCount($status, Project::PRIVATE_ALL);
132
        }
133
134
        return $this->model->projects()->status($status)->with('openIssuesCount')->get();
135
    }
136
}
137