NavbarMenuViewComposer::compose()   B
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 32
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 32
rs 8.8571
c 1
b 0
f 0
cc 3
eloc 18
nc 4
nop 1
1
<?php
2
3
namespace App\Http\ViewComposers;
4
5
use Illuminate\View\View;
6
use App\Interfaces\AboutMeServiceInterface;
7
8
class NavbarMenuViewComposer
9
{
10
    public function __construct(AboutMeServiceInterface $aboutMeService)
11
    {
12
        $this->aboutMeService = $aboutMeService;
0 ignored issues
show
Bug introduced by
The property aboutMeService does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
13
    }
14
15
    public function compose(View $view)
16
    {
17
        $navbarMenu = [
18
            [
19
                'title' => 'Home Page',
20
                'url' => route('posts.index'),
21
                'active' => request()->routeIs('/') || request()->routeIs('*posts*'),
22
            ],
23
            [
24
                'title' => 'Categories',
25
                'url' => route('categories.index'),
26
                'active' => request()->routeIs('*categories*'),
27
            ],
28
            [
29
                'title' => 'Tags',
30
                'url' => route('tags.index'),
31
                'active' => request()->routeIs('*tags*'),
32
            ],
33
        ];
34
35
        if ($this->aboutMeService->hasContent()) {
36
            $navbarMenu[] = [
37
                'title' => 'About me',
38
                'url' => route('about-me.show'),
39
                'active' => request()->routeIs('about-me.show'),
40
            ];
41
        }
42
43
        $view->with('navbarMenu', collect($navbarMenu)->map(function ($menuElement) {
44
            return (object) $menuElement;
45
        }));
46
    }
47
}
48