<?php
 namespace App\Services\Store; use App\Models\Brand; use App\Models\Client; use App\Models\Setting; class BrandContext { private ?Brand $brand = null; public function set(?Brand $brand): void { $this->brand = $brand; } public function current(): ?Brand { return $this->brand; } public function isBranded(): bool { return $this->brand !== null; } public function theme(): ?string { $theme = $this->brand?->theme; return ($theme !== null && $theme !== '') ? $theme : null; } public function visibleProductGroupIds(): ?array { if (! $this->isBranded()) { return null; } $visible = $this->normalizeIntList($this->brand->visible_product_groups); $allowed = $this->normalizeIntList($this->brand->reseller?->allowed_product_groups); return $this->intersectRestrictions($visible, $allowed); } public function visibleTlds(): ?array { if (! $this->isBranded()) { return null; } $normalize = fn ($list) => (is_array($list) && $list !== []) ? array_values(array_map(fn ($t) => '.' . ltrim((string) $t, '.'), $list)) : null; $visible = $normalize($this->brand->visible_tlds); $allowed = $normalize($this->brand->reseller?->allowed_tlds); return $this->intersectRestrictions($visible, $allowed); } private function normalizeIntList(mixed $list): ?array { return (is_array($list) && $list !== []) ? array_values(array_map('intval', $list)) : null; } private function intersectRestrictions(?array $a, ?array $b): ?array { if ($a === null) { return $b; } if ($b === null) { return $a; } return array_values(array_intersect($a, $b)); } public function productGroupVisible(?int $groupId): bool { $visible = $this->visibleProductGroupIds(); return $visible === null || in_array((int) $groupId, $visible, true); } public function tldVisible(?string $tld): bool { $visible = $this->visibleTlds(); if ($visible === null) { return true; } return in_array('.' . ltrim((string) $tld, '.'), $visible, true); } public function defaultLanguage(): ?string { $lang = $this->brand?->default_language; return ($lang !== null && $lang !== '') ? strtolower($lang) : null; } public function defaultCurrency(): ?string { $cur = $this->brand?->default_currency; return ($cur !== null && $cur !== '') ? strtoupper($cur) : null; } public function mailIdentity(): array { if ($this->isBranded()) { return [ 'site_name' => $this->brand->name, 'from_name' => $this->brand->name, 'logo_url' => $this->brand->logo, 'base_url' => 'https://' . $this->brand->domain, ]; } $siteName = Setting::getValue('site_name', config('app.name')); return [ 'site_name' => $siteName, 'from_name' => $siteName, 'logo_url' => null, 'base_url' => rtrim(config('app.url'), '/'), ]; } public function seo(string $key, $default = null) { if ($this->isBranded()) { $override = data_get($this->brand->seo_overrides, $key); if ($override !== null && $override !== '' && $override !== []) { return $override; } } $global = Setting::getValue($key, null); return ($global !== null && $global !== '') ? $global : $default; } public function seoSameAs(): array { $list = null; if ($this->isBranded()) { $override = data_get($this->brand->seo_overrides, 'seo_org_sameas'); if (is_array($override) && $override !== []) { $list = $override; } } if ($list === null) { $list = setting_array('seo_org_sameas', []); } return array_values(array_filter( array_map(fn ($u) => trim((string) $u), $list), fn ($u) => $u !== '' && preg_match('#^https?://#i', $u) === 1 )); } public function clientBelongsHere(Client $client): bool { return (int) ($client->brand_id ?? 0) === (int) ($this->brand?->id ?? 0); } } 