VerboseMessenger.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. namespace Drupal\pathauto;
  3. use Drupal\Core\Config\ConfigFactoryInterface;
  4. use Drupal\Core\Messenger\MessengerInterface as CoreMessengerInterface;
  5. use Drupal\Core\Session\AccountInterface;
  6. /**
  7. * Provides a verbose messenger.
  8. */
  9. class VerboseMessenger implements MessengerInterface {
  10. /**
  11. * The verbose flag.
  12. *
  13. * @var bool
  14. */
  15. protected $isVerbose;
  16. /**
  17. * The config factory.
  18. *
  19. * @var \Drupal\Core\Config\ConfigFactoryInterface
  20. */
  21. protected $configFactory;
  22. /**
  23. * The current user account.
  24. *
  25. * @var \Drupal\Core\Session\AccountInterface
  26. */
  27. protected $account;
  28. /**
  29. * The messenger service.
  30. *
  31. * @var \Drupal\Core\Messenger\MessengerInterface
  32. */
  33. protected $messenger;
  34. /**
  35. * Creates a verbose messenger.
  36. *
  37. * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
  38. * The config factory.
  39. * @param \Drupal\Core\Session\AccountInterface $account
  40. * The current user account.
  41. * @param \Drupal\Core\Messenger\MessengerInterface $messenger
  42. * The messenger service.
  43. */
  44. public function __construct(ConfigFactoryInterface $config_factory, AccountInterface $account, CoreMessengerInterface $messenger) {
  45. $this->configFactory = $config_factory;
  46. $this->account = $account;
  47. $this->messenger = $messenger;
  48. }
  49. /**
  50. * {@inheritdoc}
  51. */
  52. public function addMessage($message, $op = NULL) {
  53. if (!isset($this->isVerbose)) {
  54. $config = $this->configFactory->get('pathauto.settings');
  55. $this->isVerbose = $config->get('verbose') && $this->account->hasPermission('notify of path changes');
  56. }
  57. if (!$this->isVerbose || (isset($op) && in_array($op, array('bulkupdate', 'return')))) {
  58. return FALSE;
  59. }
  60. if ($message) {
  61. $this->messenger->addMessage($message);
  62. }
  63. return TRUE;
  64. }
  65. }