Passed
Push — master ( fbc61a...4022cc )
by Davide
73:49 queued 40:24
created

Teacher   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Test Coverage

Coverage 72.21%

Importance

Changes 6
Bugs 3 Features 1
Metric Value
wmc 7
eloc 37
c 6
b 3
f 1
dl 0
loc 101
ccs 26
cts 36
cp 0.7221
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A events() 0 3 1
A eventsByTeacher() 0 13 1
A user() 0 3 1
A preSave() 0 33 4
1
<?php
2
3
namespace DavideCasiraghi\LaravelEventsCalendar\Models;
4
5
use DavideCasiraghi\LaravelEventsCalendar\Facades\LaravelEventsCalendar;
6
use Illuminate\Database\Eloquent\Model;
7
8
use Illuminate\Support\Str;
9
10
class Teacher extends Model
11
{
12
    /***************************************************************************/
13
    /**
14
     * The table associated with the model.
15
     *
16
     * @var string
17
     */
18
    protected $table = 'teachers';
19
20
    /***************************************************************************/
21
22
    protected $fillable = [
23
        'name', 'bio', 'country_id', 'year_starting_practice', 'year_starting_teach', 'significant_teachers', 'profile_picture', 'website', 'facebook', 'created_by', 'slug',
24
    ];
25
26
    /***************************************************************************/
27
28
    /**
29
     * Get the user that owns the event. eg. $teacher->user.
30
     */
31
    public function user()
32
    {
33
        return $this->belongsTo('\Illuminate\Foundation\Auth\User', 'created_by');
34
    }
35
36
    /***************************************************************************/
37
38
    /**
39
     * Get the events for the teacher.
40
     */
41 3
    public function events()
42
    {
43 3
        return $this->belongsToMany('DavideCasiraghi\LaravelEventsCalendar\Models\Event', 'event_has_teachers', 'teacher_id', 'event_id');
44
    }
45
46
    /***************************************************************************/
47
48
    /**
49
     * Get the events where this teacher is going to teach to.
50
     *
51
     * @param  \DavideCasiraghi\LaravelEventsCalendar\Models\Teacher  $teacher
52
     * @return \Illuminate\Http\Response
53
     */
54 3
    public static function eventsByTeacher($teacher, $lastestEventsRepetitionsQuery)
55
    {
56 3
        $ret = $teacher->events()
57 3
                         ->select('events.title', 'events.category_id', 'events.slug', 'event_venues.name AS venue_name', 'countries.name AS country', 'event_venues.city AS city', 'events.sc_teachers_names', 'event_repetitions.start_repeat', 'event_repetitions.end_repeat')
58 3
                         ->join('event_venues', 'event_venues.id', '=', 'events.venue_id')
59 3
                         ->join('countries', 'countries.id', '=', 'event_venues.country_id')
60
                         ->joinSub($lastestEventsRepetitionsQuery, 'event_repetitions', function ($join) {
61 3
                             $join->on('events.id', '=', 'event_repetitions.event_id');
62 3
                         })
63 3
                         ->orderBy('event_repetitions.start_repeat', 'asc')
64 3
                         ->get();
65
66 3
        return $ret;
67
    }
68
69
    /***************************************************************************/
70
71
    /**
72
     * Prepare the record to be saved on DB.
73
     *
74
     * @param  array  $requestArray
75
     * @param  \Illuminate\Http\UploadedFile  $profilePicture
76
     * @return void
77
     */
78 3
    public function preSave(array $requestArray, $profilePicture): void
79
    {
80 3
        $this->name = $requestArray['name'];
81 3
        $this->bio = clean($requestArray['bio']);
82 3
        $this->country_id = $requestArray['country_id'];
83 3
        $this->year_starting_practice = $requestArray['year_starting_practice'];
84 3
        $this->year_starting_teach = $requestArray['year_starting_teach'];
85 3
        $this->significant_teachers = $requestArray['significant_teachers'];
86
87
        // Teacher profile picture upload
88 3
        if (! empty($profilePicture)) {
89
            $imageFile = $profilePicture;
90
            $imageName = $imageFile->hashName();
91
            $imageSubdir = 'teachers_profile';
92
            $imageWidth = 968;
93
            $thumbWidth = 300;
94
95
            LaravelEventsCalendar::uploadImageOnServer($imageFile, $imageName, $imageSubdir, $imageWidth, $thumbWidth);
0 ignored issues
show
Bug introduced by
The method uploadImageOnServer() does not exist on DavideCasiraghi\LaravelE...s\LaravelEventsCalendar. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

95
            LaravelEventsCalendar::/** @scrutinizer ignore-call */ 
96
                                   uploadImageOnServer($imageFile, $imageName, $imageSubdir, $imageWidth, $thumbWidth);
Loading history...
96
            $this->profile_picture = $imageName;
97
        } else {
98 3
            if (array_key_exists('profile_picture', $requestArray)) {
99
                $this->profile_picture = $requestArray['profile_picture'];
100
            }
101
        }
102
103 3
        $this->website = $requestArray['website'];
104 3
        $this->facebook = $requestArray['facebook'];
105
106
        //$this->created_by = Auth::id();$requestArray['created_by'];
107 3
        $this->created_by = $requestArray['created_by'];
108
109 3
        if (! $this->slug) {
110 2
            $this->slug = Str::slug($this->name, '-').'-'.rand(10000, 100000);
111
        }
112 3
    }
113
114
    /***************************************************************************/
115
}
116