Completed
Push — master ( aecbb4...773e85 )
by Arjay
18:51 queued 03:49
created

DynamicMenusBuilder::setWidgetGlobalScope()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 1
eloc 7
nc 1
nop 1
dl 0
loc 10
rs 9.4285
c 1
b 1
f 0
1
<?php
2
3
namespace Yajra\CMS\Http\Middleware;
4
5
use Caffeinated\Menus\Builder;
6
use Caffeinated\Menus\Facades\Menu as MenuFactory;
7
use Closure;
8
use Yajra\CMS\Entities\Menu;
9
use Yajra\CMS\Entities\Navigation;
10
use Yajra\CMS\Entities\Widget;
11
use Yajra\CMS\Repositories\Navigation\Repository;
12
13
class DynamicMenusBuilder
14
{
15
    /**
16
     * @var Repository
17
     */
18
    protected $repository;
19
20
    /**
21
     * Handle an incoming request.
22
     *
23
     * @param  \Illuminate\Http\Request $request
24
     * @param  \Closure $next
25
     * @return mixed
26
     */
27
    public function handle($request, Closure $next)
28
    {
29
        session()->forget('active_menu');
30
31
        if (! $request->is('administrator*')) {
32
            $this->repository = app(Repository::class);
33
            $this->repository->getPublished()->each(function (Navigation $navigation) {
34
                MenuFactory::make($navigation->type, function (Builder $builder) use ($navigation) {
35
                    $navigation->menus->each(function (Menu $menu) use ($builder, &$assignment) {
36
                        $menus = $menu->descendantsAndSelf()->with('permissions')->get()->toHierarchy();
37
                        foreach ($menus as $menu) {
38
                            $this->generateMenu($builder, $menu);
39
                        }
40
                    });
41
                });
42
            });
43
        }
44
45
        return $next($request);
46
    }
47
48
    /**
49
     * Generate the menu.
50
     *
51
     * @param \Caffeinated\Menus\Builder|\Caffeinated\Menus\Item $menuBuilder
52
     * @param \Yajra\CMS\Entities\Menu $menu
53
     */
54
    protected function generateMenu($menuBuilder, $menu)
55
    {
56
        $subMenu = $this->registerMenu($menuBuilder, $menu);
57
        $menu->children->each(function (Menu $subItem) use ($subMenu) {
58
            $subMenuChild = $this->registerMenu($subMenu, $subItem);
59
            if (count($subItem->children)) {
60
                $this->generateMenu($subMenuChild, $subItem);
0 ignored issues
show
Bug introduced by
It seems like $subMenuChild defined by $this->registerMenu($subMenu, $subItem) on line 58 can also be of type boolean; however, Yajra\CMS\Http\Middlewar...Builder::generateMenu() does only seem to accept object<Caffeinated\Menus...Caffeinated\Menus\Item>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
61
            }
62
        });
63
    }
64
65
    /**
66
     * Register a menu.
67
     *
68
     * @param \Caffeinated\Menus\Builder|\Caffeinated\Menus\Item $menuBuilder
69
     * @param \Yajra\CMS\Entities\Menu $menu
70
     * @return \Caffeinated\Menus\Builder|bool
71
     * @throws \Laracasts\Presenter\Exceptions\PresenterException
72
     */
73
    protected function registerMenu($menuBuilder, Menu $menu)
74
    {
75
        if (! $menu->published) {
76
            return false;
77
        }
78
79
        if ($menu->requiresAuthentication() && ! auth()->check()) {
0 ignored issues
show
Bug introduced by
The method check() does not seem to exist on object<Illuminate\Contracts\Auth\Factory>.

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...
80
            return false;
81
        }
82
83
        if (count($menu->permissions)) {
84
            if ($menu->authorization === 'can') {
85 View Code Duplication
                foreach ($menu->permissions as $permission) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
86
                    if (auth()->guest() ||
0 ignored issues
show
Bug introduced by
The method guest() does not seem to exist on object<Illuminate\Contracts\Auth\Factory>.

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...
87
                        (auth()->check() && ! auth()->user()->can($permission->slug))
0 ignored issues
show
Bug introduced by
The method check() does not seem to exist on object<Illuminate\Contracts\Auth\Factory>.

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 user() does not seem to exist on object<Illuminate\Contracts\Auth\Factory>.

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...
88
                    ) {
89
                        return false;
90
                    }
91
                }
92 View Code Duplication
            } else {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
93
                if (auth()->guest() ||
0 ignored issues
show
Bug introduced by
The method guest() does not seem to exist on object<Illuminate\Contracts\Auth\Factory>.

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...
94
                    (auth()->check() && ! auth()
0 ignored issues
show
Bug introduced by
The method check() does not seem to exist on object<Illuminate\Contracts\Auth\Factory>.

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 user() does not seem to exist on object<Illuminate\Contracts\Auth\Factory>.

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...
95
                            ->user()
96
                            ->canAtLeast($menu->permissions->pluck('slug')->toArray()))
97
                ) {
98
                    return false;
99
                }
100
            }
101
        }
102
103
        $item = $menuBuilder->add($menu->title, $menu->present()->url)
104
                            ->attribute('target', $menu->present()->target)
105
                            ->attribute('title', $menu->present()->linkTitle);
106
107
        if ($menu->present()->linkStyle) {
108
            $item->attribute('style', $menu->present()->linkStyle);
109
        }
110
111
        if ($menu->isActive()) {
112
            session()->flash('active_menu', $menu);
113
        }
114
115
        return $item;
116
    }
117
}
118