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

Teacher::generateStructuredDataScript()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 9
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 12
ccs 0
cts 0
cp 0
crap 2
rs 9.9666
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 Teacher 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\Teacher: $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\Teacher: $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 parameters used in the index view search filters.
32
     *
33
     * @var array
34
     */
35
    public const SEARCH_PARAMETERS = [
36
        'name',
37
        'surname',
38
        'countryId',
39
    ];
40
41
    /**
42
     * Return the user that created the teacher
43
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
44
     */
45
    public function user()
46
    {
47
        return $this->belongsTo(User::class);
48
    }
49
50
    /**
51
     * Return the country of the teacher
52
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
53
     */
54
    public function country()
55
    {
56
        return $this->belongsTo(Country::class);
57
    }
58
59
    /**
60
     * Returns the events of the teacher.
61
     */
62
    public function events()
63
    {
64
        return $this->belongsToMany(Event::class);
65
    }
66
67
    /**
68 33
     * Generates a unique slug.
69
     */
70 33
    public function getSlugOptions(): SlugOptions
71 33
    {
72 33
        return SlugOptions::create()
73
            ->generateSlugsFrom('name')
74
            ->saveSlugsTo('slug');
75
    }
76
77
    /**
78
     * Add Image gallery support using:
79
     * https://spatie.be/docs/laravel-medialibrary/v8/introduction
80
     * https://github.com/ebess/advanced-nova-media-library
81
     *
82
     * @param \Spatie\MediaLibrary\MediaCollections\Models\Media|null $media
83
     *
84
     * @throws \Spatie\Image\Exceptions\InvalidManipulation
85
     */
86
    public function registerMediaConversions(Media $media = null): void
87
    {
88
        $this->addMediaConversion('thumb')
89
            ->width(300)
90
            ->height(300);
91
92
        $this->addMediaConversion('facebook')
93
            ->width(1200)
94
            ->height(630);
95
96
        $this->addMediaConversion('twitter')
97
            ->width(1024)
98
            ->height(512);
99
    }
100
101
    public function registerMediaCollections(): void
102
    {
103
        $this->addMediaCollection('profile_picture')->singleFile();
104
    }
105
106
    /**
107
     * Teacher full_name accessor.
108
     * $teacher->full_name
109
     *
110
     * @return string
111
     */
112
    public function getFullNameAttribute(): string
113
    {
114
        return "{$this->name} {$this->surname}";
115
    }
116
117
    /**
118
     * Factory method for generating the script for a Schema.org type.
119
     *
120
     * @return Type
121
     */
122
    protected function generateStructuredDataScript(): Type
123
    {
124
        return Schema::person()
125
            ->name($this->full_name)
126
            ->if($this->hasMedia('profile_picture'), function (\Spatie\SchemaOrg\Person $schema) {
127
                $schema->image($this->getMedia('profile_picture')->first()->getUrl());
128
            })
129
            ->jobTitle('Teacher')
130
            ->url(env('APP_URL').'/teachers/'.$this->slug)
131
            ->sameAs([
132
              $this->facebook,
133
              $this->website
134
            ]);
135
    }
136
}
137