CustomResourcesExport   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Test Coverage

Coverage 73.08%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 12
eloc 26
c 1
b 0
f 0
dl 0
loc 59
ccs 19
cts 26
cp 0.7308
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A findByKey() 0 15 4
A use() 0 21 5
A options() 0 12 3
1
<?php
2
3
namespace NovaResourceDynamicExport;
4
5
use NovaResourceDynamicExport\Export\CustomExport;
6
7
class CustomResourcesExport
8
{
9
    /**
10
     * @var array<CustomResourcesExport>
11
     */
12
    public static array $exports = [];
13
14 10
    public static function use(string|CustomExport|array $export): static
15
    {
16 10
        if (is_array($export)) {
0 ignored issues
show
introduced by
The condition is_array($export) is always true.
Loading history...
17
            foreach ($export as $exportItem) {
18
                static::use($exportItem);
19
            }
20
21
            return new static;
22
        }
23
24 10
        if (!is_string($export)) {
25
            $export = $export::class;
26
        }
27
28 10
        if (!is_subclass_of($export, CustomExport::class)) {
29
            throw new \Exception('Custom export should be subclass of ' . CustomExport::class);
30
        }
31
32 10
        static::$exports[] = $export;
33
34 10
        return new static;
35
    }
36
37 3
    public static function options(): array
38
    {
39 3
        $exportsList   = [];
40 3
        $exports       = static::$exports;
41 3
        if(!empty($exports)) {
42
            /** @var CustomExport $export */
43 3
            foreach ($exports as $export) {
44 3
                $exportsList[$export::key()] = $export::name();
45
            }
46
        }
47
48 3
        return $exportsList;
49
    }
50
51 1
    public static function findByKey(string $key): ?CustomExport
52
    {
53 1
        $customExports = static::$exports;
54 1
        if(!empty($customExports)) {
55
            /** @var CustomExport $export */
56 1
            foreach ($customExports as $export) {
57 1
                if($export::key() != $key) {
58
                    continue;
59
                }
60
61 1
                return new $export;
62
            }
63
        }
64
65
        return null;
66
    }
67
}
68