Passed
Push — master ( 7d31db...e2c13b )
by Davide
99:42 queued 65:34
created

Teacher   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Test Coverage

Coverage 72.97%

Importance

Changes 5
Bugs 2 Features 1
Metric Value
wmc 7
eloc 38
c 5
b 2
f 1
dl 0
loc 103
ccs 27
cts 37
cp 0.7297
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 35 4
1
<?php
2
3
namespace DavideCasiraghi\LaravelEventsCalendar\Models;
4
5
use DavideCasiraghi\LaravelEventsCalendar\Facades\LaravelEventsCalendar;
6
use Illuminate\Database\Eloquent\Model;
7
use Illuminate\Http\Request; // to remove
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
        $requestArray['website'];
81
        
82 3
        $this->name = $requestArray['name'];
83 3
        $this->bio = clean($requestArray['bio']);
84 3
        $this->country_id = $requestArray['country_id'];
85 3
        $this->year_starting_practice = $requestArray['year_starting_practice'];;
86 3
        $this->year_starting_teach = $requestArray['year_starting_teach'];;
87 3
        $this->significant_teachers = $requestArray['significant_teachers'];;
88
89
        // Teacher profile picture upload
90 3
        if (! empty($profilePicture)) {
91
            $imageFile = $profilePicture;
92
            $imageName = $imageFile->hashName();
93
            $imageSubdir = 'teachers_profile';
94
            $imageWidth = 968;
95
            $thumbWidth = 300;
96
97
            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

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