NotifierList.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Notification;
  7. /**
  8. * List of registered system notifiers
  9. * @api
  10. *
  11. * @since 100.0.2
  12. */
  13. class NotifierList
  14. {
  15. /**
  16. * Object manager
  17. *
  18. * @var \Magento\Framework\ObjectManagerInterface
  19. */
  20. protected $objectManager;
  21. /**
  22. * List of notifiers
  23. *
  24. * @var NotifierInterface[]|string[]
  25. */
  26. protected $notifiers;
  27. /**
  28. * Whether the list of notifiers is verified (all notifiers should implement NotifierInterface interface)
  29. *
  30. * @var bool
  31. */
  32. protected $isNotifiersVerified;
  33. /**
  34. * @param \Magento\Framework\ObjectManagerInterface $objectManager
  35. * @param NotifierInterface[]|string[] $notifiers
  36. */
  37. public function __construct(\Magento\Framework\ObjectManagerInterface $objectManager, $notifiers = [])
  38. {
  39. $this->objectManager = $objectManager;
  40. $this->notifiers = $notifiers;
  41. $this->isNotifiersVerified = false;
  42. }
  43. /**
  44. * Returning list of notifiers.
  45. *
  46. * @return NotifierInterface[]
  47. * @throws \InvalidArgumentException
  48. */
  49. public function asArray()
  50. {
  51. if (!$this->isNotifiersVerified) {
  52. $hasErrors = false;
  53. foreach ($this->notifiers as $classIndex => $class) {
  54. $notifier = $this->objectManager->get($class);
  55. if ($notifier instanceof NotifierInterface) {
  56. $this->notifiers[$classIndex] = $notifier;
  57. } else {
  58. $hasErrors = true;
  59. unset($this->notifiers[$classIndex]);
  60. }
  61. }
  62. $this->isNotifiersVerified = true;
  63. if ($hasErrors) {
  64. throw new \InvalidArgumentException('All notifiers should implement NotifierInterface');
  65. }
  66. }
  67. return $this->notifiers;
  68. }
  69. }