HasSeoEntity::getNewInstanceSeoValueFor()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 6
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 12
ccs 7
cts 7
cp 1
crap 3
rs 10
1
<?php
2
3
namespace NovaSeoEntity\Models\Traits;
4
5
use Illuminate\Support\Str;
6
7
trait HasSeoEntity
8
{
9
    /**
10
     * Get seo information relationship.
11
     */
12 8
    public function seo_info(): \Illuminate\Database\Eloquent\Relations\MorphOne
13
    {
14 8
        return $this->morphOne(\NovaSeoEntity\Models\SEOInfo::class, 'seoptimisable');
0 ignored issues
show
Bug introduced by
It seems like morphOne() 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

14
        return $this->/** @scrutinizer ignore-call */ morphOne(\NovaSeoEntity\Models\SEOInfo::class, 'seoptimisable');
Loading history...
15
    }
16
17
    /**
18
     * Get seo information relationship with default value.
19
     */
20 2
    public function seo_info_forced(): \Illuminate\Database\Eloquent\Relations\MorphOne
21
    {
22 2
        return $this->morphOne(\NovaSeoEntity\Models\SEOInfo::class, 'seoptimisable')
23 2
                    ->withDefault([
24 2
                        'seoptimisable_type' => $this->getMorphClass(),
0 ignored issues
show
Bug introduced by
It seems like getMorphClass() 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

24
                        'seoptimisable_type' => $this->/** @scrutinizer ignore-call */ getMorphClass(),
Loading history...
25 2
                        'seoptimisable_id'   => $this->getKey(),
0 ignored issues
show
Bug introduced by
It seems like getKey() 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

25
                        'seoptimisable_id'   => $this->/** @scrutinizer ignore-call */ getKey(),
Loading history...
26 2
                    ]);
27
    }
28
29
    /**
30
     * Get default value.
31
     *
32
     * @param string $key
33
     *
34
     * @return mixed
35
     */
36 2
    public function getNewInstanceSeoValueFor(string $key): mixed
37
    {
38 2
        $method = 'getNewInstanceSeoValueFor' . Str::ucfirst(Str::camel($key));
39 2
        if (method_exists($this, $method)) {
40 2
            return $this->$method();
41
        }
42
43 2
        if (method_exists($this, 'getAttribute')) {
44 2
            return $this->getAttribute($key);
45
        }
46
47 1
        return null;
48
    }
49
50
    /**
51
     * Get value for.
52
     *
53
     * @param string $key
54
     * @param mixed|null $value
55
     *
56
     * @return mixed
57
     */
58 2
    public function getSEOFieldValue(string $key, mixed $value = null): mixed
59
    {
60 2
        $method = 'getSEO' . Str::ucfirst(Str::camel($key)) . 'FieldValue';
61 2
        if (method_exists($this, $method)) {
62 2
            return $this->$method($value);
63
        }
64
65 2
        return $value;
66
    }
67
}
68