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

DynamicWidgetsBuilder   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 75
Duplicated Lines 22.67 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 4
Bugs 0 Features 2
Metric Value
c 4
b 0
f 2
dl 17
loc 75
rs 10
wmc 17
lcom 1
cbo 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
C handle() 17 50 16
A getWidgetsToDisplay() 0 9 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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