adrema/app/Setting/SettingFactory.php

53 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;
2024-08-01 11:17:49 +02:00
use App\Setting\Contracts\Viewable;
2022-10-20 02:15:28 +02:00
use Illuminate\Routing\Router;
class SettingFactory
{
/**
* @var array<int, class-string<LocalSettings>>
*/
private array $settings = [];
/**
2023-06-01 12:04:31 +02:00
* @param class-string $setting
2022-10-20 02:15:28 +02:00
*/
public function register(string $setting): void
{
$this->settings[] = $setting;
2023-06-01 12:04:31 +02:00
if (new $setting() instanceof Storeable) {
2024-08-01 10:25:48 +02:00
app(Router::class)->middleware(['web', 'auth:web'])->post($setting::url(), $setting::storeAction());
2023-06-01 12:04:31 +02:00
}
2022-10-20 02:15:28 +02:00
if (1 === count($this->settings)) {
2024-08-01 10:25:48 +02:00
app(Router::class)->redirect('/setting', '/setting/' . $setting::slug());
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
public function resolveGroupName(string $name): Viewable
{
$settingClass = collect($this->settings)->filter(fn ($setting) => new $setting() instanceof Viewable)->first(fn ($setting) => $setting::group() === $name);
return app($settingClass);
}
2022-10-20 02:15:28 +02:00
}