Passed
Push — main ( aabb18...58cd89 )
by Michael
03:44 queued 11s
created

SeoComposer::getMaxWildcardLevels()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 7
ccs 5
cts 5
cp 1
rs 10
cc 2
nc 2
nop 0
crap 2
1
<?php
2
3
namespace MichaelRubel\SeoManager\Composers;
4
5
use Illuminate\Support\Collection;
6
use Illuminate\Support\Str;
7
use Illuminate\View\View;
8
use MichaelRubel\SeoManager\Exceptions\ShouldImplementSeoTagInterfaceException;
9
use MichaelRubel\SeoManager\Models\SeoTag;
10
use MichaelRubel\SeoManager\Models\SeoTagContract;
11
12
class SeoComposer
13
{
14
    /**
15
     * @var Collection|null
16
     */
17
    protected ?Collection $seo_manager;
18
19
    /**
20
     * SeoComposer constructor.
21
     */
22 6
    public function __construct()
23
    {
24 6
        $this->seo_manager = $this->getSeoTags();
25 5
    }
26
27
    /**
28
     * Bind data to the view.
29
     *
30
     * @param  View  $view
31
     * @return void
32
     */
33 5
    public function compose(View $view): void
34
    {
35 5
        $variable = config('seo-manager.variable_name');
36
37 5
        $view->with(
38 5
            is_string($variable)
39 5
                ? $variable
40 5
                : 'seo_manager',
41 5
            $this->seo_manager
42
        );
43 5
    }
44
45
    /**
46
     * Prepare the SEO tags for the view.
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
        $wildcardUrls = $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 = $model::firstWhere($model->getUrlColumnName(), $url);
0 ignored issues
show
Bug introduced by
The method firstWhere() does not exist on Illuminate\Contracts\Foundation\Application. ( Ignorable by Annotation )

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

72
        /** @scrutinizer ignore-call */ 
73
        $instance = $model::firstWhere($model->getUrlColumnName(), $url);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
The method firstWhere() 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

72
        /** @scrutinizer ignore-call */ 
73
        $instance = $model::firstWhere($model->getUrlColumnName(), $url);
Loading history...
73
74 5
        if (is_null($instance)) {
75 2
            $instance = $model::whereIn($model->getUrlColumnName(), $wildcardUrls)
0 ignored issues
show
Bug introduced by
The method whereIn() does not exist on Illuminate\Contracts\Foundation\Application. ( Ignorable by Annotation )

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

75
            $instance = $model::/** @scrutinizer ignore-call */ whereIn($model->getUrlColumnName(), $wildcardUrls)

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
The method whereIn() 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

75
            $instance = $model::/** @scrutinizer ignore-call */ whereIn($model->getUrlColumnName(), $wildcardUrls)
Loading history...
76 2
                ->take($this->getMaxWildcardLevels())
77 2
                ->get()
78 2
                ->sortByDesc(
79 2
                    fn ($entry) => strlen(
80 2
                        $entry->{$model->getUrlColumnName()}
81
                    )
82 2
                )->first();
83
        }
84
85 5
        return $instance?->{$model->getTagsColumnName()};
86
    }
87
88
    /**
89
     * Make the possible wildcards for the given path.
90
     *
91
     * @param string $url
92
     *
93
     * @return array
94
     */
95 6
    protected function wildcard(string $url): array
96
    {
97 6
        $separated = explode('/', $url);
98
99 6
        return collect($separated)
100 6
            ->pipe(
101 6
                fn ($parts) => $parts->map(function ($value, $key) use ($parts) {
102 6
                    $wildcard = clone $parts->reject(
103 6
                        fn ($value, $rejection) => $key < $rejection
104 6
                    )->put($key, '*');
105
106 6
                    return $wildcard->implode('/');
107 6
                })
108 6
            )->reverse()->toArray();
109
    }
110
111
    /**
112
     * Gets the levels to limit the query.
113
     *
114
     * @return int
115
     */
116 2
    private function getMaxWildcardLevels(): int
117
    {
118 2
        $levels = config('seo-manager.max_wildcard_levels');
119
120 2
        return is_int($levels)
121 2
            ? $levels
122 2
            : 3;
123
    }
124
}
125