DynamicWidgetsBuilder   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 74
rs 10
c 0
b 0
f 0
wmc 16
lcom 1
cbo 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
C handle() 0 50 15
A getWidgetsToDisplay() 0 8 1
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