Webapi.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\WebapiAsync\Plugin\Cache;
  8. use Magento\WebapiAsync\Controller\Rest\AsynchronousSchemaRequestProcessor;
  9. use Magento\Framework\Webapi\Rest\Request;
  10. /**
  11. * Class Webapi
  12. */
  13. class Webapi
  14. {
  15. /**
  16. * Cache key for Async Routes
  17. */
  18. const ASYNC_ROUTES_CONFIG_CACHE_ID = 'async-routes-services-config';
  19. /**
  20. * @var AsynchronousSchemaRequestProcessor
  21. */
  22. private $asynchronousSchemaRequestProcessor;
  23. /**
  24. * @var \Magento\Framework\Webapi\Rest\Request
  25. */
  26. private $request;
  27. /**
  28. * ServiceMetadata constructor.
  29. *
  30. * @param Request $request
  31. * @param AsynchronousSchemaRequestProcessor $asynchronousSchemaRequestProcessor
  32. */
  33. public function __construct(
  34. \Magento\Framework\Webapi\Rest\Request $request,
  35. AsynchronousSchemaRequestProcessor $asynchronousSchemaRequestProcessor
  36. ) {
  37. $this->request = $request;
  38. $this->asynchronousSchemaRequestProcessor = $asynchronousSchemaRequestProcessor;
  39. }
  40. /**
  41. * Change identifier in case if Async request before cache load
  42. *
  43. * @param \Magento\Webapi\Model\Cache\Type\Webapi $subject
  44. * @param string $identifier
  45. * @return null|string
  46. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  47. */
  48. public function beforeLoad(\Magento\Webapi\Model\Cache\Type\Webapi $subject, $identifier)
  49. {
  50. if ($this->asynchronousSchemaRequestProcessor->canProcess($this->request)
  51. && $identifier === \Magento\Webapi\Model\ServiceMetadata::ROUTES_CONFIG_CACHE_ID) {
  52. return self::ASYNC_ROUTES_CONFIG_CACHE_ID;
  53. }
  54. return null;
  55. }
  56. /**
  57. * Change identifier in case if Async request before cache save
  58. *
  59. * @param \Magento\Webapi\Model\Cache\Type\Webapi $subject
  60. * @param string $data
  61. * @param string $identifier
  62. * @param array $tags
  63. * @param int|bool|null $lifeTime
  64. * @return array|null
  65. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  66. */
  67. public function beforeSave(
  68. \Magento\Webapi\Model\Cache\Type\Webapi $subject,
  69. $data,
  70. $identifier,
  71. array $tags = [],
  72. $lifeTime = null
  73. ) {
  74. if ($this->asynchronousSchemaRequestProcessor->canProcess($this->request)
  75. && $identifier === \Magento\Webapi\Model\ServiceMetadata::ROUTES_CONFIG_CACHE_ID) {
  76. return [$data, self::ASYNC_ROUTES_CONFIG_CACHE_ID, $tags, $lifeTime];
  77. }
  78. return null;
  79. }
  80. /**
  81. * Change identifier in case if Async request before remove cache
  82. *
  83. * @param \Magento\Webapi\Model\Cache\Type\Webapi $subject
  84. * @param string $identifier
  85. * @return null|string
  86. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  87. */
  88. public function beforeRemove(\Magento\Webapi\Model\Cache\Type\Webapi $subject, $identifier)
  89. {
  90. if ($this->asynchronousSchemaRequestProcessor->canProcess($this->request)
  91. && $identifier === \Magento\Webapi\Model\ServiceMetadata::ROUTES_CONFIG_CACHE_ID) {
  92. return self::ASYNC_ROUTES_CONFIG_CACHE_ID;
  93. }
  94. return null;
  95. }
  96. }