Completed
Pull Request — develop (#2)
by
unknown
04:13
created

Student::getEnrollmentWithShiftByCourse()   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 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace App\Judite\Models;
4
5
use Illuminate\Database\Eloquent\Model;
6
use App\Exceptions\EnrollmentCannotBeDeleted;
7
use App\Exceptions\UserIsNotEnrolledInCourseException;
8
use App\Exceptions\UserIsAlreadyEnrolledInCourseException;
9
10
class Student extends Model
11
{
12
    /**
13
     * The relations to eager load on every query.
14
     *
15
     * @var array
16
     */
17
    protected $with = ['user'];
18
19
    /**
20
     * The attributes that are mass assignable.
21
     *
22
     * @var array
23
     */
24
    protected $fillable = ['student_number'];
25
26
    /**
27
     * Get user who owns this student.
28
     *
29
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
30
     */
31
    public function user()
32
    {
33
        return $this->belongsTo(User::class);
34
    }
35
36
    /**
37
     * Get exchanges requested by this student.
38
     *
39
     * @return \Illuminate\Database\Eloquent\Builder
40
     */
41 View Code Duplication
    public function requestedExchanges()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
42
    {
43
        $enrollmentsRelationship = $this->enrollments();
44
        $enrollmentsKeyName = $enrollmentsRelationship->getRelated()->getKeyName();
45
        $enrollmentsIdsQuery = $enrollmentsRelationship
46
            ->select($enrollmentsKeyName)
47
            ->getBaseQuery();
48
49
        return Exchange::whereFromEnrollmentIn($enrollmentsIdsQuery);
50
    }
51
52
    /**
53
     * Get exchanges proposed to this student.
54
     *
55
     * @return \Illuminate\Database\Eloquent\Builder
56
     */
57 View Code Duplication
    public function proposedExchanges()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
58
    {
59
        $enrollmentsRelationship = $this->enrollments();
60
        $enrollmentsKeyName = $enrollmentsRelationship->getRelated()->getKeyName();
61
        $enrollmentsIdsQuery = $enrollmentsRelationship
62
            ->select($enrollmentsKeyName)
63
            ->getBaseQuery();
64
65
        return Exchange::whereToEnrollmentIn($enrollmentsIdsQuery);
66
    }
67
68
    /**
69
     * Get enrollments of this student.
70
     *
71
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
72
     */
73
    public function enrollments()
74
    {
75
        return $this->hasMany(Enrollment::class);
76
    }
77
78
    /**
79
     * Get enrollment of this student in a given course with a associated shift.
80
     *
81
     * @param \App\Judite\Models\Course $course
82
     *
83
     * @return \App\Judite\Models\Enrollment|null
84
     */
85
    public function getEnrollmentWithShiftByCourse(Course $course)
86
    {
87
        return $this->enrollments()
88
            ->where('shift_id', '!=', null)
89
            ->where('course_id', $course->id)
90
            ->first();
91
    }
92
93
    /**
94
     * Enroll this student with a given course.
95
     *
96
     * @param \App\Judite\Models\Course $course
97
     *
98
     * @throws \App\Exceptions\UserIsAlreadyEnrolledInCourseException
99
     *
100
     * @return \App\Judite\Models\Enrollment
101
     */
102
    public function enroll(Course $course): Enrollment
103
    {
104
        if ($this->isEnrolledInCourse($course)) {
105
            throw new UserIsAlreadyEnrolledInCourseException($course);
106
        }
107
108
        $enrollment = $this->enrollments()->make();
109
        $enrollment->course()->associate($course);
110
        $enrollment->save();
111
112
        return $enrollment;
113
    }
114
115
    /**
116
     * Check if this student is enrolled in a course.
117
     *
118
     * @param \App\Judite\Models\Course $course
119
     *
120
     * @return bool
121
     */
122
    public function isEnrolledInCourse(Course $course): bool
123
    {
124
        return $this->enrollments()->where('course_id', $course->id)->exists();
125
    }
126
127
    /**
128
     * Remove enrollment in the given course.
129
     *
130
     * @param \App\Judite\Models\Course $course
131
     *
132
     * @throws \App\Exceptions\UserIsNotEnrolledInCourseException|\App\Exceptions\EnrollmentCannotBeDeleted
133
     *
134
     * @return bool
135
     */
136
    public function unenroll(Course $course): bool
137
    {
138
        if (! $this->isEnrolledInCourse($course)) {
139
            throw new UserIsNotEnrolledInCourseException($course);
140
        }
141
142
        $enrollment = $this->getEnrollmentWithShiftByCourse($course);
143
144
        if (is_null($enrollment)) {
145
            return $this->enrollments()->whereCourseId($course->id)->delete();
146
        } else {
147
            throw new EnrollmentCannotBeDeleted($enrollment, 'The enrollment cannot be deleted because it as an associated shift.');
148
        }
149
    }
150
151
    /**
152
     * Scope a query to only include users with the given student number.
153
     *
154
     * @param \Illuminate\Database\Eloquent\Builder $query
155
     * @param string                                $studentNumber
156
     *
157
     * @return \Illuminate\Database\Eloquent\Builder
158
     */
159
    public function scopeWhereNumber($query, $studentNumber)
160
    {
161
        return $query->where('student_number', $studentNumber);
162
    }
163
}
164