Alert.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. /**
  3. * MageSpecialist
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to info@magespecialist.it so we can send you a copy immediately.
  14. *
  15. * @category MSP
  16. * @package MSP_TwoFactorAuth
  17. * @copyright Copyright (c) 2017 Skeeller srl (http://www.magespecialist.it)
  18. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  19. */
  20. namespace MSP\TwoFactorAuth\Model;
  21. use Magento\Framework\Event\ManagerInterface;
  22. class Alert implements AlertInterface
  23. {
  24. /**
  25. * @var ManagerInterface
  26. */
  27. private $eventManager;
  28. public function __construct(
  29. ManagerInterface $eventManager
  30. ) {
  31. $this->eventManager = $eventManager;
  32. }
  33. /**
  34. * Trigger a security suite event
  35. * @param string $module
  36. * @param string $message
  37. * @param string $level
  38. * @param string $username
  39. * @param string $action
  40. * @param array|string $payload
  41. * @return boolean
  42. */
  43. public function event($module, $message, $level = null, $username = null, $action = null, $payload = null)
  44. {
  45. if ($level === null) {
  46. $level = self::LEVEL_INFO;
  47. }
  48. $params = [
  49. AlertInterface::ALERT_PARAM_LEVEL => $level,
  50. AlertInterface::ALERT_PARAM_MODULE => $module,
  51. AlertInterface::ALERT_PARAM_MESSAGE => $message,
  52. AlertInterface::ALERT_PARAM_USERNAME => $username,
  53. AlertInterface::ALERT_PARAM_PAYLOAD => $payload,
  54. ];
  55. $genericEvent = AlertInterface::EVENT_PREFIX . '_event';
  56. $moduleEvent = AlertInterface::EVENT_PREFIX . '_event_' . strtolower($module);
  57. $severityEvent = AlertInterface::EVENT_PREFIX . '_level_' . strtolower($level);
  58. $this->eventManager->dispatch($genericEvent, $params);
  59. $this->eventManager->dispatch($moduleEvent, $params);
  60. $this->eventManager->dispatch($severityEvent, $params);
  61. if ($action) {
  62. $actionEvent = AlertInterface::EVENT_PREFIX . '_action_' . strtolower($action);
  63. $this->eventManager->dispatch($actionEvent, $params);
  64. }
  65. return true;
  66. }
  67. }