Completed
Push — master ( 7a1e6c...8794c3 )
by Arjay
11:28
created

DynamicWidgetsBuilder::getWidgetsToDisplay()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 9
cc 1
eloc 3
nc 1
nop 0
rs 9.6666
1
<?php
2
3
namespace Yajra\CMS\Http\Middleware;
4
5
use Closure;
6
use Illuminate\Database\QueryException;
7
use Illuminate\Support\Collection;
8
use Yajra\CMS\Entities\Widget;
9
10
class DynamicWidgetsBuilder
11
{
12
    /**
13
     * Handle an incoming request.
14
     *
15
     * @param  \Illuminate\Http\Request $request
16
     * @param  \Closure $next
17
     * @return mixed
18
     */
19
    public function handle($request, Closure $next)
20
    {
21
        // skip dynamic widgets on administrator pages.
22
        if ($request->is('administrator/*') || $request->is('administrator')) {
23
            return $next($request);
24
        }
25
26
        try {
27
            $factory = app('arrilot.widget-group-collection');
28
            $this->getWidgetsToDisplay()->each(function (Collection $group) use ($factory) {
29
                $group->each(function (Widget $widget) use ($factory) {
30
                    $displayWidget = true;
31
32
                    if ($widget->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...
33
                        $displayWidget = false;
34
                    }
35
36
                    if (count($widget->permissions)) {
37
                        if ($widget->authorization === 'can') {
38 View Code Duplication
                            foreach ($widget->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...
39
                                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...
40
                                    (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...
41
                                ) {
42
                                    $displayWidget = false;
43
                                    continue;
44
                                }
45
                            }
46 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...
47
                            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...
48
                                (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...
49
                                        ->user()
50
                                        ->canAtLeast($widget->permissions->pluck('slug')->toArray()))
51
                            ) {
52
                                $displayWidget = false;
53
                            }
54
                        }
55
                    }
56
57
                    if ($displayWidget) {
58
                        $factory->group($widget->position)
59
                                ->addWidget($widget->present()->class(), [], $widget);
60
                    }
61
                });
62
            });
63
        } catch (QueryException $e) {
64
            // \\_(",)_//
65
        }
66
67
        return $next($request);
68
    }
69
70
    /**
71
     * Get all published widgets.
72
     *
73
     * @return \Illuminate\Support\Collection
74
     */
75
    protected function getWidgetsToDisplay()
76
    {
77
        // Note:
78
        // This widget has menu_assignment global scope attached in the query.
79
        // @see DynamicMenusBuilder for the global scope.
80
        $widgets = Widget::with('permissions')->published()->get();
81
82
        return $widgets->groupBy('position')->sortBy('order');
83
    }
84
}
85