Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
44 / 44
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
BaseConfigController
100.00% covered (success)
100.00%
44 / 44
100.00% covered (success)
100.00%
3 / 3
21
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 getPageData
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
 parseOptions
100.00% covered (success)
100.00%
33 / 33
100.00% covered (success)
100.00%
1 / 1
19
1<?php
2
3declare(strict_types=1);
4
5namespace Engelsystem\Controllers\Admin;
6
7use Engelsystem\Controllers\BaseController;
8use Engelsystem\Controllers\HasUserNotifications;
9use Illuminate\Support\Str;
10
11abstract class BaseConfigController extends BaseController
12{
13    use HasUserNotifications;
14
15    protected array $options = [];
16
17    protected array $permissions = [
18        'config.edit',
19    ];
20
21    public function __construct()
22    {
23        $this->options = config('config_options', []);
24        // Sort by order ascending, ignore missing
25        uasort(
26            $this->options,
27            fn($a, $b) => ($a['order'] ?? $b['order'] ?? 0) <=> ($b['order'] ?? $a['order'] ?? 0)
28        );
29    }
30
31    protected function getPageData(string $page): array
32    {
33        return [
34            'page' => $page,
35            'title' => $this->options[$page]['title'],
36            'config' => $this->options[$page]['config'],
37            'options' => $this->options,
38        ];
39    }
40
41    protected function parseOptions(bool $localConfigWritable = false, bool $withAll = false): void
42    {
43        $fromEnv = array_filter(config('env_config', []), fn($a) => !is_null($a));
44
45        foreach ($this->options as $key => $value) {
46            // Add page URL
47            if (empty($this->options[$key]['url'])) {
48                $this->options[$key]['url'] = url('/admin/config/' . $key);
49                $this->options[$key]['url_added'] = true;
50            }
51
52            // Configure page translation names
53            if (empty($this->options[$key]['title'])) {
54                $this->options[$key]['title'] = 'config.' . $key;
55            }
56
57            // Define internal validation action
58            $internalValidation = 'validate' . Str::ucfirst($key);
59            if (method_exists($this, $internalValidation)) {
60                // Used until proper dynamic config loading is implemented
61                $this->options[$key]['validation'] = [$this, $internalValidation];
62            }
63
64            // Iterate over settings
65            foreach ($this->options[$key]['config'] as $name => $config) {
66                // Ignore hidden options
67                if (!empty($config['hidden']) && !$withAll) {
68                    unset($this->options[$key]['config'][$name]);
69                    continue;
70                }
71
72                // Set name for translation
73                if (empty($this->options[$key]['config'][$name]['name'])) {
74                    $this->options[$key]['config'][$name]['name'] = 'config.' . $name;
75                }
76
77                // Configure required icon
78                if (!empty($this->options[$key]['config'][$name]['required'])) {
79                    $this->options[$key]['config'][$name]['required_icon'] = true;
80                }
81
82                // Set ENV name
83                if (empty($config['env'])) {
84                    $config['env'] = Str::upper($name);
85                    $this->options[$key]['config'][$name]['env'] = $config['env'];
86                }
87
88                // Configure select values
89                if ($config['type'] == 'select' || $config['type'] == 'select_multi') {
90                    $data = [];
91                    foreach ($config['data'] ?? [] as $dataKey => $dataValue) {
92                        if (is_int($dataKey) && !($config['preserve_key'] ?? false)) {
93                            $dataKey = $dataValue;
94                            $dataValue = 'config.' . $name . '.select.' . $dataKey;
95                        }
96                        $data[$dataKey] = $dataValue;
97                    }
98                    $this->options[$key]['config'][$name]['data'] = $data;
99                }
100
101                // Set if set from ENV
102                if (isset($fromEnv[$config['env']])) {
103                    $this->options[$key]['config'][$name]['in_env'] = true;
104                }
105
106                // Set if local config can be written to
107                if (!$localConfigWritable && ($config['write_back'] ?? false)) {
108                    $this->options[$key]['config'][$name]['writable'] = false;
109                }
110            }
111        }
112    }
113}