Test Setup Failed
Pull Request — main (#28)
by Enrico
07:55
created

Event::generateStructuredDataScript()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 26
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

192
            ->about(/** @scrutinizer ignore-type */ $this->category->name)
Loading history...
193
            ->startDate($this->repetitions()->first()->start_repeat)
194
            ->endDate($this->repetitions()->first()->end_repeat)
195
            ->performer(Schema::person()
196
                ->name($this->teachers()->first()->name)
197
            )
198
            ->organizer(Schema::person()
199
                ->name($this->organizers()->first()->name)
200
                ->url($this->organizers()->first()->website)
201
            )
202
            ->location(Schema::place()
203
                ->name($this->venue->name)
204
                ->address(Schema::postalAddress()
205
                    ->streetAddress($this->venue->address)
206
                    ->addressLocality($this->venue->city)
207
                    ->addressRegion($this->venue->state_province)
208
                    ->postalCode($this->venue->zip_code)
209
                    ->addressCountry($this->venue->country->code)
210
                )
211
            );
212
    }
213
}
214