NovaExportConfig::configurationActions()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 7
ccs 0
cts 4
cp 0
crap 6
rs 10
1
<?php
2
3
namespace NovaExportConfiguration;
4
5
use Closure;
6
use Laravel\Nova\Http\Requests\NovaRequest;
7
use NovaExportConfiguration\Models\ExportConfig;
8
use NovaExportConfiguration\Repositories\ExportRepository;
9
use NovaExportConfiguration\Repositories\ExportRepositoryCollection;
10
11
class NovaExportConfig
12
{
13
    /**
14
     * Export repositories used by application
15
     */
16
    protected static ?ExportRepositoryCollection $repositories = null;
17
18
    /**
19
     * Export configuration model class
20
     */
21
    public static string $configurationModelClass = \NovaExportConfiguration\Models\ExportConfig::class;
22
23
    /**
24
     * Indicates if NovaExportConfiguration migrations will be run.
25
     */
26
    public static bool $runsMigrations = true;
27
28
    /**
29
     * Route Configuration callback
30
     */
31
    public static Closure|null $routeConfigurationCallback = null;
32
33
34
    /**
35
     * Additional actions
36
     */
37
    public static Closure|array|null $configurationActionsCallback = null;
38
39
40
    public static function ignoreMigrations(): static
41
    {
42
        static::$runsMigrations = false;
43
44
        return new static;
45
    }
46
47
    public static function useConfigurationModelClass(string $class): static
48
    {
49
        if (!is_a($class, ExportConfig::class, true)) {
50
            throw new \InvalidArgumentException('Class should extend ExportConfig');
51
        }
52
53
        static::$configurationModelClass = $class;
54
55
        return new static;
56
    }
57
58
    public static function useRepository(string|ExportRepository|array $repository): static
59
    {
60
        if (is_null(static::$repositories)) {
61
            static::$repositories = new ExportRepositoryCollection();
62
        }
63
64
        if (is_array($repository)) {
0 ignored issues
show
introduced by
The condition is_array($repository) is always true.
Loading history...
65
            foreach ($repository as $repo) {
66
                static::useRepository($repo);
67
            }
68
69
            return new static;
70
        }
71
72
        if (is_string($repository)) {
73
            $repository = new $repository();
74
        }
75
76
        if ($repository instanceof ExportRepository) {
77
            static::$repositories->add($repository);
78
            static::$repositories = static::$repositories->uniqueNames();
79
        }
80
81
        return new static;
82
    }
83
84
    public static function getRepositories(): ExportRepositoryCollection
85
    {
86
        if (is_null(static::$repositories)) {
87
            static::$repositories = new ExportRepositoryCollection();
88
        }
89
90
        return self::$repositories;
91
    }
92
93
    public static function useConfigurationActions(Closure|array $configurationActionsCallback): static
94
    {
95
        self::$configurationActionsCallback = $configurationActionsCallback;
96
97
        return new static;
98
    }
99
100
    public static function configurationActions(NovaRequest $request): array
101
    {
102
        if (is_callable(static::$configurationActionsCallback)) {
103
            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

103
            return call_user_func(/** @scrutinizer ignore-type */ static::$configurationActionsCallback, $request);
Loading history...
104
        }
105
106
        return [];
107
    }
108
109
    public static function typeOptions(): array
110
    {
111
        return static::getRepositories()
112
                        ->mapWithKeys(fn ($repo) => [$repo->name() => $repo->label()])
113
                        ->toArray();
114
    }
115
}
116