69 lines
1.8 KiB
PHP
69 lines
1.8 KiB
PHP
<?php
|
|
|
|
namespace App\Domain;
|
|
|
|
use App\Models\UserShop;
|
|
use Illuminate\Support\Arr;
|
|
|
|
/**
|
|
* DomainContext ist ein unveränderliches Datenobjekt, das den Kontext
|
|
* der aktuellen Domain für eine einzelne Anfrage enthält.
|
|
*
|
|
* Es wird vom DomainServiceProvider erstellt und im Service-Container
|
|
* registriert, damit andere Teile der Anwendung darauf zugreifen können.
|
|
*/
|
|
final class DomainContext
|
|
{
|
|
/**
|
|
* @param string $type Der Typ der Domain (z.B. 'main', 'crm', 'user-shop').
|
|
* @param string $host Der vollständige Hostname (z.B. 'my.mivita.care').
|
|
* @param string|null $subdomain Die extrahierte Subdomain (z.B. 'my').
|
|
* @param UserShop|null $userShop Das zugehörige UserShop-Objekt, falls vorhanden.
|
|
*/
|
|
public function __construct(
|
|
public readonly string $type,
|
|
public readonly string $host,
|
|
public readonly ?string $subdomain,
|
|
public readonly ?UserShop $userShop
|
|
) {
|
|
}
|
|
|
|
/**
|
|
* Erstellt eine neue Instanz aus einem Array von Domain-Informationen.
|
|
*/
|
|
public static function fromArray(array $domainInfo, ?UserShop $userShop = null): self
|
|
{
|
|
return new self(
|
|
Arr::get($domainInfo, 'type', 'unknown'),
|
|
Arr::get($domainInfo, 'host', ''),
|
|
Arr::get($domainInfo, 'subdomain'),
|
|
$userShop
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Prüft, ob es sich um eine bekannte Domain handelt.
|
|
*/
|
|
public function isUnknown(): bool
|
|
{
|
|
return $this->type === 'unknown';
|
|
}
|
|
|
|
/**
|
|
* Prüft, ob es sich um eine User-Shop-Domain handelt.
|
|
*/
|
|
public function isUserShop(): bool
|
|
{
|
|
return $this->type === 'user-shop';
|
|
}
|
|
|
|
/**
|
|
* Gibt den Slug des User-Shops zurück.
|
|
*/
|
|
public function getUserShopSlug(): ?string
|
|
{
|
|
return $this->userShop?->slug;
|
|
}
|
|
|
|
|
|
}
|