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