PageRenderTime 131ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/Magento/Integration/Model/Config/Consolidated/Converter.php

https://gitlab.com/crazybutterfly815/magento2
PHP | 137 lines | 88 code | 8 blank | 41 comment | 13 complexity | 6d9e0d89982e7e983ca8ce6a15d46e74 MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright © 2016 Magento. 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. /** @var \Magento\Framework\Acl\AclResource\ProviderInterface */
  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. // Remove any duplicates added parents
  78. $result[$integrationName][self::API_RESOURCES] =
  79. array_values(array_unique($result[$integrationName][self::API_RESOURCES]));
  80. }
  81. return $result;
  82. }
  83. /**
  84. * Make ACL resource array return a hash with parent-resource-name => [children-resources-names] representation
  85. *
  86. * @param array $resources
  87. * @return array
  88. */
  89. private function hashResources(array $resources)
  90. {
  91. $output = [];
  92. foreach ($resources as $resource) {
  93. if (isset($resource['children'])) {
  94. $item = $this->hashResources($resource['children']);
  95. } else {
  96. $item = [];
  97. }
  98. $output[$resource['id']] = $item;
  99. }
  100. return $output;
  101. }
  102. /**
  103. * Find parents names of a node in an ACL resource hash and add them to returned array
  104. *
  105. * @param array $resourcesHash
  106. * @param string $nodeName
  107. * @return array
  108. */
  109. private function addParentsToResource(array $resourcesHash, $nodeName)
  110. {
  111. $output = [];
  112. foreach ($resourcesHash as $resource => $children) {
  113. if ($resource == $nodeName) {
  114. $output = [$resource];
  115. break;
  116. }
  117. if (!empty($children)) {
  118. $names = $this->addParentsToResource($children, $nodeName);
  119. if (!empty($names)) {
  120. $output = array_merge([$resource], $names);
  121. break;
  122. } else {
  123. continue;
  124. }
  125. }
  126. }
  127. return $output;
  128. }
  129. }