CachedMetadata.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\Model\Metadata;
  7. use Magento\Customer\Api\MetadataInterface;
  8. use Magento\Framework\App\ObjectManager;
  9. /**
  10. * Cached attribute metadata service
  11. */
  12. class CachedMetadata implements MetadataInterface
  13. {
  14. const CACHE_SEPARATOR = ';';
  15. /**
  16. * @var string
  17. */
  18. protected $entityType = 'none';
  19. /**
  20. * @var AttributeMetadataCache
  21. */
  22. private $attributeMetadataCache;
  23. /**
  24. * @var MetadataInterface
  25. */
  26. protected $metadata;
  27. /**
  28. * Constructor
  29. *
  30. * @param MetadataInterface $metadata
  31. * @param AttributeMetadataCache|null $attributeMetadataCache
  32. */
  33. public function __construct(
  34. MetadataInterface $metadata,
  35. AttributeMetadataCache $attributeMetadataCache = null
  36. ) {
  37. $this->metadata = $metadata;
  38. $this->attributeMetadataCache = $attributeMetadataCache ?: ObjectManager::getInstance()
  39. ->get(AttributeMetadataCache::class);
  40. }
  41. /**
  42. * {@inheritdoc}
  43. */
  44. public function getAttributes($formCode)
  45. {
  46. $attributes = $this->attributeMetadataCache->load($this->entityType, $formCode);
  47. if ($attributes !== false) {
  48. return $attributes;
  49. }
  50. $attributes = $this->metadata->getAttributes($formCode);
  51. $this->attributeMetadataCache->save($this->entityType, $attributes, $formCode);
  52. return $attributes;
  53. }
  54. /**
  55. * {@inheritdoc}
  56. */
  57. public function getAttributeMetadata($attributeCode)
  58. {
  59. $attributesMetadata = $this->attributeMetadataCache->load($this->entityType, $attributeCode);
  60. if (false !== $attributesMetadata) {
  61. return array_shift($attributesMetadata);
  62. }
  63. $attributeMetadata = $this->metadata->getAttributeMetadata($attributeCode);
  64. $this->attributeMetadataCache->save($this->entityType, [$attributeMetadata], $attributeCode);
  65. return $attributeMetadata;
  66. }
  67. /**
  68. * {@inheritdoc}
  69. */
  70. public function getAllAttributesMetadata()
  71. {
  72. $attributes = $this->attributeMetadataCache->load($this->entityType, 'all');
  73. if ($attributes !== false) {
  74. return $attributes;
  75. }
  76. $attributes = $this->metadata->getAllAttributesMetadata();
  77. $this->attributeMetadataCache->save($this->entityType, $attributes, 'all');
  78. return $attributes;
  79. }
  80. /**
  81. * {@inheritdoc}
  82. */
  83. public function getCustomAttributesMetadata($dataObjectClassName = null)
  84. {
  85. $attributes = $this->attributeMetadataCache->load($this->entityType, 'custom');
  86. if ($attributes !== false) {
  87. return $attributes;
  88. }
  89. $attributes = $this->metadata->getCustomAttributesMetadata();
  90. $this->attributeMetadataCache->save($this->entityType, $attributes, 'custom');
  91. return $attributes;
  92. }
  93. }