Passed
Push — develop ( a18085...de8258 )
by Francisco
14:47
created

Student   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 125
Duplicated Lines 16 %

Importance

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

8 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 enroll() 0 11 2
A isEnrolledInCourse() 0 3 1
A scopeWhereNumber() 0 3 1
A unenroll() 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
     * Enroll this student with a given course.
78
     *
79
     * @param \App\Judite\Models\Course $course
80
     *
81
     * @throws \App\Exceptions\UserIsAlreadyEnrolledInCourseException
82
     *
83
     * @return \App\Judite\Models\Enrollment
84
     */
85
    public function enroll(Course $course): Enrollment
86
    {
87
        if ($this->isEnrolledInCourse($course)) {
88
            throw new UserIsAlreadyEnrolledInCourseException($course);
89
        }
90
91
        $enrollment = $this->enrollments()->make();
92
        $enrollment->course()->associate($course);
93
        $enrollment->save();
94
95
        return $enrollment;
96
    }
97
98
    /**
99
     * Check if this student is enrolled in a course.
100
     *
101
     * @param \App\Judite\Models\Course $course
102
     *
103
     * @return bool
104
     */
105
    public function isEnrolledInCourse(Course $course): bool
106
    {
107
        return $this->enrollments()->where('course_id', $course->id)->exists();
108
    }
109
110
    /**
111
     * Remove enrollment in the given course.
112
     *
113
     * @param \App\Judite\Models\Course $course
114
     *
115
     * @return bool
116
     */
117
    public function unenroll(Course $course): bool
118
    {
119
        return $this->enrollments()->where('course_id', $course->id)->delete();
120
    }
121
122
    /**
123
     * Scope a query to only include users with the given student number.
124
     *
125
     * @param \Illuminate\Database\Eloquent\Builder $query
126
     * @param string                                $studentNumber
127
     *
128
     * @return \Illuminate\Database\Eloquent\Builder
129
     */
130
    public function scopeWhereNumber($query, $studentNumber)
131
    {
132
        return $query->where('student_number', $studentNumber);
133
    }
134
}
135