Passed
Push — main ( 5debc0...5f2847 )
by Michael
03:26
created

SeoComposer::getInstance()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 1
c 1
b 1
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 2
crap 1
1
<?php
2
3
namespace MichaelRubel\SeoManager\Composers;
4
5
use Illuminate\Database\Eloquent\Builder;
6
use Illuminate\Database\Eloquent\Model;
7
use Illuminate\Support\Collection;
8
use Illuminate\Support\Str;
9
use Illuminate\View\View;
10
use MichaelRubel\SeoManager\Exceptions\ShouldImplementSeoTagInterfaceException;
11
use MichaelRubel\SeoManager\Models\SeoTag;
12
use MichaelRubel\SeoManager\Models\SeoTagContract;
13
14
class SeoComposer
15
{
16
    /**
17
     * @var Collection|null
18
     */
19
    protected ?Collection $seo_manager;
20
21
    /**
22
     * SeoComposer constructor.
23
     */
24 6
    public function __construct()
25
    {
26 6
        $this->seo_manager = $this->getSeoTags();
27 5
    }
28
29
    /**
30
     * Bind data to the view.
31
     *
32
     * @param  View  $view
33
     * @return void
34
     */
35 5
    public function compose(View $view): void
36
    {
37 5
        $variable = config('seo-manager.variable_name');
38
39 5
        $view->with(
40 5
            is_string($variable)
41 5
                ? $variable
42 5
                : 'seo_manager',
43 5
            $this->seo_manager
44
        );
45 5
    }
46
47
    /**
48
     * @return Collection|null
49
     * @throws ShouldImplementSeoTagInterfaceException
50
     */
51 6
    protected function getSeoTags(): ?Collection
52
    {
53 6
        $url = Str::start(
54 6
            request()->path(),
55 6
            '/'
56
        );
57
58 6
        $wildcardUrl = $this->wildcard($url);
59
60 6
        $configuredModel = config('seo-manager.model');
61
62 6
        $model = app(
63 6
            is_string($configuredModel) && class_exists($configuredModel)
64 6
                ? $configuredModel
65 6
                : SeoTag::class
66
        );
67
68 6
        if (! $model instanceof SeoTagContract) {
69 1
            throw new ShouldImplementSeoTagInterfaceException();
70
        }
71
72 5
        $instance = $this->getInstance($model, $url);
73
74 5
        if (! $instance->exists()) {
75 1
            $instance = $this->getInstance($model, $wildcardUrl);
76
        }
77
78 5
        return $instance->first()?->{$model->getTagsColumnName()};
79
    }
80
81
    /**
82
     * @param string $url
83
     *
84
     * @return string
85
     */
86 6
    protected function wildcard(string $url): string
87
    {
88 6
        $array = explode('/', $url);
89
90 6
        array_pop($array);
91 6
        array_push($array, '*');
92
93 6
        return implode('/', $array);
94
    }
95
96
    /**
97
     * @param SeoTagContract $model
98
     * @param string         $url
99
     *
100
     * @return Builder
101
     */
102 5
    private function getInstance(SeoTagContract $model, string $url): Builder
103
    {
104 5
        return $model::where($model->getUrlColumnName(), $url);
0 ignored issues
show
Bug introduced by
The method where() does not exist on MichaelRubel\SeoManager\Models\SeoTagContract. Since it exists in all sub-types, consider adding an abstract or default implementation to MichaelRubel\SeoManager\Models\SeoTagContract. ( Ignorable by Annotation )

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

104
        return $model::/** @scrutinizer ignore-call */ where($model->getUrlColumnName(), $url);
Loading history...
105
    }
106
}
107