Passed
Pull Request — develop (#2)
by
unknown
04:32
created

Student   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 145
Duplicated Lines 13.79 %

Importance

Changes 0
Metric Value
dl 20
loc 145
rs 10
c 0
b 0
f 0
wmc 12

9 Methods

Rating   Name   Duplication   Size   Complexity  
A unenroll() 0 11 3
A proposedExchanges() 9 9 1
A enroll() 0 11 2
A isEnrolledInCourse() 0 3 1
A scopeWhereNumber() 0 3 1
A user() 0 3 1
A requestedExchanges() 9 9 1
A getEnrollmentByCourse() 0 3 1
A enrollments() 0 3 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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.
80
     *
81
     * @param \App\Judite\Models\Course $course
82
     *
83
     * @return \App\Judite\Models\Enrollment|null
84
     */
85
    public function getEnrollmentByCourse(Course $course)
86
    {
87
        return $this->enrollments()->whereCourseId($course->id)->first();
88
    }
89
90
    /**
91
     * Enroll this student with a given course.
92
     *
93
     * @param \App\Judite\Models\Course $course
94
     *
95
     * @throws \App\Exceptions\UserIsAlreadyEnrolledInCourseException
96
     *
97
     * @return \App\Judite\Models\Enrollment
98
     */
99
    public function enroll(Course $course): Enrollment
100
    {
101
        if ($this->isEnrolledInCourse($course)) {
102
            throw new UserIsAlreadyEnrolledInCourseException($course);
103
        }
104
105
        $enrollment = $this->enrollments()->make();
106
        $enrollment->course()->associate($course);
107
        $enrollment->save();
108
109
        return $enrollment;
110
    }
111
112
    /**
113
     * Check if this student is enrolled in a course.
114
     *
115
     * @param \App\Judite\Models\Course $course
116
     *
117
     * @return bool
118
     */
119
    public function isEnrolledInCourse(Course $course): bool
120
    {
121
        return $this->enrollments()->where('course_id', $course->id)->exists();
122
    }
123
124
    /**
125
     * Remove enrollment in the given course.
126
     *
127
     * @param \App\Judite\Models\Course $course
128
     *
129
     * @return bool
130
     */
131
    public function unenroll(Course $course): bool
132
    {
133
        $enrollment = $this->getEnrollmentByCourse($course);
134
135
        if (is_null($enrollment)) {
136
            throw new UserIsNotEnrolledInCourseException($course);
137
        } elseif ($enrollment->hasShift()) {
138
            throw new EnrollmentCannotBeDeleted($enrollment, 'The enrollment cannot be deleted because it as an associated shift.');
139
        }
140
141
        return $this->enrollments()->where('course_id', $course->id)->delete();
142
    }
143
144
    /**
145
     * Scope a query to only include users with the given student number.
146
     *
147
     * @param \Illuminate\Database\Eloquent\Builder $query
148
     * @param string                                $studentNumber
149
     *
150
     * @return \Illuminate\Database\Eloquent\Builder
151
     */
152
    public function scopeWhereNumber($query, $studentNumber)
153
    {
154
        return $query->where('student_number', $studentNumber);
155
    }
156
}
157