DashboardNova::allAvailableDashboardCards()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 12
rs 9.9332
cc 1
nc 1
nop 1
1
<?php
2
3
namespace AlexBowers\MultipleDashboard;
4
5
use Laravel\Nova\Nova;
0 ignored issues
show
Bug introduced by
The type Laravel\Nova\Nova was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use Illuminate\Http\Request;
0 ignored issues
show
Bug introduced by
The type Illuminate\Http\Request was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use Laravel\Nova\Http\Requests\NovaRequest;
0 ignored issues
show
Bug introduced by
The type Laravel\Nova\Http\Requests\NovaRequest was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use Illuminate\Support\Str;
0 ignored issues
show
Bug introduced by
The type Illuminate\Support\Str was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use Symfony\Component\Finder\Finder;
10
11
class DashboardNova extends Nova
12
{
13
    /**
14
     * The registered dashboard names.
15
     */
16
    public static $dashboards = [];
17
18
    /**
19
     * The registered cards for the default dashboard.
20
     */
21
    public static $default_dashboard_cards = [];
22
23
    /**
24
     * Copy the default dashboard cards so we can
25
     * return which cards are in use on the
26
     * main dashboard.
27
     */
28
    public static function copyDefaultDashboardCards()
29
    {
30
        static::$default_dashboard_cards = static::$cards;
31
32
        return new static;
33
    }
34
35
    /**
36
     * Get the dashboards registered with Nova.
37
     *
38
     * @param \Illuminate\Http\Request $request
39
     * @return mixed
40
     */
41
    public static function availableDashboards(Request $request)
42
    {
43
        return collect(static::$dashboards)
0 ignored issues
show
Bug introduced by
The function collect was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

43
        return /** @scrutinizer ignore-call */ collect(static::$dashboards)
Loading history...
44
            ->filter
45
            ->authorize($request)
46
            ->all();
47
    }
48
49
    /**
50
     * Register one or more dashboards
51
     *
52
     * @param Dashboard ...$dashboards
53
     */
54
    public static function registerDashboards(Dashboard ...$dashboards)
55
    {
56
        static::dashboards($dashboards);
57
    }
58
59
    /**
60
     * Register the dashboards from a specific directory
61
     */
62
    public static function dashboardsIn($directory)
63
    {
64
        $namespace = app()->getNamespace();
0 ignored issues
show
Bug introduced by
The function app was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

64
        $namespace = /** @scrutinizer ignore-call */ app()->getNamespace();
Loading history...
65
66
        $dashboards = [];
67
68
        foreach ((new Finder)->in($directory)->files() as $dashboard) {
69
            $dashboard = $namespace.str_replace(
70
                ['/', '.php'],
71
                ['\\', ''],
72
                Str::after($dashboard->getPathname(), app_path().DIRECTORY_SEPARATOR)
0 ignored issues
show
Bug introduced by
The function app_path was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

72
                Str::after($dashboard->getPathname(), /** @scrutinizer ignore-call */ app_path().DIRECTORY_SEPARATOR)
Loading history...
73
            );
74
75
            if (is_subclass_of($dashboard, Dashboard::class) && !(new \ReflectionClass($dashboard))->isAbstract()) {
76
                $dashboards[] = $dashboard;
77
            }
78
        }
79
80
        static::dashboards(
81
            collect($dashboards)->transform(function ($dashboard) {
0 ignored issues
show
Bug introduced by
The function collect was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

81
            /** @scrutinizer ignore-call */ 
82
            collect($dashboards)->transform(function ($dashboard) {
Loading history...
82
                if ($dashboard instanceof Dashboard) {
83
                    return $dashboard;
84
                }
85
86
                return app()->make($dashboard);
0 ignored issues
show
Bug introduced by
The function app was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

86
                return /** @scrutinizer ignore-call */ app()->make($dashboard);
Loading history...
87
            })->sortBy('label')->sortBy('order')->all()
88
        );
89
    }
90
91
    /**
92
     * Load the cards in use in each dashboard
93
     */
94
    public static function cardsInDashboards()
95
    {
96
        $dashboards = static::$dashboards;
97
98
        foreach ($dashboards as $dashboard) {
99
            DashboardNova::cards($dashboard->cards());
100
        }
101
    }
102
103
    /**
104
     * Register the given dashboards.
105
     *
106
     * @param  array  $dashboards
107
     * @return static
108
     */
109
    public static function dashboards(array $dashboards)
110
    {
111
        static::$dashboards = array_merge(static::$dashboards, $dashboards);
112
113
        return new static;
114
    }
115
116
    /**
117
     * Get the available dashboard cards for the given request.
118
     *
119
     * @param  \Laravel\Nova\Http\Requests\NovaRequest  $request
120
     * @return \Illuminate\Support\Collection
0 ignored issues
show
Bug introduced by
The type Illuminate\Support\Collection was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
121
     */
122
    public static function allAvailableDashboardCards(NovaRequest $request)
123
    {
124
        return collect(static::$dashboards)
0 ignored issues
show
Bug introduced by
The function collect was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

124
        return /** @scrutinizer ignore-call */ collect(static::$dashboards)
Loading history...
125
            ->filter
126
            ->authorize($request)
127
            ->flatMap(function ($dashboard) {
128
                return $dashboard->cards();
129
            })->merge(static::$cards)
130
            ->unique()
131
            ->filter
132
            ->authorize($request)
133
            ->values();
134
    }
135
136
    /**
137
     * Get the available dashboard cards for the given request.
138
     *
139
     * @param string $dashboard
140
     * @param  \Laravel\Nova\Http\Requests\NovaRequest  $request
141
     * @return \Illuminate\Support\Collection
142
     */
143
    public static function availableDashboardCardsForDashboard($dashboard, NovaRequest $request)
144
    {
145
        return collect(static::$dashboards)->filter->authorize($request)->filter(function ($board) use ($dashboard) {
0 ignored issues
show
Bug introduced by
The function collect was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

145
        return /** @scrutinizer ignore-call */ collect(static::$dashboards)->filter->authorize($request)->filter(function ($board) use ($dashboard) {
Loading history...
146
            return $board->uriKey() === $dashboard;
147
        })->flatMap(function ($dashboard) {
148
            return $dashboard->cards();
149
        })->filter->authorize($request)->values();
150
    }
151
}
152