Teacher::preSave()   A
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 33
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 4.7691

Importance

Changes 3
Bugs 1 Features 0
Metric Value
eloc 22
c 3
b 1
f 0
dl 0
loc 33
ccs 14
cts 22
cp 0.6364
rs 9.568
cc 4
nc 6
nop 2
crap 4.7691
1
<?php
2
3
namespace DavideCasiraghi\LaravelEventsCalendar\Models;
4
5
use DavideCasiraghi\LaravelEventsCalendar\Facades\LaravelEventsCalendar;
6
use Illuminate\Database\Eloquent\Model;
7
use Illuminate\Support\Str;
8
9
class Teacher extends Model
10
{
11
    /***************************************************************************/
12
    /**
13
     * The table associated with the model.
14
     *
15
     * @var string
16
     */
17
    protected $table = 'teachers';
18
19
    /***************************************************************************/
20
21
    protected $fillable = [
22
        'name', 'bio', 'country_id', 'year_starting_practice', 'year_starting_teach', 'significant_teachers', 'profile_picture', 'website', 'facebook', 'created_by', 'slug',
23
    ];
24
25
    /***************************************************************************/
26
27
    /**
28
     * Get the user that owns the teacher. eg. $teacher->user.
29
     */
30 1
    public function user()
31
    {
32 1
        return $this->belongsTo('\Illuminate\Foundation\Auth\User', 'created_by');
33
    }
34
35
    /***************************************************************************/
36
37
    /**
38
     * Get the events for the teacher.
39
     */
40 3
    public function events()
41
    {
42 3
        return $this->belongsToMany('DavideCasiraghi\LaravelEventsCalendar\Models\Event', 'event_has_teachers', 'teacher_id', 'event_id');
43
    }
44
45
    /***************************************************************************/
46
47
    /**
48
     * Get the events where this teacher is going to teach to.
49
     *
50
     * @param  \DavideCasiraghi\LaravelEventsCalendar\Models\Teacher  $teacher
51
     * @return \Illuminate\Http\Response
52
     */
53 3
    public static function eventsByTeacher($teacher, $lastestEventsRepetitionsQuery)
54
    {
55 3
        $ret = $teacher->events()
56 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')
57 3
                         ->join('event_venues', 'event_venues.id', '=', 'events.venue_id')
58 3
                         ->join('countries', 'countries.id', '=', 'event_venues.country_id')
59
                         ->joinSub($lastestEventsRepetitionsQuery, 'event_repetitions', function ($join) {
60 3
                             $join->on('events.id', '=', 'event_repetitions.event_id');
61 3
                         })
62 3
                         ->orderBy('event_repetitions.start_repeat', 'asc')
63 3
                         ->get();
64
65 3
        return $ret;
66
    }
67
68
    /***************************************************************************/
69
70
    /**
71
     * Prepare the record to be saved on DB.
72
     *
73
     * @param  array  $requestArray
74
     * @param  \Illuminate\Http\UploadedFile  $profilePicture
75
     * @return void
76
     */
77 4
    public function preSave(array $requestArray, $profilePicture): void
78
    {
79 4
        $this->name = $requestArray['name'];
80 4
        $this->bio = clean($requestArray['bio']);
81 4
        $this->country_id = $requestArray['country_id'] ?? null;
82 4
        $this->year_starting_practice = $requestArray['year_starting_practice'] ?? null;
83 4
        $this->year_starting_teach = $requestArray['year_starting_teach'] ?? null;
84 4
        $this->significant_teachers = $requestArray['significant_teachers'] ?? null;
85
86
        // Teacher profile picture upload
87 4
        if (! empty($profilePicture)) {
88
            $imageFile = $profilePicture;
89
            $imageName = $imageFile->hashName();
90
            $imageSubdir = 'teachers_profile';
91
            $imageWidth = 968;
92
            $thumbWidth = 300;
93
94
            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

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