BaseCronJob.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. <?php
  2. namespace App\Service\Cron;
  3. use App\Service\Cron\UI\CronUIInterface;
  4. use App\Service\Cron\UI\SilentUI;
  5. use App\Service\Utils\StringsUtils;
  6. use JetBrains\PhpStorm\Pure;
  7. use Psr\Log\LoggerInterface;
  8. use Symfony\Contracts\Service\Attribute\Required;
  9. /**
  10. * Base class for the Cron-jobs defined in \App\Service\Cron\Job
  11. *
  12. * This class shouldn't implement directly the CronjobInterface because it shall not be injected itself into the
  13. * CronjobIterator, but all its subclasses should.
  14. */
  15. abstract class BaseCronJob
  16. {
  17. protected CronUIInterface $ui;
  18. protected LoggerInterface $logger;
  19. #[Pure]
  20. public function __construct()
  21. {
  22. $this->ui = new SilentUI();
  23. }
  24. #[Required]
  25. /** @see https://symfony.com/doc/current/logging/channels_handlers.html#how-to-autowire-logger-channels */
  26. public function setLoggerInterface(LoggerInterface $cronLogger): void { $this->logger = $cronLogger; }
  27. final public function name(): string {
  28. return StringsUtils::camelToSnake(
  29. preg_replace('/(?:\w+\\\)*(\w+)$/', '$1', static::class),
  30. '-'
  31. );
  32. }
  33. public function setUI(CronUIInterface $ui): void {
  34. $this->ui = $ui;
  35. }
  36. }