JobContext.php 795 B

123456789101112131415161718192021222324252627282930313233
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Domain\Jobs;
  4. use App\Domain\Time\Clock;
  5. use Psr\Log\LoggerInterface;
  6. /**
  7. * Per-invocation state passed into Job::run().
  8. *
  9. * Carries the clock + logger plus arbitrary params (e.g. `full=true` for
  10. * RecomputeScoresJob). `param()` is the only safe way to read params —
  11. * defaults handle missing keys without forcing every job to validate them.
  12. */
  13. final class JobContext
  14. {
  15. /**
  16. * @param array<string, mixed> $params
  17. */
  18. public function __construct(
  19. public readonly Clock $clock,
  20. public readonly LoggerInterface $logger,
  21. public readonly array $params = [],
  22. ) {
  23. }
  24. public function param(string $key, mixed $default = null): mixed
  25. {
  26. return $this->params[$key] ?? $default;
  27. }
  28. }