Config.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Webapi\Model\Soap;
  7. use Magento\Webapi\Model\ServiceMetadata;
  8. /**
  9. * Webapi Config Model for Soap.
  10. *
  11. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  12. */
  13. class Config
  14. {
  15. /**
  16. * @var \Magento\Framework\ObjectManagerInterface
  17. */
  18. protected $objectManager;
  19. /**
  20. * List of SOAP operations available in the system
  21. *
  22. * @var array
  23. */
  24. protected $soapOperations;
  25. /**
  26. * @var \Magento\Framework\Registry
  27. */
  28. protected $registry;
  29. /**
  30. * @var \Magento\Webapi\Model\ServiceMetadata
  31. */
  32. protected $serviceMetadata;
  33. /**
  34. * Initialize dependencies.
  35. *
  36. * @param \Magento\Framework\ObjectManagerInterface $objectManager
  37. * @param \Magento\Framework\Registry $registry
  38. * @param \Magento\Webapi\Model\ServiceMetadata $serviceMetadata
  39. */
  40. public function __construct(
  41. \Magento\Framework\ObjectManagerInterface $objectManager,
  42. \Magento\Framework\Registry $registry,
  43. \Magento\Webapi\Model\ServiceMetadata $serviceMetadata
  44. ) {
  45. $this->objectManager = $objectManager;
  46. $this->registry = $registry;
  47. $this->serviceMetadata = $serviceMetadata;
  48. }
  49. /**
  50. * Retrieve the list of SOAP operations available in the system
  51. *
  52. * @param array $requestedServices The list of requested services with their versions
  53. * @return array <pre>
  54. * array(
  55. * array(
  56. * 'class' => $serviceClass,
  57. * 'method' => $serviceMethod
  58. * 'isSecure' => $isSecure
  59. * ),
  60. * ...
  61. * )</pre>
  62. */
  63. protected function getSoapOperations($requestedServices)
  64. {
  65. if (null == $this->soapOperations) {
  66. $this->soapOperations = [];
  67. foreach ($this->getRequestedSoapServices($requestedServices) as $serviceName => $serviceData) {
  68. foreach ($serviceData[ServiceMetadata::KEY_SERVICE_METHODS] as $methodData) {
  69. $method = $methodData[ServiceMetadata::KEY_METHOD];
  70. $class = $serviceData[ServiceMetadata::KEY_CLASS];
  71. $operationName = $serviceName . ucfirst($method);
  72. $this->soapOperations[$operationName] = [
  73. ServiceMetadata::KEY_CLASS => $class,
  74. ServiceMetadata::KEY_METHOD => $method,
  75. ServiceMetadata::KEY_IS_SECURE => $methodData[ServiceMetadata::KEY_IS_SECURE],
  76. ServiceMetadata::KEY_ACL_RESOURCES => $methodData[ServiceMetadata::KEY_ACL_RESOURCES],
  77. ];
  78. }
  79. }
  80. }
  81. return $this->soapOperations;
  82. }
  83. /**
  84. * Retrieve service method information, including service class, method name, and isSecure attribute value.
  85. *
  86. * @param string $soapOperation
  87. * @param array $requestedServices The list of requested services with their versions
  88. * @return array
  89. * @throws \Magento\Framework\Webapi\Exception
  90. */
  91. public function getServiceMethodInfo($soapOperation, $requestedServices)
  92. {
  93. $soapOperations = $this->getSoapOperations($requestedServices);
  94. if (!isset($soapOperations[$soapOperation])) {
  95. throw new \Magento\Framework\Webapi\Exception(
  96. __('Operation "%1" not found.', $soapOperation),
  97. 0,
  98. \Magento\Framework\Webapi\Exception::HTTP_NOT_FOUND
  99. );
  100. }
  101. return [
  102. ServiceMetadata::KEY_CLASS => $soapOperations[$soapOperation][ServiceMetadata::KEY_CLASS],
  103. ServiceMetadata::KEY_METHOD => $soapOperations[$soapOperation][ServiceMetadata::KEY_METHOD],
  104. ServiceMetadata::KEY_IS_SECURE => $soapOperations[$soapOperation][ServiceMetadata::KEY_IS_SECURE],
  105. ServiceMetadata::KEY_ACL_RESOURCES => $soapOperations[$soapOperation][ServiceMetadata::KEY_ACL_RESOURCES]
  106. ];
  107. }
  108. /**
  109. * Retrieve the list of services corresponding to specified services and their versions.
  110. *
  111. * @param array $requestedServices array('FooBarV1', 'OtherBazV2', ...)
  112. * @return array Filtered list of services
  113. */
  114. public function getRequestedSoapServices(array $requestedServices)
  115. {
  116. $services = [];
  117. $soapServicesConfig = $this->serviceMetadata->getServicesConfig();
  118. foreach ($requestedServices as $serviceName) {
  119. if (isset($soapServicesConfig[$serviceName])) {
  120. $services[$serviceName] = $soapServicesConfig[$serviceName];
  121. }
  122. }
  123. return $services;
  124. }
  125. /**
  126. * Generate SOAP operation name.
  127. *
  128. * @param string $interfaceName e.g. \Magento\Catalog\Api\ProductInterfaceV1
  129. * @param string $methodName e.g. create
  130. * @param string $version
  131. * @return string e.g. catalogProductCreate
  132. */
  133. public function getSoapOperation($interfaceName, $methodName, $version)
  134. {
  135. $serviceName = $this->serviceMetadata->getServiceName($interfaceName, $version);
  136. $operationName = $serviceName . ucfirst($methodName);
  137. return $operationName;
  138. }
  139. }