選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

CompanySettingsService.php 3.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. namespace App\Services;
  3. use App\Enums\Setting\DateFormat;
  4. use App\Enums\Setting\Font;
  5. use App\Enums\Setting\PrimaryColor;
  6. use App\Enums\Setting\WeekStart;
  7. use App\Models\Company;
  8. use Illuminate\Support\Facades\Cache;
  9. class CompanySettingsService
  10. {
  11. public static function getSettings(int $companyId): array
  12. {
  13. $cacheKey = "company_settings_{$companyId}";
  14. return Cache::rememberForever($cacheKey, function () use ($companyId) {
  15. $company = Company::with(['locale', 'appearance'])->find($companyId);
  16. if (! $company) {
  17. return self::getDefaultSettings();
  18. }
  19. return [
  20. 'default_language' => $company->locale->language ?? config('transmatic.source_locale'),
  21. 'default_timezone' => $company->locale->timezone ?? config('app.timezone'),
  22. 'default_currency' => $company->currency_code ?? 'USD',
  23. 'default_primary_color' => $company->appearance->primary_color->value ?? PrimaryColor::DEFAULT,
  24. 'default_font' => $company->appearance->font->value ?? Font::DEFAULT,
  25. 'default_date_format' => $company->locale->date_format->value ?? DateFormat::DEFAULT,
  26. 'default_week_start' => $company->locale->week_start->value ?? WeekStart::DEFAULT,
  27. ];
  28. });
  29. }
  30. public static function invalidateSettings(int $companyId): void
  31. {
  32. $cacheKey = "company_settings_{$companyId}";
  33. Cache::forget($cacheKey);
  34. }
  35. public static function getDefaultSettings(): array
  36. {
  37. return [
  38. 'default_language' => config('transmatic.source_locale'),
  39. 'default_timezone' => config('app.timezone'),
  40. 'default_currency' => 'USD',
  41. 'default_primary_color' => PrimaryColor::DEFAULT,
  42. 'default_font' => Font::DEFAULT,
  43. 'default_date_format' => DateFormat::DEFAULT,
  44. 'default_week_start' => WeekStart::DEFAULT,
  45. ];
  46. }
  47. public static function getSpecificSetting(int $companyId, string $key, $default = null)
  48. {
  49. $settings = self::getSettings($companyId);
  50. return $settings[$key] ?? $default;
  51. }
  52. public static function getDefaultLanguage(int $companyId): string
  53. {
  54. return self::getSpecificSetting($companyId, 'default_language', config('transmatic.source_locale'));
  55. }
  56. public static function getDefaultTimezone(int $companyId): string
  57. {
  58. return self::getSpecificSetting($companyId, 'default_timezone', config('app.timezone'));
  59. }
  60. public static function getDefaultCurrency(int $companyId): string
  61. {
  62. return self::getSpecificSetting($companyId, 'default_currency', 'USD');
  63. }
  64. public static function getDefaultPrimaryColor(int $companyId): string
  65. {
  66. return self::getSpecificSetting($companyId, 'default_primary_color', PrimaryColor::DEFAULT);
  67. }
  68. public static function getDefaultFont(int $companyId): string
  69. {
  70. return self::getSpecificSetting($companyId, 'default_font', Font::DEFAULT);
  71. }
  72. public static function getDefaultDateFormat(int $companyId): string
  73. {
  74. return self::getSpecificSetting($companyId, 'default_date_format', DateFormat::DEFAULT);
  75. }
  76. public static function getDefaultWeekStart(int $companyId): string
  77. {
  78. return self::getSpecificSetting($companyId, 'default_week_start', WeekStart::DEFAULT);
  79. }
  80. }