RealtimeRoom.php 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Broadcasting;
  4. use InvalidArgumentException;
  5. final readonly class RealtimeRoom
  6. {
  7. private function __construct(
  8. public string $name
  9. ) {}
  10. public static function user(int $userId): self
  11. {
  12. return self::make("user.{$userId}");
  13. }
  14. public static function schedule(int $scheduleId): self
  15. {
  16. return self::make("schedule.{$scheduleId}");
  17. }
  18. public static function package(int $servicePackageId): self
  19. {
  20. return self::make("package.{$servicePackageId}");
  21. }
  22. public static function provider(int $providerId): self
  23. {
  24. return self::make("provider.{$providerId}");
  25. }
  26. private static function make(string $name): self
  27. {
  28. if (str_contains($name, ':') || str_contains($name, '@')) {
  29. throw new InvalidArgumentException(
  30. "Nome de sala invalido: \"{$name}\". Nao pode conter \":\" nem \"@\"."
  31. );
  32. }
  33. return new self($name);
  34. }
  35. public function __toString(): string
  36. {
  37. return $this->name;
  38. }
  39. }