StoresData.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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\Store\Model;
  8. /**
  9. * Class that computes and stores into cache the active store ids.
  10. */
  11. class StoresData
  12. {
  13. /**
  14. * Cache tag
  15. */
  16. const CACHE_TAG = 'store_relations';
  17. /**
  18. * @var \Magento\Framework\Cache\FrontendInterface
  19. */
  20. private $cache;
  21. /**
  22. * @var \Magento\Store\Model\StoreResolver\ReaderList
  23. */
  24. private $readerList;
  25. /**
  26. * @var \Magento\Framework\Serialize\SerializerInterface
  27. */
  28. private $serializer;
  29. /**
  30. * @param \Magento\Framework\Cache\FrontendInterface $cache
  31. * @param \Magento\Store\Model\StoreResolver\ReaderList $readerList
  32. * @param \Magento\Framework\Serialize\SerializerInterface $serializer
  33. */
  34. public function __construct(
  35. \Magento\Framework\Cache\FrontendInterface $cache,
  36. \Magento\Store\Model\StoreResolver\ReaderList $readerList,
  37. \Magento\Framework\Serialize\SerializerInterface $serializer
  38. ) {
  39. $this->cache = $cache;
  40. $this->readerList = $readerList;
  41. $this->serializer = $serializer;
  42. }
  43. /**
  44. * Get stores data
  45. *
  46. * @param string $runMode
  47. * @param string|null $scopeCode
  48. * @return array
  49. */
  50. public function getStoresData(string $runMode, string $scopeCode = null) : array
  51. {
  52. $cacheKey = 'resolved_stores_' . md5($runMode . $scopeCode);
  53. $cacheData = $this->cache->load($cacheKey);
  54. if ($cacheData) {
  55. $storesData = $this->serializer->unserialize($cacheData);
  56. } else {
  57. $reader = $this->readerList->getReader($runMode);
  58. $storesData = [$reader->getAllowedStoreIds($scopeCode), $reader->getDefaultStoreId($scopeCode)];
  59. $this->cache->save(
  60. $this->serializer->serialize($storesData),
  61. $cacheKey,
  62. [
  63. self::CACHE_TAG,
  64. \Magento\Store\Model\Store::CACHE_TAG
  65. ]
  66. );
  67. }
  68. return $storesData;
  69. }
  70. }