Passed
Push — main ( ac25fd...00e2bd )
by Yaroslav
14:14
created

NovaExportConfig::customExportsByKey()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 7
nc 3
nop 1
dl 0
loc 15
ccs 0
cts 8
cp 0
crap 20
rs 10
c 1
b 0
f 0
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
     * Additional actions
31
     */
32
    public static Closure|array|null $configurationActionsCallback = null;
33
34
    /**
35
     * @var array
36
     */
37
    public static array $customExports = [];
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
117
118
    public static function useCustomExportsToFile(string|CustomExport|array $customExport): static
119
    {
120
        if (is_array($customExport)) {
0 ignored issues
show
introduced by
The condition is_array($customExport) is always true.
Loading history...
121
            foreach ($customExport as $customExportItem) {
122
                static::useCustomExportsToFile($customExportItem);
123
            }
124
125
            return new static;
126
        }
127
128
        if (!is_string($customExport)) {
129
            $customExport = $customExport::class;
130
        }
131
132
        if (!is_subclass_of($customExport, CustomExport::class)) {
133
            throw new \Exception('Custom export should be subclass of ' . CustomExport::class);
134
        }
135
136
        static::$customExports[] = $customExport;
137
138
        return new static;
139
    }
140
141
    public static function customExportsOptions(): array
142
    {
143
        $exportsList   = [];
144
        $customExports = NovaExportConfig::$customExports;
145
        if(!empty($customExports)) {
146
            /** @var CustomExport $customExport */
147
            foreach ($customExports as $customExport) {
148
                $exportsList[$customExport::key()] = $customExport::name();
149
            }
150
        }
151
152
        return $exportsList;
153
    }
154
155
    public static function customExportsByKey(string $key): ?CustomExport
156
    {
157
        $customExports = NovaExportConfig::$customExports;
158
        if(!empty($customExports)) {
159
            /** @var CustomExport $customExport */
160
            foreach ($customExports as $customExport) {
161
                if($customExport::key() != $key) {
162
                    continue;
163
                }
164
165
                return new $customExport;
166
            }
167
        }
168
169
        return null;
170
    }
171
}
172