DynamicWidgetsBuilder::handle()   C
last analyzed

Complexity

Conditions 15
Paths 4

Size

Total Lines 50
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 15
eloc 28
nc 4
nop 2
dl 0
loc 50
rs 5.3624
c 0
b 0
f 0

How to fix   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(admin_prefix() . '*')) {
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 only exist in Illuminate\Contracts\Auth\Guard, but not in Illuminate\Contracts\Auth\Factory.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
33
                        $displayWidget = false;
34
                    }
35
36
                    if (count($widget->permissions)) {
37
                        if ($widget->authorization === 'can') {
38
                            foreach ($widget->permissions as $permission) {
39
                                if (auth()->guest() ||
0 ignored issues
show
Bug introduced by
The method guest does only exist in Illuminate\Contracts\Auth\Guard, but not in Illuminate\Contracts\Auth\Factory.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
40
                                    (auth()->check() && ! auth()->user()->can($permission->slug))
0 ignored issues
show
Bug introduced by
The method user does only exist in Illuminate\Contracts\Auth\Guard, but not in Illuminate\Contracts\Auth\Factory.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
41
                                ) {
42
                                    $displayWidget = false;
43
                                    continue;
44
                                }
45
                            }
46
                        } else {
47
                            if (auth()->guest() ||
48
                                (auth()->check() && ! auth()
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
        $widgets = Widget::with('permissions')->published()->orderBy('order', 'asc')->get();
80
81
        return $widgets->groupBy('position');
82
    }
83
}
84