Converter.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Integration\Model\Config\Consolidated;
  7. /**
  8. * Converter of integration.xml content into array format.
  9. */
  10. class Converter implements \Magento\Framework\Config\ConverterInterface
  11. {
  12. /**#@+
  13. * Array keys for config internal representation.
  14. */
  15. const KEY_EMAIL = 'email';
  16. const KEY_AUTHENTICATION_ENDPOINT_URL = 'endpoint_url';
  17. const KEY_IDENTITY_LINKING_URL = 'identity_link_url';
  18. const API_RESOURCES = 'resource';
  19. const API_RESOURCE_NAME = 'name';
  20. /**#@-*/
  21. /**#@-*/
  22. protected $resourceProvider;
  23. /**
  24. * Initialize dependencies.
  25. *
  26. * @param \Magento\Framework\Acl\AclResource\ProviderInterface $resourceProvider
  27. */
  28. public function __construct(
  29. \Magento\Framework\Acl\AclResource\ProviderInterface $resourceProvider
  30. ) {
  31. $this->resourceProvider = $resourceProvider;
  32. }
  33. /**
  34. * @inheritdoc
  35. */
  36. public function convert($source)
  37. {
  38. $result = [];
  39. $allResources = $this->resourceProvider->getAclResources();
  40. $hashAclResourcesTree = $this->hashResources($allResources[1]['children']);
  41. /** @var \DOMNodeList $integrations */
  42. $integrations = $source->getElementsByTagName('integration');
  43. /** @var \DOMElement $integration */
  44. foreach ($integrations as $integration) {
  45. if ($integration->nodeType != XML_ELEMENT_NODE) {
  46. continue;
  47. }
  48. $integrationName = $integration->attributes->getNamedItem('name')->nodeValue;
  49. $result[$integrationName] = [];
  50. $result[$integrationName][self::API_RESOURCES] = [];
  51. /** @var \DOMElement $email */
  52. $email = $integration->getElementsByTagName('email')->item(0)->nodeValue;
  53. /** @var \DOMNodeList $resources */
  54. $resources = $integration->getElementsByTagName('resource');
  55. $result[$integrationName][self::KEY_EMAIL] = $email;
  56. if ($integration->getElementsByTagName('endpoint_url')->length) {
  57. /** @var \DOMElement $endpointUrl */
  58. $endpointUrl = $integration->getElementsByTagName('endpoint_url')->item(0)->nodeValue;
  59. $result[$integrationName][self::KEY_AUTHENTICATION_ENDPOINT_URL] = $endpointUrl;
  60. }
  61. if ($integration->getElementsByTagName('identity_link_url')->length) {
  62. /** @var \DOMElement $identityLinkUrl */
  63. $identityLinkUrl = $integration->getElementsByTagName('identity_link_url')->item(0)->nodeValue;
  64. $result[$integrationName][self::KEY_IDENTITY_LINKING_URL] = $identityLinkUrl;
  65. }
  66. /** @var \DOMElement $resource */
  67. foreach ($resources as $resource) {
  68. if ($resource->nodeType != XML_ELEMENT_NODE) {
  69. continue;
  70. }
  71. $resource = $resource->attributes->getNamedItem('name')->nodeValue;
  72. $resourceNames = $this->addParentsToResource($hashAclResourcesTree, $resource);
  73. foreach ($resourceNames as $name) {
  74. $result[$integrationName][self::API_RESOURCES][] = $name;
  75. }
  76. }
  77. // Add root resource if any child has been added
  78. if (!empty($result[$integrationName][self::API_RESOURCES])) {
  79. array_unshift($result[$integrationName][self::API_RESOURCES], $allResources[1]['id']);
  80. }
  81. // Remove any duplicates added parents
  82. $result[$integrationName][self::API_RESOURCES] = array_values(
  83. array_unique($result[$integrationName][self::API_RESOURCES])
  84. );
  85. }
  86. return $result;
  87. }
  88. /**
  89. * Make ACL resource array return a hash with parent-resource-name => [children-resources-names] representation
  90. *
  91. * @param array $resources
  92. * @return array
  93. */
  94. private function hashResources(array $resources)
  95. {
  96. $output = [];
  97. foreach ($resources as $resource) {
  98. if (isset($resource['children'])) {
  99. $item = $this->hashResources($resource['children']);
  100. } else {
  101. $item = [];
  102. }
  103. $output[$resource['id']] = $item;
  104. }
  105. return $output;
  106. }
  107. /**
  108. * Find parents names of a node in an ACL resource hash and add them to returned array
  109. *
  110. * @param array $resourcesHash
  111. * @param string $nodeName
  112. * @return array
  113. */
  114. private function addParentsToResource(array $resourcesHash, $nodeName)
  115. {
  116. $output = [];
  117. foreach ($resourcesHash as $resource => $children) {
  118. if ($resource == $nodeName) {
  119. $output = [$resource];
  120. break;
  121. }
  122. if (!empty($children)) {
  123. $names = $this->addParentsToResource($children, $nodeName);
  124. if (!empty($names)) {
  125. $output = array_merge([$resource], $names);
  126. break;
  127. } else {
  128. continue;
  129. }
  130. }
  131. }
  132. return $output;
  133. }
  134. }