Passed
Push — master ( d814e3...62a6b7 )
by Davide
128:23 queued 93:44
created

EventVenue::preSave()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 25
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 17
c 1
b 0
f 0
dl 0
loc 25
ccs 18
cts 18
cp 1
rs 9.7
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
namespace DavideCasiraghi\LaravelEventsCalendar\Models;
4
5
use Illuminate\Database\Eloquent\Model;
6
7
use Illuminate\Http\Request; // to remove
8
use Illuminate\Support\Str;
9
use DavideCasiraghi\LaravelEventsCalendar\Models\Country;
10
use DavideCasiraghi\LaravelEventsCalendar\Facades\LaravelEventsCalendar;
11
12
class EventVenue extends Model
13
{
14
    /***************************************************************************/
15
    /**
16
     * The table associated with the model.
17
     *
18
     * @var string
19
     */
20
    protected $table = 'event_venues';
21
22
    /***************************************************************************/
23
24
    protected $fillable = [
25
        'name', 'slug', 'continent_id', 'country_id', 'region_id', 'city', 'state_province', 'address', 'zip_code', 'extra_info', 'lat', 'lng', 'description', 'website', 'created_by', 'created_at', 'updated_at',
26
    ];
27
28
    /***************************************************************************/
29
30
    /**
31
     * Get the user that owns the event. eg. $eventVenue->user.
32
     */
33
    public function user()
34
    {
35
        return $this->belongsTo('\Illuminate\Foundation\Auth\User', 'created_by');
36
    }
37
38
    /***************************************************************************/
39
40
    /**
41
     * Return the venue name - used by - /http/resources/event.php.
42
     *
43
     * @param int $venueId
44
     * @return string
45
     */
46 3
    public static function getVenueName($venueId)
47
    {
48 3
        $ret = self::find($venueId)->name;
49
50 3
        return $ret;
51
    }
52
53
    /***************************************************************************/
54
55
    /**
56
     * Return true if the Venue contains any event.
57
     *
58
     * @param int $venueId
59
     * @return bool
60
     */
61 4
    public static function venueContainsEvents($venueId)
62
    {
63 4
        $events = Event::where('venue_id', '=', $venueId)->first();
64 4
        if ($events === null) {
65 2
            $ret = false;
66
        } else {
67 2
            $ret = true;
68
        }
69
70 4
        return $ret;
71
    }
72
    
73
    /***************************************************************************/
74
75
    /**
76
     * Prepare the record to be saved on DB.
77
     *
78
     * @param  \Illuminate\Http\Request  $request
79
     * @return void
80
     */
81 3
    public function preSave(Request $request): void
82
    {
83 3
        $this->name = $request->get('name');
84 3
        $this->description = clean($request->get('description'));
85 3
        $this->continent_id = Country::where('id', $request->get('country_id'))->pluck('continent_id')->first();
86 3
        $this->country_id = $request->get('country_id');
87 3
        $this->region_id = $request->get('region_id');
88 3
        $this->city = $request->get('city');
89 3
        $this->address = $request->get('address');
90 3
        $this->zip_code = $request->get('zip_code');
91 3
        $this->extra_info = $request->get('extra_info');
92 3
        $this->website = $request->get('website');
93
94
        // Get GPS coordinates
95 3
        $address = Country::getCountryName($this->country_id).', '.$this->city.', '.$this->address;
96 3
        $gpsCoordinates = LaravelEventsCalendar::getVenueGpsCoordinates($address);
0 ignored issues
show
Bug introduced by
The method getVenueGpsCoordinates() 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
        /** @scrutinizer ignore-call */ 
97
        $gpsCoordinates = LaravelEventsCalendar::getVenueGpsCoordinates($address);
Loading history...
97 3
        $this->lat = $gpsCoordinates['lat'];
98 3
        $this->lng = $gpsCoordinates['lng'];
99
100 3
        if (!$this->slug) {
101 2
            $this->slug = Str::slug($this->name, '-').rand(10000, 100000);
102
        }
103
104
        //$eventVenue->created_by = Auth::id();
105 3
        $this->created_by = $request->get('created_by');
106 3
    }
107
    
108
}
109