Passed
Push — master ( 693377...95d81f )
by Davide
75:43
created

Teacher::preSave()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 32
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 3.3332

Importance

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

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