Completed
Push — master ( 3768ac...6b4e3f )
by Gabriel
03:57
created

MetaboxComposer::compose()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 12
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 18
rs 9.5555
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
        $this->seo = Seo::where('slug', $this->requestUrl)->first();
17
        if (! $this->seo) {
18
            $metaBox = app()->make('laravel-seo-meta-box')->getObjectOnPage();
19
            if ($metaBox) {
20
                $this->seo = Seo::where('type', $metaBox['type'])->where('object_id', $metaBox['id'])->first();
21
            }
22
        }
23
    }
24
25
    public function compose(View $view)
26
    {
27
        $title = config('app.name');
28
        if ($this->seo) {
29
            if ($this->seo->title) {
30
                $title = $this->seo->title;
31
                if (config('meta-box.use_app_name')) {
32
                    $title .= config('meta-box.use_app_name_separator').config('app.name');
33
                }
34
            }
35
        }
36
37
        $view->with('seoUseTwitter', config('meta-box.use_twitter'));
38
        $view->with('seoTwitterHandle', config('meta-box.twitter_handle'));
39
        $view->with('seoTitle', $title);
40
        $view->with('seoDescription', $this->seo ? $this->seo->description : '');
41
        $view->with('seoUseOpenGraph', config('meta-box.use_open_graph'));
42
        $view->with('seoFullUrl', url($this->requestUrl));
43
    }
44
}
45