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

Sprint::workingDays()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 22
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 8.6737
c 0
b 0
f 0
cc 5
eloc 16
nc 4
nop 1
1
<?php
2
/**
3
 * GitScrum v0.1.
4
 *
5
 * @author  Renato Marinho <[email protected]>
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\Database\Eloquent\SoftDeletes;
13
use Carbon\Carbon;
14
15
class Sprint extends Model
16
{
17
    use SoftDeletes;
18
19
    /**
20
     * The database table used by the model.
21
     *
22
     * @var string
23
     */
24
    protected $table = 'sprints';
25
26
    /**
27
     * Attributes that should be mass-assignable.
28
     *
29
     * @var array
30
     */
31
    protected $fillable = ['user_id', 'product_backlog_id', 'slug', 'title', 'description', 'version',
32
        'is_private', 'date_start', 'date_finish', 'state', 'color', 'position', 'closed_at', ];
33
34
    /**
35
     * The attributes excluded from the model's JSON form.
36
     *
37
     * @var array
38
     */
39
    protected $hidden = [];
40
41
    /**
42
     * The attributes that should be casted to native types.
43
     *
44
     * @var array
45
     */
46
    protected $casts = [];
47
48
    protected static function boot()
49
    {
50
        parent::boot();
51
    }
52
53
    public function productBacklog()
54
    {
55
        return $this->belongsTo(\GitScrum\Models\ProductBacklog::class, 'product_backlog_id', 'id');
56
    }
57
58
    public function branches()
59
    {
60
        return $this->hasMany(\GitScrum\Models\Branch::class, 'sprint_id', 'id');
61
    }
62
63
    public function issues()
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...
64
    {
65
        return $this->hasMany(\GitScrum\Models\Issue::class, 'sprint_id', 'id')
66
            ->orderby('position', 'ASC');
67
    }
68
69
    public function issuesHasUsers()
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...
70
    {
71
        $users = $this->issues->map(function ($issue) {
0 ignored issues
show
Documentation introduced by
The property issues does not exist on object<GitScrum\Models\Sprint>. 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...
72
            return $issue->users;
73
        })->reject(function ($value) {
74
            return $value == null;
75
        })->flatten(1)->unique('id')->splice(0, 3);
76
77
        return $users->all();
78
    }
79
80
    public function comments()
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...
81
    {
82
        return $this->morphMany(\GitScrum\Models\Comment::class, 'commentable')
83
            ->orderby('created_at', 'DESC');
84
    }
85
86
    public function attachments()
87
    {
88
        return $this->morphMany(\GitScrum\Models\Attachment::class, 'attachmentable');
89
    }
90
91
    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...
92
    {
93
        return $this->morphMany(\GitScrum\Models\Note::class, 'noteable')
94
            ->orderby('position', 'ASC');
95
    }
96
97
    public function favorite()
98
    {
99
        return $this->morphOne(\GitScrum\Models\Favorite::class, 'favoriteable');
100
    }
101
102
    public function status()
103
    {
104
        return $this->hasOne(\GitScrum\Models\ConfigStatus::class, 'id', 'config_status_id');
105
    }
106
107
    public function pullrequests()
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
        $prs = $this->branches->map(function ($branch) {
0 ignored issues
show
Documentation introduced by
The property branches does not exist on object<GitScrum\Models\Sprint>. 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...
110
            if ($branch->pullrequests->count()) {
111
                return $branch->pullrequests;
112
            }
113
        })->reject(function ($value) {
114
            return $value == null;
115
        });
116
117
        return $prs->all();
118
    }
119
120
    public function totalAdditions()
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...
121
    {
122
        $additions = $this->branches->map(function ($branch) {
0 ignored issues
show
Documentation introduced by
The property branches does not exist on object<GitScrum\Models\Sprint>. 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...
123
            return $branch->commits;
124
        })->flatten(1)->map(function ($commit) {
125
            return $commit->files;
126
        })->flatten(1)->sum('additions');
127
128
        return $additions;
129
    }
130
131
    public function totalPullRequests()
132
    {
133
        $prs = $this->branches->map(function ($branch) {
0 ignored issues
show
Documentation introduced by
The property branches does not exist on object<GitScrum\Models\Sprint>. 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
            return $branch->pullrequests()->count();
135
        });
136
137
        return array_sum($prs->all());
138
    }
139
140
    public function workingDays($start = null)
141
    {
142
        $begin = strtotime(is_null($start) ? $this->attributes['date_start'] : $start);
143
        $end = strtotime($this->attributes['date_finish']);
144
        if ($begin > $end) {
145
            return 0;
146
        } else {
147
            $no_days = 0;
148
            $weekends = 0;
149
            while ($begin <= $end) {
150
                ++$no_days;
151
                $what_day = date('N', $begin);
152
                if ($what_day > 5) {
153
                    ++$weekends;
154
                }
155
                $begin += 86400;
156
            }
157
            $working_days = $no_days - $weekends;
158
159
            return $working_days;
160
        }
161
    }
162
163
    public function weeks($start = null)
164
    {
165
        $begin = Carbon::parse(is_null($start) ? $this->attributes['date_start'] : $start);
166
        $end = Carbon::parse($this->attributes['date_finish']);
167
168
        return round($begin->diffInDays($end) / 7);
169
    }
170
171
    public function getPSR2Errors()
172
    {
173
        $errors = 0;
174
        /*
175
        foreach ($this->branches as $branch) {
176
            foreach ($branch->commits as $commit) {
177
                foreach ($commit->files as $file) {
178
                    $errors += $file->filePhpcs->count();
179
                }
180
            }
181
        }
182
        */
183
        return $errors;
184
    }
185
186
    public function getEffort()
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...
187
    {
188
        $effort = $this->issues->map(function ($issue) {
0 ignored issues
show
Documentation introduced by
The property issues does not exist on object<GitScrum\Models\Sprint>. 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...
189
            return $issue->configEffort;
190
        })->sum('effort');
191
192
        return $effort;
193
    }
194
195
    public function getEffortAvg()
196
    {
197
        $effort = $this->issues->map(function ($issue) {
0 ignored issues
show
Documentation introduced by
The property issues does not exist on object<GitScrum\Models\Sprint>. 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...
198
            return $issue->configEffort;
199
        })->avg('effort');
200
201
        return round($effort, 2);
202
    }
203
204
    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...
205
    {
206
        $activities = $this->issues()
207
            ->with('statuses')->get()->map(function ($issue) {
208
                return $issue->statuses;
209
            })->flatten(1)->map(function ($statuses) {
210
                return $statuses;
211
            })->sortByDesc('created_at');
212
213
        $activities->splice(15);
214
215
        return $activities->all();
216
    }
217
218
    public function issueTypes()
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...
219
    {
220
        $types = $this->issues->map(function ($issue) {
0 ignored issues
show
Documentation introduced by
The property issues does not exist on object<GitScrum\Models\Sprint>. 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...
221
            return $issue->type;
222
        })->groupBy('slug')->map(function ($type) {
223
            return [
224
                'sprint' => $this->slug,
0 ignored issues
show
Documentation introduced by
The property slug does not exist on object<GitScrum\Models\Sprint>. 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...
225
                'slug' => $type->first()->slug,
226
                'title' => $type->first()->title,
227
                'color' => $type->first()->color,
228
                'total' => $type->count(), ];
229
        })->sortByDesc('total')->all();
230
231
        return $types;
232
    }
233
234
    public function issueStatus()
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...
235
    {
236
        $status = $this->issues->map(function ($issue) {
0 ignored issues
show
Documentation introduced by
The property issues does not exist on object<GitScrum\Models\Sprint>. 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...
237
            return $issue->status;
238
        })->groupBy('slug')->all();
239
240
        return $status;
241
    }
242
243
    public function getVisibilityAttribute()
244
    {
245
        return $this->attributes['is_private'] ? trans('Private') : trans('Public');
246
    }
247
248
    public function getSlugAttribute()
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...
249
    {
250
        return isset($this->attributes['slug']) ? $this->attributes['slug'] : '';
251
    }
252
253
    public function getTimeboxAttribute()
254
    {
255
        $date_start = isset($this->attributes['date_start']) ?
256
            Carbon::parse($this->attributes['date_start'])->toDateString() : '';
257
        $date_finish = isset($this->attributes['date_finish']) ?
258
            Carbon::parse($this->attributes['date_finish'])->toDateString() : '';
259
260
        return $date_start.' '.trans('to').' '.$date_finish;
261
    }
262
}
263