Passed
Push — main ( 302427...b5b644 )
by Yaroslav
14:44
created

NovaExportConfig::withoutRoutes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 2
nc 1
nop 0
dl 0
loc 5
c 0
b 0
f 0
cc 1
ccs 0
cts 3
cp 0
crap 2
rs 10
1
<?php
2
3
namespace NovaExportConfiguration;
4
5
use Closure;
6
use Laravel\Nova\Http\Requests\NovaRequest;
7
use NovaExportConfiguration\Export\CustomExport;
8
use NovaExportConfiguration\Models\ExportConfig;
9
use NovaExportConfiguration\Repositories\ExportRepository;
10
use NovaExportConfiguration\Repositories\ExportRepositoryCollection;
11
12
class NovaExportConfig
13
{
14
    /**
15
     * Export repositories used by application
16
     */
17
    protected static ?ExportRepositoryCollection $repositories = null;
18
19
    /**
20
     * Export configuration model class
21
     */
22
    public static string $configurationModelClass = \NovaExportConfiguration\Models\ExportConfig::class;
23
24
    /**
25
     * Indicates if NovaExportConfiguration migrations will be run.
26
     */
27
    public static bool $runsMigrations = true;
28
29
    /**
30
     * Indicates if NovaExportConfiguration will provide download route.
31
     */
32
    public static bool $useRoutes = true;
33
34
    /**
35
     * Route Configuration callback
36
     */
37
    public static Closure|null $routeConfigurationCallback = null;
38
39
40
    /**
41
     * Additional actions
42
     */
43
    public static Closure|array|null $configurationActionsCallback = null;
44
45
    /**
46
     * @var array
47
     */
48
    public static array $customExports = [];
49
50
51
    public static function ignoreMigrations(): static
52
    {
53
        static::$runsMigrations = false;
54
55
        return new static;
56
    }
57
58
    public static function withoutRoutes(): static
59
    {
60
        static::$useRoutes = false;
61
62
        return new static;
63
    }
64
65
    public static function routeConfiguration(?\Closure $callback = null): mixed
66
    {
67
        if($callback) {
68
            static::$routeConfigurationCallback = $callback;
69
70
            return new static;
71
        }
72
73
        if(is_callable(static::$routeConfigurationCallback)) {
74
            return call_user_func(static::$routeConfigurationCallback);
0 ignored issues
show
Bug introduced by
It seems like static::routeConfigurationCallback can also be of type null; however, parameter $callback of call_user_func() does only seem to accept callable, maybe add an additional type check? ( Ignorable by Annotation )

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

74
            return call_user_func(/** @scrutinizer ignore-type */ static::$routeConfigurationCallback);
Loading history...
75
        }
76
77
        return [
78
            'prefix'     => 'downloads/exports',
79
            'middleware' => config('nova.middleware'),
80
        ];
81
    }
82
83
    public static function useConfigurationModelClass(string $class): static
84
    {
85
        if (!is_a($class, ExportConfig::class, true)) {
86
            throw new \InvalidArgumentException('Class should extend ExportConfig');
87
        }
88
89
        static::$configurationModelClass = $class;
90
91
        return new static;
92
    }
93
94
    public static function useRepository(string|ExportRepository|array $repository): static
95
    {
96
        if (is_null(static::$repositories)) {
97
            static::$repositories = new ExportRepositoryCollection();
98
        }
99
100
        if (is_array($repository)) {
0 ignored issues
show
introduced by
The condition is_array($repository) is always true.
Loading history...
101
            foreach ($repository as $repo) {
102
                static::useRepository($repo);
103
            }
104
105
            return new static;
106
        }
107
108
        if (is_string($repository)) {
109
            $repository = new $repository();
110
        }
111
112
        if ($repository instanceof ExportRepository) {
113
            static::$repositories->add($repository);
114
            static::$repositories = static::$repositories->uniqueNames();
115
        }
116
117
        return new static;
118
    }
119
120
    public static function getRepositories(): ExportRepositoryCollection
121
    {
122
        if (is_null(static::$repositories)) {
123
            static::$repositories = new ExportRepositoryCollection();
124
        }
125
126
        return self::$repositories;
127
    }
128
129
    public static function useConfigurationActions(Closure|array $configurationActionsCallback): static
130
    {
131
        self::$configurationActionsCallback = $configurationActionsCallback;
132
133
        return new static;
134
    }
135
136
    public static function configurationActions(NovaRequest $request): array
137
    {
138
        if (is_callable(static::$configurationActionsCallback)) {
139
            return call_user_func(static::$configurationActionsCallback, $request);
0 ignored issues
show
Bug introduced by
It seems like static::configurationActionsCallback can also be of type null; however, parameter $callback of call_user_func() does only seem to accept callable, maybe add an additional type check? ( Ignorable by Annotation )

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

139
            return call_user_func(/** @scrutinizer ignore-type */ static::$configurationActionsCallback, $request);
Loading history...
140
        }
141
142
        return [];
143
    }
144
145
    public static function typeOptions(): array
146
    {
147
        return static::getRepositories()
148
                        ->mapWithKeys(fn ($repo) => [$repo->name() => $repo->label()])
149
                        ->toArray();
150
    }
151
152
    public static function useCustomExportsToFile(string|CustomExport|array $customExport): static
153
    {
154
        if (is_array($customExport)) {
0 ignored issues
show
introduced by
The condition is_array($customExport) is always true.
Loading history...
155
            foreach ($customExport as $customExportItem) {
156
                static::useCustomExportsToFile($customExportItem);
157
            }
158
159
            return new static;
160
        }
161
162
        if (!is_string($customExport)) {
163
            $customExport = $customExport::class;
164
        }
165
166
        if (!is_subclass_of($customExport, CustomExport::class)) {
167
            throw new \Exception('Custom export should be subclass of ' . CustomExport::class);
168
        }
169
170
        static::$customExports[] = $customExport;
171
172
        return new static;
173
    }
174
175
    public static function customExportsOptions(): array
176
    {
177
        $exportsList   = [];
178
        $customExports = NovaExportConfig::$customExports;
179
        if(!empty($customExports)) {
180
            /** @var CustomExport $customExport */
181
            foreach ($customExports as $customExport) {
182
                $exportsList[$customExport::key()] = $customExport::name();
183
            }
184
        }
185
186
        return $exportsList;
187
    }
188
189
    public static function customExportsByKey(string $key): ?CustomExport
190
    {
191
        $customExports = NovaExportConfig::$customExports;
192
        if(!empty($customExports)) {
193
            /** @var CustomExport $customExport */
194
            foreach ($customExports as $customExport) {
195
                if($customExport::key() != $key) {
196
                    continue;
197
                }
198
199
                return new $customExport;
200
            }
201
        }
202
203
        return null;
204
    }
205
}
206