HasSeo   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 25
c 1
b 0
f 0
dl 0
loc 43
rs 10
wmc 11

2 Methods

Rating   Name   Duplication   Size   Complexity  
B bootHasSeo() 0 33 10
A seo() 0 3 1
1
<?php
2
3
namespace Giuga\LaravelSeoMetaBox\Traits;
4
5
use Giuga\LaravelSeoMetaBox\Models\Seo;
6
use Illuminate\Database\Eloquent\Model;
7
8
trait HasSeo
9
{
10
    abstract public function getSeoOptions(): SeoOptions;
11
12
    protected static function bootHasSeo()
13
    {
14
        static::saved(function (Model $model) {
15
            $options = $model->getSeoOptions();
16
            $seo = Seo::firstOrNew([
17
                'type' => get_class($model),
18
                'object_id' => $model->{$model->primaryKey},
19
            ]);
20
            $seo->slug = $options->routePrefix ?? '';
21
            if ($options->hasSlug) {
22
                $seo->slug .= $model->{$options->slugField};
23
            } else {
24
                $seo->slug .= $model->{$model->primaryKey};
25
            }
26
            if (($title = request()->get('seo_title', false)) !== false) {
27
                $seo->title = $title;
28
            } else {
29
                if ((empty($seo->title) || $options->overwriteOnUpdate) && isset($options->titleField)) {
30
                    $seo->title = $model->{$options->titleField};
31
                }
32
            }
33
            if (($description = request()->get('seo_description', false)) !== false) {
34
                $seo->description = $description;
35
            } else {
36
                if ((empty($seo->description) || $options->overwriteOnUpdate) && isset($options->descriptionField)) {
37
                    $seo->description = $model->{$options->descriptionField};
38
                }
39
            }
40
            $seo->save();
41
        });
42
43
        static::retrieved(function (Model $model) {
44
            app()->make('laravel-seo-meta-box')->addObjectOnPage(get_class($model), $model->{$model->primaryKey});
45
        });
46
    }
47
48
    public function seo()
49
    {
50
        return $this->hasOne(Seo::class, 'object_id')->where('type', static::class);
0 ignored issues
show
Bug introduced by
It seems like hasOne() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

50
        return $this->/** @scrutinizer ignore-call */ hasOne(Seo::class, 'object_id')->where('type', static::class);
Loading history...
51
    }
52
}
53