Glossary   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 117
Duplicated Lines 0 %

Test Coverage

Coverage 44.44%

Importance

Changes 0
Metric Value
eloc 34
dl 0
loc 117
ccs 12
cts 27
cp 0.4444
rs 10
c 0
b 0
f 0
wmc 7

7 Methods

Rating   Name   Duplication   Size   Complexity  
A variants() 0 3 1
A getSlugOptions() 0 5 1
A getSearchResult() 0 8 1
A publishingStatus() 0 3 1
A isPublished() 0 3 1
A registerMediaCollections() 0 3 1
A registerMediaConversions() 0 13 1
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\ModelStatus\HasStatuses;
11
use Spatie\Searchable\Searchable;
12
use Spatie\Searchable\SearchResult;
13
use Spatie\Sluggable\HasSlug;
14
use Spatie\Sluggable\SlugOptions;
15
use Spatie\Translatable\HasTranslations;
16
17
class Glossary extends Model implements HasMedia, Searchable
18
{
19
    use HasFactory;
20
    use HasSlug;
0 ignored issues
show
introduced by
The trait Spatie\Sluggable\HasSlug requires some properties which are not provided by App\Models\Glossary: $slugSeparator, $preventOverwrite, $maximumLength, $generateUniqueSlugs, $generateSlugsOnCreate, $slugOptions, $slugField, $generateSlugFrom, $slugLanguage, $generateSlugsOnUpdate
Loading history...
21
    use HasTranslations;
0 ignored issues
show
introduced by
The trait Spatie\Translatable\HasTranslations requires some properties which are not provided by App\Models\Glossary: $translationLocale, $translatable
Loading history...
22
    use InteractsWithMedia;
0 ignored issues
show
introduced by
The trait Spatie\MediaLibrary\InteractsWithMedia requires some properties which are not provided by App\Models\Glossary: $name, $mediaCollections, $deletePreservingMedia, $fallbackPath, $mediaConversionRegistrations, $mediaConversions, $forceDeleting, $fallbackUrl, $media, $unAttachedMediaLibraryItems, $collection_name
Loading history...
23
24
    /**
25
     * The attributes that aren't mass assignable.
26
     *
27
     * @var array
28
     */
29
    protected $guarded = [];
30
31
    /**
32
     * The attributes that are translatable.
33
     *
34
     * @var array
35
     */
36
    public $translatable = ['term','definition', 'body'];
37
38
    /**
39
     * The parameters used in the index view search filters.
40
     *
41
     * @var array
42
     */
43
    public const SEARCH_PARAMETERS = [
44
        'term',
45
        'is_published'
46
    ];
47
48
    /**
49
     * The possible values the publishing status can be.
50
     */
51
    public const PUBLISHING_STATUS = [
52
        0 => 'unpublished',
53
        1 => 'published',
54
    ];
55
56
    /**
57
     * Generates a unique slug.
58
     */
59
    public function getSlugOptions(): SlugOptions
60 10
    {
61
        return SlugOptions::create()
62 10
            ->generateSlugsFrom('term')
63 10
            ->saveSlugsTo('slug');
64 10
    }
65
66
    /**
67
     * Returns the variants of the glossary term.
68
     */
69
    public function variants()
70 11
    {
71
        return $this->hasMany(GlossaryVariant::class, 'glossary_id');
72 11
    }
73
74 11
    /**
75 10
     * Add Image gallery support using:
76 11
     * https://spatie.be/docs/laravel-medialibrary/v8/introduction
77 11
     * https://github.com/ebess/advanced-nova-media-library
78
     *
79
     * @param \Spatie\MediaLibrary\MediaCollections\Models\Media|null $media
80
     *
81
     * @throws \Spatie\Image\Exceptions\InvalidManipulation
82
     */
83
    public function registerMediaConversions(Media $media = null): void
84
    {
85
        $this->addMediaConversion('thumb')
86
            ->width(300)
87
            ->height(300);
88
89
        $this->addMediaConversion('facebook')
90
            ->width(1200)
91
            ->height(630);
92
93
        $this->addMediaConversion('twitter')
94
            ->width(1024)
95
            ->height(512);
96
    }
97
98
    public function registerMediaCollections(): void
99
    {
100
        $this->addMediaCollection('introimage')->singleFile();
101
    }
102
103
    /**
104
     * Return true if the glossary term is published
105
     *
106
     * @return bool
107
     */
108
    public function isPublished(): bool
109
    {
110
        return $this->is_published;
111
    }
112
113
    /**
114
     * Return the glossary term publishing status
115 1
     *
116
     * @return string
117 1
     */
118
    public function publishingStatus(): string
119
    {
120
        return self::PUBLISHING_STATUS[$this->is_published];
0 ignored issues
show
Bug introduced by
The constant App\Models\Glossary::PUBLISHING_STATUS was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
121
    }
122
123
    /**
124
     * Method required by Spatie Laravel Searchable
125
     */
126
    public function getSearchResult(): SearchResult
127
    {
128
        $url = route('glossaries.edit', $this->id);
129
130
        return new SearchResult(
131
            $this,
0 ignored issues
show
Bug introduced by
$this of type App\Models\Glossary is incompatible with the type Spatie\Searchable\Searchable expected by parameter $searchable of Spatie\Searchable\SearchResult::__construct(). ( Ignorable by Annotation )

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

131
            /** @scrutinizer ignore-type */ $this,
Loading history...
132
            $this->term,
133
            $url
134
        );
135
    }
136
}
137