Passed
Pull Request — develop (#18)
by
unknown
07:13
created

Group::pendingStudents()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace App\Judite\Models;
4
5
use Illuminate\Database\Eloquent\Model;
6
use App\Exceptions\GroupIsFullException;
7
use App\Exceptions\StudentIsNotEnrolledInCourseException;
8
use App\Exceptions\StudentHasAlreadyGroupInCourseException;
9
10
class Group extends Model
11
{
12
    /**
13
     * The attributes that are mass assignable.
14
     *
15
     * @var array
16
     */
17
    protected $fillable = ['name'];
18
19
    protected $visible = ['id','course','students'];
20
21
    /**
22
     * Get course of this group.
23
     *
24
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
25
     */
26
    public function course()
27
    {
28
        return $this->belongsTo(Course::class);
29
    }
30
31
    /**
32
     * Get students for this group.
33
     *
34
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
35
     */
36
    public function students()
37
    {
38
        return $this->belongsToMany(Student::class)
39
                    ->as('invitation')
40
                    ->withPivot('accepted_at');
41
    }
42
43
    /**
44
     * Get confirmed students for this group.
45
     *
46
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
47
     */
48
    public function confirmedStudents()
49
    {
50
        return $this->students()->wherePivot('accepted_at', '!=', null);
51
    }
52
53
    /**
54
     * Get pending students for this group.
55
     *
56
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
57
     */
58
    public function pendingStudents()
59
    {
60
        return $this->students()->wherePivot('accepted_at', '=', null);
61
    }
62
63
    /**
64
     * Check if this group is eligible for acceptance.
65
     *
66
     * @return bool
67
     */
68
    public function isEligibleForAcceptance()
69
    {
70
        $currentStudentsCount = $this->relationLoaded('students')
71
            ? $this->students->count()
72
            : $this->students()->count();
73
74
        return $currentStudentsCount >= app('settings')->min_group_members;
0 ignored issues
show
Bug introduced by
The property min_group_members does not seem to exist on App\Judite\Models\Settings. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
75
    }
76
77
    /**
78
     * Check if this group is full.
79
     *
80
     * @return bool
81
     */
82
    public function isFull()
83
    {
84
        $currentStudentsCount = $this->relationLoaded('students')
85
            ? $this->stisEligibleForAcceptanceudents->count()
0 ignored issues
show
Bug introduced by
The property stisEligibleForAcceptanceudents does not seem to exist on App\Judite\Models\Group. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
86
            : $this->students()->count();
87
88
        return $currentStudentsCount === app('settings')->max_group_members;
0 ignored issues
show
Bug introduced by
The property max_group_members does not seem to exist on App\Judite\Models\Settings. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
89
    }
90
91
    /**
92
     * Check if this group is empty.
93
     *
94
     * @return bool
95
     */
96
    public function isEmpty()
97
    {
98
        return ! $this->students()->exists();
99
    }
100
101
    /**
102
     * Check if this group is available.
103
     *
104
     * @return bool
105
     */
106
    public function isAvailableToJoin()
107
    {
108
        $currentStudentsCount = $this->relationLoaded('students')
109
            ? $this->students->count()
110
            : $this->students()->count();
111
112
        return $currentStudentsCount < app('settings')->max_group_members;
0 ignored issues
show
Bug introduced by
The property max_group_members does not seem to exist on App\Judite\Models\Settings. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
113
    }
114
115
    /**
116
     * Add the given student to this group.
117
     *
118
     * @param \App\Judite\Models\Student $student
119
     *
120
     * @throws \App\Exceptions\GroupIsFullException
121
     *
122
     * @return bool
123
     */
124
    public function addMember(Student $student)
125
    {
126
        throw_if($this->isFull(), new GroupIsFullException('The group is full.'));
127
        
128
        throw_if(
129
            $student->hasGroupInCourse($this->course),
130
            new StudentHasAlreadyGroupInCourseException()
131
        );
132
133
        throw_unless(
134
            $student->isEnrolledInCourse($this->course), 
135
            new StudentIsNotEnrolledInCourseException($this->course)
136
        );
137
138
        if ($student->isAssociatedToGroup($this)) {
139
            return true;
140
        }
141
142
        return $this->students()->save($student);
143
    }
144
145
    /**
146
     * Remove the given member from this group.
147
     *
148
     * @param \App\Judite\Models\Student $member
149
     *
150
     * @return $this
151
     */
152
    public function removeMember(Student $member)
153
    {
154
        if (! $member->isAssociatedToGroup($this)) {
155
            return $this;
156
        }
157
158
        $this->students()->detach($member->id);
159
        $this->save();
160
161
        if ($this->isEmpty()) {
162
            $this->delete();
163
        }
164
165
        return $this;
166
    }
167
168
}
169