MarkUserNotified.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\ReleaseNotification\Controller\Adminhtml\Notification;
  7. use Magento\Backend\App\Action;
  8. use Magento\Framework\Exception\LocalizedException;
  9. use Magento\Framework\Controller\ResultFactory;
  10. use Magento\ReleaseNotification\Model\ResourceModel\Viewer\Logger as NotificationLogger;
  11. use Magento\Framework\App\ProductMetadataInterface;
  12. use Psr\Log\LoggerInterface;
  13. /**
  14. * Controller to record that the current admin user has seen the release notification content
  15. */
  16. class MarkUserNotified extends Action
  17. {
  18. /**
  19. * @var ProductMetadataInterface
  20. */
  21. private $productMetadata;
  22. /**
  23. * @var NotificationLogger
  24. */
  25. private $notificationLogger;
  26. /**
  27. * @var LoggerInterface
  28. */
  29. private $logger;
  30. /**
  31. * MarkUserNotified constructor.
  32. *
  33. * @param Action\Context $context
  34. * @param ProductMetadataInterface $productMetadata
  35. * @param NotificationLogger $notificationLogger
  36. * @param LoggerInterface $logger
  37. */
  38. public function __construct(
  39. Action\Context $context,
  40. ProductMetadataInterface $productMetadata,
  41. NotificationLogger $notificationLogger,
  42. LoggerInterface $logger
  43. ) {
  44. parent::__construct($context);
  45. $this->productMetadata = $productMetadata;
  46. $this->notificationLogger = $notificationLogger;
  47. $this->logger = $logger;
  48. }
  49. /**
  50. * Log information about the last shown advertisement
  51. *
  52. * @return \Magento\Framework\Controller\ResultInterface
  53. */
  54. public function execute()
  55. {
  56. try {
  57. $responseContent = [
  58. 'success' => $this->notificationLogger->log(
  59. $this->_auth->getUser()->getId(),
  60. $this->productMetadata->getVersion()
  61. ),
  62. 'error_message' => ''
  63. ];
  64. } catch (LocalizedException $e) {
  65. $this->logger->error($e->getMessage());
  66. $responseContent = [
  67. 'success' => false,
  68. 'error_message' => $e->getMessage()
  69. ];
  70. } catch (\Exception $e) {
  71. $this->logger->error($e->getMessage());
  72. $responseContent = [
  73. 'success' => false,
  74. 'error_message' => __('It is impossible to log user action')
  75. ];
  76. }
  77. $resultJson = $this->resultFactory->create(ResultFactory::TYPE_JSON);
  78. return $resultJson->setData($responseContent);
  79. }
  80. /**
  81. * @return bool
  82. */
  83. protected function _isAllowed()
  84. {
  85. return parent::_isAllowed();
  86. }
  87. }