MetaboxComposer::__construct()   A
last analyzed

Complexity

Conditions 5
Paths 8

Size

Total Lines 14
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 11
c 1
b 0
f 0
nc 8
nop 0
dl 0
loc 14
rs 9.6111
1
<?php
2
3
namespace Giuga\LaravelSeoMetaBox\Http\View;
4
5
use Giuga\LaravelSeoMetaBox\Models\Seo;
6
use Illuminate\View\View;
7
8
class MetaboxComposer
9
{
10
    private ?Seo $seo = null;
11
    private string $requestUrl;
12
13
    public function __construct()
14
    {
15
        $this->requestUrl = request()->getRequestUri();
16
        $query = request()->getQueryString();
17
        if ($query !== null) {
18
            $query = '?'.$query;
19
        }
20
        $this->seo = Seo::where('slug', $this->requestUrl)->first();
21
        if (! $this->seo) {
22
            $this->seo = Seo::where('slug', str_replace($query, '', $this->requestUrl))->first();
23
            if (! $this->seo) {
24
                $metaBox = app()->make('laravel-seo-meta-box')->getObjectOnPage();
25
                if ($metaBox) {
26
                    $this->seo = Seo::where('type', $metaBox['type'])->where('object_id', $metaBox['id'])->first();
27
                }
28
            }
29
        }
30
    }
31
32
    public function compose(View $view)
33
    {
34
        $title = config('app.name');
35
        if ($this->seo) {
36
            if ($this->seo->title) {
37
                $title = $this->seo->title;
38
                if (config('meta-box.use_app_name')) {
39
                    $title .= config('meta-box.use_app_name_separator').config('app.name');
40
                }
41
            }
42
        }
43
44
        $view->with('seoUseTwitter', config('meta-box.use_twitter'));
45
        $view->with('seoTwitterHandle', config('meta-box.twitter_handle'));
46
        $view->with('seoTitle', $title);
47
        $view->with('seoDescription', $this->seo ? $this->seo->description : '');
48
        $view->with('seoUseOpenGraph', config('meta-box.use_open_graph'));
49
        $view->with('seoFullUrl', url($this->requestUrl));
50
    }
51
}
52