Test Setup Failed
Push — main ( 6d9d37...ec6371 )
by Davide
05:55 queued 12s
created

Event::toSeoStructuredDataScript()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 28
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 21
nc 1
nop 0
dl 0
loc 28
ccs 1
cts 1
cp 1
crap 1
rs 9.584
c 0
b 0
f 0
1
<?php
2
3
namespace App\Models;
4
5
use Illuminate\Database\Eloquent\Factories\HasFactory;
6
use Illuminate\Database\Eloquent\Model;
7
use Spatie\MediaLibrary\HasMedia;
8
use Spatie\MediaLibrary\InteractsWithMedia;
9
use Spatie\MediaLibrary\MediaCollections\Models\Media;
10
use Spatie\SchemaOrg\Schema;
11
use Spatie\Sluggable\HasSlug;
12
use Spatie\Sluggable\SlugOptions;
13
14
class Event extends Model implements HasMedia
15
{
16
    use HasFactory;
17
    use HasSlug;
0 ignored issues
show
introduced by
The trait Spatie\Sluggable\HasSlug requires some properties which are not provided by App\Models\Event: $slugSeparator, $preventOverwrite, $maximumLength, $generateUniqueSlugs, $generateSlugsOnCreate, $slugField, $generateSlugFrom, $slugLanguage, $generateSlugsOnUpdate
Loading history...
18
    use InteractsWithMedia;
0 ignored issues
show
introduced by
The trait Spatie\MediaLibrary\InteractsWithMedia requires some properties which are not provided by App\Models\Event: $fallbackPath, $mediaConversionRegistrations, $forceDeleting, $fallbackUrl, $media, $collection_name
Loading history...
19
20
    /**
21
     * The attributes that aren't mass assignable.
22
     *
23
     * @var array
24
     */
25
    protected $guarded = [];
26
27
    /**
28
     * The attributes that should be mutated to dates.
29
     *
30
     * @var array
31
     */
32
    protected $dates = [
33
        'repeat_until',
34
    ];
35
36
    /**
37
     * The parameters used in the index view search filters.
38
     *
39
     * @var array
40
     */
41
    public const SEARCH_PARAMETERS = [
42
        'title',
43
        'eventCategoryId',
44
        'startDate',
45
        'endDate',
46
        'teacherId',
47
        'organizerId',
48
        'repetitionKindId',
49
        'venueId',
50
        'is_published'
51
    ];
52
53
    /**
54
     * The possible values the publishing status can be.
55
     */
56
    public const PUBLISHING_STATUS = [
57
        0 => 'unpublished',
58
        1 => 'published',
59
    ];
60
61
    /**
62
     * Generates a unique slug.
63 27
     */
64
    public function getSlugOptions(): SlugOptions
65 27
    {
66 27
        return SlugOptions::create()
67 27
            ->generateSlugsFrom('title')
68
            ->saveSlugsTo('slug');
69
    }
70
71
    /**
72
     * Return the user that created the event
73
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
74
     */
75
    public function user()
76
    {
77
        return $this->belongsTo(User::class);
78
    }
79
80
    /**
81
     * Returns the category of the event.
82
     */
83
    public function category()
84
    {
85
        return $this->belongsTo(EventCategory::class, 'event_category_id', 'id');
86
    }
87
88
    /**
89
     * Returns the venue of the event.
90
     */
91
    public function venue()
92
    {
93
        return $this->belongsTo(Venue::class); // 1-to-1 (one event can have just one venue)
94
    }
95
96
    /**
97
     * Returns the teachers of the event.
98 5
     */
99
    public function teachers()
100 5
    {
101
        return $this->belongsToMany(Teacher::class);
102
    }
103
104
    /**
105
     * Returns the organizers of the event.
106 5
     */
107
    public function organizers()
108 5
    {
109
        return $this->belongsToMany(Organizer::class);
110
    }
111
112
    /**
113
     * Returns the repetitions of the event.
114 1
     */
115
    public function repetitions()
116 1
    {
117
        return $this->hasMany(EventRepetition::class);
118
    }
119
120
    /**
121
     * Get the repeat type of the event.
122
     */
123
    /*public function repeat_type() {
124
        return $this->belongsTo(EventRepeatType::class); // 1-to-1 (one event can have just one repeat type)
125
    }*/
126
127
    /**
128
     * Return the post publishing status
129
     *
130 27
     * @return string
131 27
     */
132
    public function publishingStatus(): string
133 27
    {
134 27
        return self::PUBLISHING_STATUS[$this->is_published];
135 27
    }
136 27
137
    /**
138
     * Add Image gallery support using:
139
     * https://spatie.be/docs/laravel-medialibrary/v8/introduction
140
     * https://github.com/ebess/advanced-nova-media-library
141
     *
142
     * @param \Spatie\MediaLibrary\MediaCollections\Models\Media|null $media
143
     *
144
     * @throws \Spatie\Image\Exceptions\InvalidManipulation
145
     */
146
    public function registerMediaConversions(Media $media = null): void
147
    {
148
        $this->addMediaConversion('thumb')
149
            ->width(300)
150
            ->height(300);
151
152
        $this->addMediaConversion('facebook')
153
            ->width(1200)
154
            ->height(630);
155
156
        $this->addMediaConversion('twitter')
157
            ->width(1024)
158
            ->height(512);
159
    }
160
161
    public function registerMediaCollections(): void
162
    {
163
        $this->addMediaCollection('introimage')->singleFile();
164
    }
165
166
    /**
167
     * Return true if the event is published
168
     *
169
     * @return bool
170
     */
171
    public function isPublished(): bool
172
    {
173
        return $this->is_published;
174 1
    }
175
176 1
    public function toSeoStructuredDataScript(): string
177
    {
178
        return Schema::event()
179
            ->name($this->title)
180
            ->description($this->description)
181
            ->if($this->hasMedia('introimage'), function (\Spatie\SchemaOrg\Event $schema) {
182
                $schema->image($this->getMedia('introimage')->first()->getUrl('thumb'));
183
            })
184
            ->startDate($this->repetitions()->first()->start_repeat)
185
            ->endDate($this->repetitions()->first()->end_repeat)
186
            ->performer(Schema::person()
187
                           ->name($this->teachers()->first()->name)
188
            )
189
            ->organizer(Schema::person()
190
                            ->name($this->organizers()->first()->name)
191
                            ->url($this->organizers()->first()->website)
192
            )
193
            ->location(Schema::place()
194
                            ->name($this->venue->name)
195
                            ->address(Schema::postalAddress()
196
                                        ->streetAddress($this->venue->address)
197
                                        ->addressLocality($this->venue->city)
198
                                        ->addressRegion($this->venue->state_province)
199
                                        ->postalCode($this->venue->zip_code)
200
                                        ->addressCountry($this->venue->country->code)
201
                            )
202
            )
203
        ->toScript();
204
    }
205
}
206