Completed
Push — master ( 3a53f1...4212b8 )
by Renato
08:23
created

User::comments()   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
 * GitScrum v0.1.
4
 *
5
 * @author  Renato Marinho <renato.marinho>
6
 * @license http://opensource.org/licenses/GPL-3.0 GPLv3
7
 */
8
namespace GitScrum\Models;
9
10
use Illuminate\Database\Eloquent\Model;
11
use Illuminate\Foundation\Auth\User as Authenticatable;
12
use GitScrum\Classes\Helper;
13
use Carbon\Carbon;
14
15
class User extends Authenticatable
16
{
17
    /**
18
     * The database table used by the model.
19
     *
20
     * @var string
21
     */
22
    protected $table = 'users';
23
24
    /**
25
     * Attributes that should be mass-assignable.
26
     *
27
     * @var array
28
     */
29
    protected $fillable = ['github_id', 'username', 'name', 'avatar', 'html_url', 'email',
30
        'bio', 'location', 'blog', 'since', 'token', 'main_repository', 'position_held', ];
31
32
    /**
33
     * The attributes excluded from the model's JSON form.
34
     *
35
     * @var array
36
     */
37
    protected $hidden = ['password', 'remember_token'];
38
39
    /**
40
     * The attributes that should be casted to native types.
41
     *
42
     * @var array
43
     */
44
    protected $casts = [];
45
46
    public function setNameAttribute($value)
47
    {
48
        $this->attributes['name'] = $value;
49
50
        if (empty($value)) {
51
            $this->attributes['name'] = $this->attributes['username'];
52
        }
53
    }
54
55
    public function configStatuses()
56
    {
57
        return $this->belongsToMany(\GitScrum\Models\ConfigStatus::class, 'statuses', 'user_id', 'id');
58
    }
59
60
    public function issues()
61
    {
62
        return $this->belongsToMany(\GitScrum\Models\Issue::class, 'issues_has_users', 'user_id', 'issue_id');
63
    }
64
65
    public function organizations()
66
    {
67
        return $this->belongsToMany(\GitScrum\Models\Organization::class, 'users_has_organizations')
68
            ->withTimestamps();
69
    }
70
71
    public function organizationActive()
72
    {
73
        return $this->belongsToMany(\GitScrum\Models\Organization::class, 'users_has_organizations', 'user_id', 'organization_id')
74
            ->withTimestamps();
75
    }
76
77
    public function attachments()
78
    {
79
        return $this->hasMany(\GitScrum\Models\Attachment::class, 'user_id', 'id');
80
    }
81
82
    public function branches()
83
    {
84
        return $this->hasMany(\GitScrum\Models\Branch::class, 'user_id', 'id');
85
    }
86
87
    public function comments()
88
    {
89
        return $this->hasMany(\GitScrum\Models\Comment::class, 'user_id', 'id');
90
    }
91
92
    public function commits()
93
    {
94
        return $this->hasMany(\GitScrum\Models\Commit::class, 'user_id', 'id');
95
    }
96
97
    public function statuses()
98
    {
99
        return $this->hasMany(\GitScrum\Models\Status::class, 'user_id', 'id');
100
    }
101
102
    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...
103
    {
104
        return $this->{$feature}->map(function ($obj) {
105
            return $obj->labels;
106
        })->flatten(1)->unique('id');
107
    }
108
109
    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...
110
    {
111
        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...
112
            $obj = $organization->productBacklog()
113
                ->with('sprints')
114
                ->with('favorite')
115
                ->with('organization')
116
                ->with('issues')->get();
117
118
            if (!is_null($product_backlog_id)) {
119
                $obj = $obj->where('id', '=', $product_backlog_id);
120
            }
121
122
            return $obj;
123
        })->flatten(1);
124
    }
125
126
    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...
127
    {
128
        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...
129
            $obj = $issue->sprint()
130
                ->get();
131
132
            if (!is_null($sprint_id)) {
133
                $obj = $obj->where('id', '=', $sprint_id);
134
            }
135
136
            return $obj;
137
        })->flatten(1);
138
139
    }
140
141
    public function burdown()
142
    {
143
        $helper = new Helper();
144
        $dt_finish = Carbon::now();
145
146
        $date_finish = $dt_finish->toDateString();
147
        $date_start = $dt_finish->subMonths(1)->toDateString();
148
149
        $issues = $this->issues;
0 ignored issues
show
Documentation introduced by
The property issues does not exist on object<GitScrum\Models\User>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write 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.");
        }
    }

}

Since the property has write access only, you can use the @property-write 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...
150
151
        $total = $issues->count();
152
153
        $dates = $helper->arrayDateRange([$date_start, $date_finish], $total);
154
155
        $previous = $date_start;
156
        $arr = [];
157
        $arr[$previous] = $total;
158
159
        foreach ($dates as $date => $value) {
160
            $closed = $issues()->whereDate('closed_at', '=', $date)->count();
161
            $totalPrevious = $total - $arr[$previous];
162
            $arr[$date] = $total - ($closed + $totalPrevious);
163
            $previous = $date;
164
        }
165
166
        return $arr;
167
    }
168
169
    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...
170
    {
171
        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...
172
            return $obj->users;
173
        })->flatten(1)->unique('id');
174
    }
175
176
    public function activities()
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...
177
    {
178
        return $this->team()->map(function ($obj) {
179
            return $obj->statuses;
180
        })->flatten(1)->sortByDesc('id')->take(6);
181
    }
182
}
183