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

Student   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 137
Duplicated Lines 14.6 %

Importance

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

9 Methods

Rating   Name   Duplication   Size   Complexity  
A proposedExchanges() 9 9 1
A user() 0 3 1
A requestedExchanges() 9 9 1
A enrollments() 0 3 1
A unenroll() 0 3 1
A enroll() 0 11 2
A getEnrollmentByCourseId() 0 3 1
A isEnrolledInCourse() 0 3 1
A scopeWhereNumber() 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\UserIsAlreadyEnrolledInCourseException;
7
8
class Student extends Model
9
{
10
    /**
11
     * The relations to eager load on every query.
12
     *
13
     * @var array
14
     */
15
    protected $with = ['user'];
16
17
    /**
18
     * The attributes that are mass assignable.
19
     *
20
     * @var array
21
     */
22
    protected $fillable = ['student_number'];
23
24
    /**
25
     * Get user who owns this student.
26
     *
27
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
28
     */
29
    public function user()
30
    {
31
        return $this->belongsTo(User::class);
32
    }
33
34
    /**
35
     * Get exchanges requested by this student.
36
     *
37
     * @return \Illuminate\Database\Eloquent\Builder
38
     */
39 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...
40
    {
41
        $enrollmentsRelationship = $this->enrollments();
42
        $enrollmentsKeyName = $enrollmentsRelationship->getRelated()->getKeyName();
43
        $enrollmentsIdsQuery = $enrollmentsRelationship
44
            ->select($enrollmentsKeyName)
45
            ->getBaseQuery();
46
47
        return Exchange::whereFromEnrollmentIn($enrollmentsIdsQuery);
48
    }
49
50
    /**
51
     * Get exchanges proposed to this student.
52
     *
53
     * @return \Illuminate\Database\Eloquent\Builder
54
     */
55 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...
56
    {
57
        $enrollmentsRelationship = $this->enrollments();
58
        $enrollmentsKeyName = $enrollmentsRelationship->getRelated()->getKeyName();
59
        $enrollmentsIdsQuery = $enrollmentsRelationship
60
            ->select($enrollmentsKeyName)
61
            ->getBaseQuery();
62
63
        return Exchange::whereToEnrollmentIn($enrollmentsIdsQuery);
64
    }
65
66
    /**
67
     * Get enrollments of this student.
68
     *
69
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
70
     */
71
    public function enrollments()
72
    {
73
        return $this->hasMany(Enrollment::class);
74
    }
75
76
    /**
77
     * Get enrollment of this student in a given course.
78
     *
79
     * @param \App\Judite\Models\Course $course
80
     *
81
     * @return \App\Judite\Models\Enrollment|null
82
     */
83
    public function getEnrollmentByCourseId(Course $course)
84
    {
85
        return $this->enrollments()->whereCourseId($course->id)->first();
86
    }
87
88
    /**
89
     * Enroll this student with a given course.
90
     *
91
     * @param \App\Judite\Models\Course $course
92
     *
93
     * @throws \App\Exceptions\UserIsAlreadyEnrolledInCourseException
94
     *
95
     * @return \App\Judite\Models\Enrollment
96
     */
97
    public function enroll(Course $course): Enrollment
98
    {
99
        if ($this->isEnrolledInCourse($course)) {
100
            throw new UserIsAlreadyEnrolledInCourseException($course);
101
        }
102
103
        $enrollment = $this->enrollments()->make();
104
        $enrollment->course()->associate($course);
105
        $enrollment->save();
106
107
        return $enrollment;
108
    }
109
110
    /**
111
     * Check if this student is enrolled in a course.
112
     *
113
     * @param \App\Judite\Models\Course $course
114
     *
115
     * @return bool
116
     */
117
    public function isEnrolledInCourse(Course $course): bool
118
    {
119
        return $this->enrollments()->where('course_id', $course->id)->exists();
120
    }
121
122
    /**
123
     * Remove enrollment in the given course.
124
     *
125
     * @param \App\Judite\Models\Course $course
126
     *
127
     * @return bool
128
     */
129
    public function unenroll(Course $course): bool
130
    {
131
        return $this->enrollments()->where('course_id', $course->id)->delete();
132
    }
133
134
    /**
135
     * Scope a query to only include users with the given student number.
136
     *
137
     * @param \Illuminate\Database\Eloquent\Builder $query
138
     * @param string                                $studentNumber
139
     *
140
     * @return \Illuminate\Database\Eloquent\Builder
141
     */
142
    public function scopeWhereNumber($query, $studentNumber)
143
    {
144
        return $query->where('student_number', $studentNumber);
145
    }
146
}
147