adrema/app/Setting/SettingFactory.php

57 lines
1.4 KiB
PHP
Raw Normal View History

2022-10-20 02:15:28 +02:00
<?php
namespace App\Setting;
2024-08-01 11:17:49 +02:00
use App\Invoice\InvoiceSettings;
2023-06-01 12:04:31 +02:00
use App\Setting\Contracts\Storeable;
2022-10-20 02:15:28 +02:00
use Illuminate\Routing\Router;
class SettingFactory
{
/**
* @var array<int, class-string<LocalSettings>>
*/
private array $settings = [];
/**
2024-08-01 11:32:14 +02:00
* @param class-string<LocalSettings> $setting
2022-10-20 02:15:28 +02:00
*/
public function register(string $setting): void
{
$this->settings[] = $setting;
2024-08-01 11:32:14 +02:00
if (new $setting instanceof Storeable) {
$this->registerStoreRoute(new $setting);
2023-06-01 12:04:31 +02:00
}
2022-10-20 02:15:28 +02:00
if (1 === count($this->settings)) {
2024-08-01 11:32:14 +02:00
app(Router::class)->redirect('/setting', '/setting/' . $setting::group());
2022-10-20 02:15:28 +02:00
}
}
/**
* @return array<int, array{url: string, is_active: bool}>
*/
public function getShare(): array
{
return collect($this->settings)->map(fn ($setting) => [
'url' => $setting::url(),
2024-08-01 10:25:48 +02:00
'is_active' => '/' . request()->path() === $setting::url(),
2022-10-20 02:15:28 +02:00
'title' => $setting::title(),
])
2024-08-01 10:25:48 +02:00
->toArray();
2022-10-20 02:15:28 +02:00
}
2024-08-01 11:17:49 +02:00
2024-08-01 11:32:14 +02:00
public function resolveGroupName(string $name): LocalSettings
2024-08-01 11:17:49 +02:00
{
2024-08-01 11:32:14 +02:00
$settingClass = collect($this->settings)->first(fn ($setting) => $setting::group() === $name);
2024-08-01 11:17:49 +02:00
return app($settingClass);
}
2024-08-01 11:32:14 +02:00
protected function registerStoreRoute(Storeable $setting): void
{
app(Router::class)->middleware(['web', 'auth:web'])->post($setting::url(), $setting::storeAction());
}
2022-10-20 02:15:28 +02:00
}