AddressRegistry.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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\Framework\Exception\NoSuchEntityException;
  8. /**
  9. * Registry for Address models
  10. */
  11. class AddressRegistry
  12. {
  13. /**
  14. * @var Address[]
  15. */
  16. protected $registry = [];
  17. /**
  18. * @var AddressFactory
  19. */
  20. protected $addressFactory;
  21. /**
  22. * @param AddressFactory $addressFactory
  23. */
  24. public function __construct(AddressFactory $addressFactory)
  25. {
  26. $this->addressFactory = $addressFactory;
  27. }
  28. /**
  29. * Get instance of the Address Model identified by id
  30. *
  31. * @param int $addressId
  32. * @return Address
  33. * @throws NoSuchEntityException
  34. */
  35. public function retrieve($addressId)
  36. {
  37. if (isset($this->registry[$addressId])) {
  38. return $this->registry[$addressId];
  39. }
  40. $address = $this->addressFactory->create();
  41. $address->load($addressId);
  42. if (!$address->getId()) {
  43. throw NoSuchEntityException::singleField('addressId', $addressId);
  44. }
  45. $this->registry[$addressId] = $address;
  46. return $address;
  47. }
  48. /**
  49. * Remove an instance of the Address Model from the registry
  50. *
  51. * @param int $addressId
  52. * @return void
  53. */
  54. public function remove($addressId)
  55. {
  56. unset($this->registry[$addressId]);
  57. }
  58. /**
  59. * Replace existing Address Model with a new one
  60. *
  61. * @param Address $address
  62. * @return $this
  63. */
  64. public function push(Address $address)
  65. {
  66. $this->registry[$address->getId()] = $address;
  67. return $this;
  68. }
  69. }