Passed
Pull Request — develop (#11)
by
unknown
03:42
created

Course::scopeGroupOrderedList()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace App\Judite\Models;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Laracasts\Presenter\PresentableTrait;
7
use App\Judite\Presenters\CoursePresenter;
8
9
class Course extends Model
10
{
11
    use PresentableTrait;
12
13
    /**
14
     * The presenter for this entity.
15
     *
16
     * @var string
17
     */
18
    protected $presenter = CoursePresenter::class;
19
20
    /**
21
     * Scope a query to order courses by year, semester and name.
22
     *
23
     * @param \Illuminate\Database\Eloquent\Builder $query
24
     *
25
     * @return \Illuminate\Database\Eloquent\Builder
26
     */
27
    public function scopeOrderedList($query)
28
    {
29
        return $query->orderBy('year', 'asc')
0 ignored issues
show
Bug Best Practice introduced by
The expression return $query->orderBy('...>orderBy('name', 'asc') also could return the type Illuminate\Database\Query\Builder which is incompatible with the documented return type Illuminate\Database\Eloquent\Builder.
Loading history...
30
            ->orderBy('semester', 'asc')
31
            ->orderBy('name', 'asc');
32
    }
33
34
    /**
35
     * Scope a query to order courses with groups by year, semester and name.
36
     *
37
     * @param \Illuminate\Database\Eloquent\Builder $query
38
     *
39
     * @return \Illuminate\Database\Eloquent\Builder
40
     */
41
    public function scopeGroupOrderedList($query)
42
    {
43
        return $query->where('group_min', '>', '0')
0 ignored issues
show
Bug Best Practice introduced by
The expression return $query->where('gr...>orderBy('name', 'asc') also could return the type Illuminate\Database\Query\Builder which is incompatible with the documented return type Illuminate\Database\Eloquent\Builder.
Loading history...
44
            ->orderBy('year', 'asc')
45
            ->orderBy('semester', 'asc')
46
            ->orderBy('name', 'asc');
47
    }
48
49
    /**
50
     * Get shifts of this course.
51
     *
52
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
53
     */
54
    public function shifts()
55
    {
56
        return $this->hasMany(Shift::class);
57
    }
58
59
    /**
60
     * Get enrollments on this course.
61
     *
62
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
63
     */
64
    public function enrollments()
65
    {
66
        return $this->hasMany(Enrollment::class);
67
    }
68
69
    /**
70
     * Add shift to this course.
71
     *
72
     * @param \App\Judite\Models\Shift $shift
73
     *
74
     * @return $this
75
     */
76
    public function addShift(Shift $shift): self
77
    {
78
        $this->shifts()->save($shift);
79
80
        return $this;
81
    }
82
83
    /**
84
     * Get shift of this course by tag.
85
     *
86
     * @param string $tag
87
     *
88
     * @return \App\Judite\Models\Shift|null
89
     */
90
    public function getShiftByTag($tag): ?Shift
91
    {
92
        return $this->shifts()->where('tag', $tag)->first();
93
    }
94
}
95