mivita/dev/subdomain-optimization-grok/src/Domain/DomainContext.php
2025-10-20 17:42:08 +02:00

193 lines
4.8 KiB
PHP

<?php
namespace App\Domain;
use App\Models\UserShop;
use Illuminate\Support\Arr;
/**
* DomainContext - Unveränderlicher Domain-Kontext
*
* Ersetzt das alte DomainContext mit besserer Typsicherheit
* und zusätzlichen Hilfsmethoden.
*/
final readonly class DomainContext
{
public function __construct(
public DomainType $type,
public string $host,
public ?string $subdomain,
public ?UserShop $userShop = null,
public ?string $path = null,
public array $metadata = []
) {}
/**
* Erstellt DomainContext aus Domain-Informationen
*/
public static function fromDomainInfo(array $domainInfo, ?UserShop $userShop = null): self
{
return new self(
type: DomainType::fromString(Arr::get($domainInfo, 'type', 'unknown')),
host: Arr::get($domainInfo, 'host', ''),
subdomain: Arr::get($domainInfo, 'subdomain'),
userShop: $userShop,
path: Arr::get($domainInfo, 'path'),
metadata: Arr::get($domainInfo, 'metadata', [])
);
}
/**
* Erstellt DomainContext für unbekannte Domains
*/
public static function unknown(string $host): self
{
return new self(
type: DomainType::UNKNOWN,
host: $host,
subdomain: null
);
}
/**
* Prüft, ob es sich um eine bekannte Domain handelt
*/
public function isKnown(): bool
{
return $this->type->isKnown();
}
/**
* Prüft, ob es sich um eine unbekannte Domain handelt
*/
public function isUnknown(): bool
{
return $this->type === DomainType::UNKNOWN;
}
/**
* Prüft, ob es sich um eine User-Shop-Domain handelt
*/
public function isUserShop(): bool
{
return $this->type->isUserShop();
}
/**
* Prüft, ob es sich um eine feste Subdomain handelt
*/
public function isFixedSubdomain(): bool
{
return $this->type->isFixedSubdomain();
}
/**
* Prüft, ob es sich um eine Haupt-Domain handelt
*/
public function isMainDomain(): bool
{
return $this->type->isMainDomain();
}
/**
* Prüft, ob Session-Daten beibehalten werden sollen
*/
public function shouldPreserveSession(): bool
{
return $this->type->shouldPreserveSession();
}
/**
* Gibt den UserShop-Slug zurück
*/
public function getUserShopSlug(): ?string
{
return $this->userShop?->slug;
}
/**
* Gibt die Route-Gruppe zurück
*/
public function getRouteGroup(): string
{
return $this->type->getRouteGroup();
}
/**
* Gibt die Session-Domain zurück
*/
public function getSessionDomain(string $baseDomain, string $shopTld, string $careTld): string
{
return $this->type->getSessionDomain($baseDomain, $shopTld, $careTld);
}
/**
* Prüft, ob der UserShop aktiv ist
*/
public function isUserShopActive(): bool
{
return $this->userShop && $this->userShop->active;
}
/**
* Prüft, ob der UserShop-Zahlung aktiv ist
*/
public function isUserShopPaymentActive(): bool
{
return $this->userShop &&
$this->userShop->user &&
$this->userShop->user->isActiveShop();
}
/**
* Gibt Domain-Informationen als Array zurück
*/
public function toArray(): array
{
return [
'type' => $this->type->value,
'host' => $this->host,
'subdomain' => $this->subdomain,
'user_shop_id' => $this->userShop?->id,
'user_shop_slug' => $this->getUserShopSlug(),
'path' => $this->path,
'metadata' => $this->metadata,
'is_known' => $this->isKnown(),
'is_user_shop' => $this->isUserShop(),
'should_preserve_session' => $this->shouldPreserveSession(),
'route_group' => $this->getRouteGroup()
];
}
/**
* Gibt eine lesbare String-Repräsentation zurück
*/
public function __toString(): string
{
$parts = [
"DomainType: {$this->type->value}",
"Host: {$this->host}"
];
if ($this->subdomain) {
$parts[] = "Subdomain: {$this->subdomain}";
}
if ($this->userShop) {
$parts[] = "UserShop: {$this->userShop->slug} (ID: {$this->userShop->id})";
}
return '[' . implode(', ', $parts) . ']';
}
/**
* Vergleicht zwei DomainContexts
*/
public function equals(self $other): bool
{
return $this->type === $other->type &&
$this->host === $other->host &&
$this->subdomain === $other->subdomain &&
$this->userShop?->id === $other->userShop?->id;
}
}