vendor/league/tactician-logger/src/LoggerMiddleware.php line 38

Open in your IDE?
  1. <?php
  2. namespace League\Tactician\Logger;
  3. use League\Tactician\Logger\Formatter\Formatter;
  4. use League\Tactician\Middleware;
  5. use Psr\Log\LoggerInterface;
  6. use Exception;
  7. /**
  8. * Add support for writing a message to the log whenever a command is received,
  9. * handled or failed.
  10. */
  11. class LoggerMiddleware implements Middleware
  12. {
  13. /**
  14. * @var LoggerInterface
  15. */
  16. private $logger;
  17. /**
  18. * @var Formatter
  19. */
  20. private $formatter;
  21. /**
  22. * @param Formatter $formatter
  23. * @param LoggerInterface $logger
  24. */
  25. public function __construct(Formatter $formatter, LoggerInterface $logger)
  26. {
  27. $this->formatter = $formatter;
  28. $this->logger = $logger;
  29. }
  30. /**
  31. * {@inheritdoc}
  32. */
  33. public function execute($command, callable $next)
  34. {
  35. $this->formatter->logCommandReceived($this->logger, $command);
  36. try {
  37. $returnValue = $next($command);
  38. } catch (Exception $e) {
  39. $this->formatter->logCommandFailed($this->logger, $command, $e);
  40. throw $e;
  41. }
  42. $this->formatter->logCommandSucceeded($this->logger, $command, $returnValue);
  43. return $returnValue;
  44. }
  45. }