Completed
Push — master ( e5ba92...7a1e6c )
by Arjay
13:54
created

DynamicWidgetsBuilder::handle()   C

Complexity

Conditions 16
Paths 5

Size

Total Lines 53
Code Lines 29

Duplication

Lines 17
Ratio 32.08 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 16
eloc 29
c 2
b 0
f 1
nc 5
nop 2
dl 17
loc 53
rs 6.2752

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
29
            /** @var Collection $widgets */
30
            $widgets = $this->getWidgets();
31
            $widgets->each(function (Collection $group) use ($factory) {
32
                $group->each(function (Widget $widget) use ($factory) {
33
                    $displayWidget = true;
34
35
                    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...
36
                        $displayWidget = false;
37
                    }
38
39
                    if ($widget->permissions->count()) {
40
                        if ($widget->authorization === 'can') {
41 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...
42
                                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...
43
                                    (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...
44
                                ) {
45
                                    $displayWidget = false;
46
                                    continue;
47
                                }
48
                            }
49 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...
50
                            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...
51
                                (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...
52
                                        ->user()
53
                                        ->canAtLeast($widget->permissions->pluck('slug')->toArray()))
54
                            ) {
55
                                $displayWidget = false;
56
                            }
57
                        }
58
                    }
59
60
                    if ($displayWidget) {
61
                        $factory->group($widget->position)
62
                                ->addWidget($widget->present()->class(), [], $widget);
63
                    }
64
                });
65
            });
66
        } catch (QueryException $e) {
67
            // \\_(",)_//
68
        }
69
70
        return $next($request);
71
    }
72
73
    /**
74
     * Get all published widgets.
75
     *
76
     * @return \Illuminate\Support\Collection
77
     */
78
    protected function getWidgets()
79
    {
80
        $widgets = Widget::with('permissions')->published()->get();
81
82
        return $widgets->groupBy('position')->sortBy('order');
83
    }
84
}
85