Completed
Push — master ( b1e8a8...b817fe )
by Renato
05:04 queued 02:33
created

User::organizationActive()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
/**
3
 * GitScrum v0.1.
4
 *
5
 * @author  Renato Marinho <renato.marinho>
6
 * @license http://opensource.org/licenses/GPL-3.0 GPLv3
7
 */
8
9
namespace GitScrum\Models;
10
11
use Illuminate\Database\Eloquent\Model;
12
use Illuminate\Foundation\Auth\User as Authenticatable;
13
14
class User extends Authenticatable
15
{
16
    /**
17
     * The database table used by the model.
18
     *
19
     * @var string
20
     */
21
    protected $table = 'users';
22
23
    /**
24
     * Attributes that should be mass-assignable.
25
     *
26
     * @var array
27
     */
28
    protected $fillable = ['github_id', 'username', 'name', 'avatar', 'html_url', 'email',
29
        'bio', 'location', 'blog', 'since', 'token', 'main_repository', 'position_held', ];
30
31
    /**
32
     * The attributes excluded from the model's JSON form.
33
     *
34
     * @var array
35
     */
36
    protected $hidden = ['password', 'remember_token'];
37
38
    /**
39
     * The attributes that should be casted to native types.
40
     *
41
     * @var array
42
     */
43
    protected $casts = [];
44
45
    public function setNameAttribute($value)
46
    {
47
        $this->attributes['name'] = $value;
48
49
        if (empty($value)) {
50
            $this->attributes['name'] = $this->attributes['username'];
51
        }
52
    }
53
54
    public function configStatuses()
55
    {
56
        return $this->belongsToMany(\GitScrum\Models\ConfigStatus::class, 'statuses', 'user_id', 'id');
57
    }
58
59
    public function issues()
60
    {
61
        return $this->belongsToMany(\GitScrum\Models\Issue::class, 'issues_has_users', 'user_id', 'issue_id');
62
    }
63
64
    public function organizations()
65
    {
66
        return $this->belongsToMany(\GitScrum\Models\Organization::class, 'users_has_organizations')
67
            ->withTimestamps();
68
    }
69
70
    public function organizationActive()
71
    {
72
        return $this->belongsToMany(\GitScrum\Models\Organization::class, 'users_has_organizations', 'user_id', 'organization_id')
73
            ->withTimestamps();
74
    }
75
76
    public function attachments()
77
    {
78
        return $this->hasMany(\GitScrum\Models\Attachment::class, 'user_id', 'id');
79
    }
80
81
    public function branches()
82
    {
83
        return $this->hasMany(\GitScrum\Models\Branch::class, 'user_id', 'id');
84
    }
85
86
    public function comments()
87
    {
88
        return $this->hasMany(\GitScrum\Models\Comment::class, 'user_id', 'id');
89
    }
90
91
    public function commits()
92
    {
93
        return $this->hasMany(\GitScrum\Models\Commit::class, 'user_id', 'id');
94
    }
95
96
    public function statuses()
97
    {
98
        return $this->hasMany(\GitScrum\Models\Status::class, 'user_id', 'id');
99
    }
100
101
    public function notes()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
102
    {
103
        return $this->morphMany(\GitScrum\Models\Note::class, 'noteable')
104
            ->orderby('position', 'ASC');
105
    }
106
107
    public function labels($feature)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
108
    {
109
        return $this->{$feature}->map(function ($obj) {
110
            return $obj->labels;
111
        })->flatten(1)->unique('id');
112
    }
113
114
    public function productBacklogs($product_backlog_id = null)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
115
    {
116
        return $this->organizations->map(function ($organization) use ($product_backlog_id) {
0 ignored issues
show
Documentation introduced by
The property organizations does not exist on object<GitScrum\Models\User>. 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...
117
            $obj = $organization->productBacklog()
118
                ->with('sprints')
119
                ->with('favorite')
120
                ->with('organization')
121
                ->with('issues')->get();
122
123
            if (!is_null($product_backlog_id)) {
124
                $obj = $obj->where('id', '=', $product_backlog_id);
125
            }
126
127
            return $obj;
128
        })->flatten(1);
129
    }
130
131
    public function sprints($sprint_id = null)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
132
    {
133
        return $this->issues->map(function ($issue) use ($sprint_id) {
0 ignored issues
show
Documentation introduced by
The property issues does not exist on object<GitScrum\Models\User>. 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...
134
            $obj = $issue->sprint()->get();
135
136
            if (!is_null($sprint_id)) {
137
                $obj = $obj->where('id', '=', $sprint_id);
138
            }
139
140
            return $obj;
141
        })->flatten(1)->unique('id');
142
    }
143
144
    public function team()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
145
    {
146
        return $this->organizations->map(function ($obj) {
0 ignored issues
show
Documentation introduced by
The property organizations does not exist on object<GitScrum\Models\User>. 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...
147
            return $obj->users;
148
        })->flatten(1)->unique('id');
149
    }
150
151
    public function activities($user_id = null)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
152
    {
153
        return $this->team()->map(function ($obj) use ($user_id) {
154
            $statuses = $obj->statuses;
155
            if(!is_null($user_id)){
156
                $statuses = $statuses->where('user_id', $user_id);
157
            }
158
            return $statuses;
159
        })->flatten(1)->sortByDesc('id')->take(6);
160
    }
161
}
162