LinkType::existsByTypename()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
namespace App\Models;
3
4
use Illuminate\Database\Eloquent\Model;
5
use Symfony\Component\Yaml\Yaml;
6
use Illuminate\Support\Collection;
7
use Illuminate\Filesystem\Filesystem;
8
9
class LinkType extends Model
10
{
11
    protected $fillable = ['id', 'typename', 'title', 'description', 'icon', 'custom_html', 'ignore_container', 'include_libraries'];
12
13
    // Assuming no database interaction, we can disable timestamps
14
    public $timestamps = false;
15
16
    /**
17
     * Get all LinkTypes from the config.yml files in each subfolder of the blocks directory.
18
     *
19
     * @return Collection
20
     */
21
    public static function get()
22
    {
23
        $blocksPath = base_path('blocks/');
24
        $directories = (new Filesystem)->directories($blocksPath);
25
        $linkTypes = collect();
26
    
27
    // Prepend "predefined" entry to the $linkTypes list
28
    $predefinedLinkType = new self([
29
        'id' => 1,
30
        'typename' => 'predefined',
31
        'title' => null,
32
        'description' => null,
33
        'icon' => 'bi bi-boxes',
34
        'custom_html' => false,
35
        'ignore_container' => false,
36
        'include_libraries' => [],
37
    ]);
38
39
    $linkTypes->prepend($predefinedLinkType);
40
41
    foreach ($directories as $dir) {
42
        $configPath = $dir . '/config.yml';
43
        if (file_exists($configPath)) {
44
            $configData = Yaml::parse(file_get_contents($configPath));
45
46
            // Create a new instance of LinkType for each config file
47
            $linkType = new self([
48
                'id' => $configData['id'] ?? 0,
49
                'typename' => $configData['typename'] ?? null,
50
                'title' => $configData['title'] ?? null,
51
                'description' => $configData['description'] ?? null,
52
                'icon' => $configData['icon'] ?? null,
53
                'custom_html' => $configData['custom_html'] ?? false,
54
                'ignore_container' => $configData['ignore_container'] ?? false,
55
                'include_libraries' => $configData['include_libraries'] ?? [],
56
            ]);
57
            $linkTypes->push($linkType);
58
        }
59
    }
60
    
61
        $custom_order = [
62
            'predefined',
63
            'link',
64
            'vcard',
65
            'email',
66
            'telephone',
67
            'heading',
68
            'spacer',
69
            'text',
70
        ];
71
    
72
        $sorted = $linkTypes->sortBy(function ($item) use ($custom_order) {
73
            $index = array_search($item->typename, $custom_order);
74
            return $index !== false ? $index : count($custom_order);
75
        });
76
    
77
        return $sorted->values();
78
    }
79
80
    /**
81
     * Check if a LinkType with the given typename exists.
82
     *
83
     * @param string $typename
84
     * @return bool
85
     */
86
    public static function existsByTypename($typename)
87
    {
88
        return self::get()->contains('typename', $typename);
89
    }
90
91
    /**
92
     * Find a LinkType by its typename.
93
     *
94
     * @param string $typename
95
     * @return LinkType|null
96
     */
97
    public static function findByTypename($typename)
98
    {
99
        return self::get()->firstWhere('typename', $typename);
100
    }
101
}
102