GroupRegistry.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\Model;
  7. use Magento\Customer\Api\Data\GroupInterface;
  8. use Magento\Framework\Exception\NoSuchEntityException;
  9. /**
  10. * Registry for Customer Group models
  11. */
  12. class GroupRegistry
  13. {
  14. /**
  15. * @var array
  16. */
  17. protected $registry = [];
  18. /**
  19. * @var GroupFactory
  20. */
  21. protected $groupFactory;
  22. /**
  23. * @param GroupFactory $groupFactory
  24. */
  25. public function __construct(GroupFactory $groupFactory)
  26. {
  27. $this->groupFactory = $groupFactory;
  28. }
  29. /**
  30. * Get instance of the Group Model identified by an id
  31. *
  32. * @param int $groupId
  33. * @return Group
  34. * @throws NoSuchEntityException
  35. */
  36. public function retrieve($groupId)
  37. {
  38. if (isset($this->registry[$groupId])) {
  39. return $this->registry[$groupId];
  40. }
  41. $group = $this->groupFactory->create();
  42. $group->load($groupId);
  43. if ($group->getId() === null || $group->getId() != $groupId) {
  44. throw NoSuchEntityException::singleField(GroupInterface::ID, $groupId);
  45. }
  46. $this->registry[$groupId] = $group;
  47. return $group;
  48. }
  49. /**
  50. * Remove an instance of the Group Model from the registry
  51. *
  52. * @param int $groupId
  53. * @return void
  54. */
  55. public function remove($groupId)
  56. {
  57. unset($this->registry[$groupId]);
  58. }
  59. }