Completed
Branch develop-3.0 (4fe777)
by Mohamed
11:06
created

UserRepository   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 118
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 4
dl 0
loc 118
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getProjects() 0 4 1
A getProjectsWithSettings() 0 4 1
A getActiveUsers() 0 4 1
A getProjectsWithRecentActivities() 0 4 1
A getProjectsWithRecentIssues() 0 14 2
A getIssuesGroupByTags() 0 20 2
A getProjectsWithOpenIssuesCount() 0 8 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\Repository\User;
13
14
use Illuminate\Database\Eloquent;
15
use Illuminate\Database\Eloquent\Relations;
16
use Tinyissue\Contracts\Model\UserInterface;
17
use Tinyissue\Contracts\Repository\Project\ProjectRepository;
18
use Tinyissue\Contracts\Repository\User\UserRepository as UserContract;
19
use Tinyissue\Model\Permission;
20
use Tinyissue\Model\Project;
21
use Tinyissue\Model\User;
22
use Tinyissue\Repository\Repository;
23
24
class UserRepository extends Repository implements UserContract
25
{
26
    protected $updaterClass = UpdaterRepository::class;
27
    protected $counterClass = CounterRepository::class;
28
29
    /**
30
     * @var User
31
     */
32
    protected $model;
33
34
    public function __construct(UserInterface $model)
35
    {
36
        $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...
37
    }
38
39
//    /**
40
//     * @return UpdaterRepository
41
//     */
42
//    public function updater()
43
//    {
44
//        return parent::updater(); // TODO: Change the autogenerated stub
45
//    }
46
47
    /**
48
     * Returns projects the user member of.
49
     *
50
     * @return Eloquent\Collection
51
     */
52
    public function getProjects()
53
    {
54
        return $this->model->projects()->get();
55
    }
56
57
    public function getProjectsWithSettings()
58
    {
59
        return $this->model->projects()->with('projectUsers')->get();
60
    }
61
62
    /**
63
     * Returns public users.
64
     *
65
     * @return Eloquent\Collection
66
     */
67
    public function getActiveUsers()
68
    {
69
        return $this->model->with('role')->private()->orderBy('firstname')->get();
70
    }
71
72
    /**
73
     * Returns user projects with activities details eager loaded.
74
     *
75
     * @param int $status
76
     * @done
77
     *
78
     * @return Relations\HasMany
79
     */
80
    public function getProjectsWithRecentActivities($status = Project::STATUS_OPEN)
81
    {
82
        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...
83
    }
84
85
    /**
86
     * Returns projects with issues details eager loaded.
87
     *
88
     * @return Relations\HasMany
89
     */
90
    public function getProjectsWithRecentIssues()
91
    {
92
        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...
93
            ->active()
94
            ->with('issues.user', 'issues.countComments')
95
            ->with([
96
                'openIssuesWithUpdater' => function (Relations\Relation $query) {
97
                    $assignedOrCreate = $this->model->isUser() ? 'createdBy' : 'assignedTo';
98
                    $query->$assignedOrCreate($this->id);
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<Tinyissue\Repository\User\UserRepository>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
99
                },
100
            ])
101
            ->orderBy('name')
102
            ->get();
103
    }
104
105
    public function getIssuesGroupByTags(Eloquent\Collection $tagIds, $projectId = null)
106
    {
107
        $tagIds = $tagIds->pluck('id')->all();
108
109
        $assignedOrCreate = $this->model->isUser() ? 'issuesCreatedBy' : 'issues';
110
        $issues           = $this->$assignedOrCreate()
111
            ->with('user', 'tags')
112
            ->open()
113
            ->forProject($projectId)
114
            ->join('projects_issues_tags', 'issue_id', '=', 'id')
115
            ->whereIn('projects_issues_tags.tag_id', $tagIds)
116
            ->orderBy('id')
117
            ->get()
118
            ->groupBy(function (Project\Issue $issue) {
119
                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...
120
            })
121
            ->get();
122
123
        return $issues;
124
    }
125
126
    /**
127
     * Returns all projects with open issue count.
128
     *
129
     * @param int $status
130
     *
131
     * @return Builder|Relations\BelongsToMany
132
     */
133
    public function getProjectsWithOpenIssuesCount($status = Project::STATUS_OPEN)
134
    {
135
        if ($this->model->permission(Permission::PERM_PROJECT_ALL)) {
136
            return app()->make(ProjectRepository::class)->getProjectsWithOpenIssuesCount($status, Project::PRIVATE_ALL);
137
        }
138
139
        return $this->model->projects()->status($status)->with('countOpenIssues')->get();
140
    }
141
}
142