CustomerMetadata.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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\CustomerMetadataInterface;
  8. use Magento\Customer\Model\AttributeMetadataConverter;
  9. use Magento\Customer\Model\AttributeMetadataDataProvider;
  10. use Magento\Eav\Model\Entity\Attribute\AbstractAttribute;
  11. use Magento\Framework\Api\SimpleDataObjectConverter;
  12. use Magento\Framework\Exception\NoSuchEntityException;
  13. /**
  14. * Service to fetch customer related custom attributes
  15. */
  16. class CustomerMetadata implements CustomerMetadataInterface
  17. {
  18. /**
  19. * @var array
  20. */
  21. private $customerDataObjectMethods;
  22. /**
  23. * @var AttributeMetadataConverter
  24. */
  25. private $attributeMetadataConverter;
  26. /**
  27. * @var AttributeMetadataDataProvider
  28. */
  29. private $attributeMetadataDataProvider;
  30. /**
  31. * List of system attributes which should be available to the clients.
  32. *
  33. * @var string[]
  34. */
  35. private $systemAttributes;
  36. /**
  37. * @param AttributeMetadataConverter $attributeMetadataConverter
  38. * @param AttributeMetadataDataProvider $attributeMetadataDataProvider
  39. * @param string[] $systemAttributes
  40. */
  41. public function __construct(
  42. AttributeMetadataConverter $attributeMetadataConverter,
  43. AttributeMetadataDataProvider $attributeMetadataDataProvider,
  44. array $systemAttributes = []
  45. ) {
  46. $this->attributeMetadataConverter = $attributeMetadataConverter;
  47. $this->attributeMetadataDataProvider = $attributeMetadataDataProvider;
  48. $this->systemAttributes = $systemAttributes;
  49. }
  50. /**
  51. * @inheritdoc
  52. */
  53. public function getAttributes($formCode)
  54. {
  55. $attributes = [];
  56. $attributesFormCollection = $this->attributeMetadataDataProvider->loadAttributesCollection(
  57. self::ENTITY_TYPE_CUSTOMER,
  58. $formCode
  59. );
  60. foreach ($attributesFormCollection as $attribute) {
  61. /** @var $attribute \Magento\Customer\Model\Attribute */
  62. $attributes[$attribute->getAttributeCode()] = $this->attributeMetadataConverter
  63. ->createMetadataAttribute($attribute);
  64. }
  65. if (empty($attributes)) {
  66. throw NoSuchEntityException::singleField('formCode', $formCode);
  67. }
  68. return $attributes;
  69. }
  70. /**
  71. * @inheritdoc
  72. */
  73. public function getAttributeMetadata($attributeCode)
  74. {
  75. /** @var AbstractAttribute $attribute */
  76. $attribute = $this->attributeMetadataDataProvider->getAttribute(self::ENTITY_TYPE_CUSTOMER, $attributeCode);
  77. if ($attribute && ($attributeCode === 'id' || $attribute->getId() !== null)) {
  78. $attributeMetadata = $this->attributeMetadataConverter->createMetadataAttribute($attribute);
  79. return $attributeMetadata;
  80. } else {
  81. throw new NoSuchEntityException(
  82. __(
  83. 'No such entity with %fieldName = %fieldValue, %field2Name = %field2Value',
  84. [
  85. 'fieldName' => 'entityType',
  86. 'fieldValue' => self::ENTITY_TYPE_CUSTOMER,
  87. 'field2Name' => 'attributeCode',
  88. 'field2Value' => $attributeCode
  89. ]
  90. )
  91. );
  92. }
  93. }
  94. /**
  95. * @inheritdoc
  96. */
  97. public function getAllAttributesMetadata()
  98. {
  99. /** @var AbstractAttribute[] $attribute */
  100. $attributeCodes = $this->attributeMetadataDataProvider->getAllAttributeCodes(
  101. self::ENTITY_TYPE_CUSTOMER,
  102. self::ATTRIBUTE_SET_ID_CUSTOMER
  103. );
  104. $attributesMetadata = [];
  105. foreach ($attributeCodes as $attributeCode) {
  106. try {
  107. $attributesMetadata[] = $this->getAttributeMetadata($attributeCode);
  108. } catch (NoSuchEntityException $e) {
  109. //If no such entity, skip
  110. }
  111. }
  112. return $attributesMetadata;
  113. }
  114. /**
  115. * @inheritdoc
  116. */
  117. public function getCustomAttributesMetadata($dataObjectClassName = self::DATA_INTERFACE_NAME)
  118. {
  119. $customAttributes = [];
  120. if (!$this->customerDataObjectMethods) {
  121. $dataObjectMethods = array_flip(get_class_methods($dataObjectClassName));
  122. $baseClassDataObjectMethods = array_flip(
  123. get_class_methods(\Magento\Framework\Api\AbstractExtensibleObject::class)
  124. );
  125. $this->customerDataObjectMethods = array_diff_key($dataObjectMethods, $baseClassDataObjectMethods);
  126. }
  127. foreach ($this->getAllAttributesMetadata() as $attributeMetadata) {
  128. $attributeCode = $attributeMetadata->getAttributeCode();
  129. $camelCaseKey = SimpleDataObjectConverter::snakeCaseToUpperCamelCase($attributeCode);
  130. $isDataObjectMethod = isset($this->customerDataObjectMethods['get' . $camelCaseKey])
  131. || isset($this->customerDataObjectMethods['is' . $camelCaseKey]);
  132. if (!$isDataObjectMethod
  133. && (!$attributeMetadata->isSystem()
  134. || in_array($attributeCode, $this->systemAttributes)
  135. )
  136. ) {
  137. $customAttributes[] = $attributeMetadata;
  138. }
  139. }
  140. return $customAttributes;
  141. }
  142. }