QueryResolver.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Quote\Model;
  7. use Magento\Framework\App\ObjectManager;
  8. use Magento\Framework\Config\CacheInterface;
  9. use Magento\Framework\App\ResourceConnection\ConfigInterface;
  10. use Magento\Framework\Serialize\SerializerInterface;
  11. class QueryResolver
  12. {
  13. /**
  14. * @var array
  15. */
  16. private $data = [];
  17. /**
  18. * @var ConfigInterface
  19. */
  20. private $config;
  21. /**
  22. * @var CacheInterface
  23. */
  24. private $cache;
  25. /**
  26. * @var string
  27. */
  28. private $cacheId;
  29. /**
  30. * Cache tags
  31. *
  32. * @var array
  33. */
  34. private $cacheTags = [];
  35. /**
  36. * @var SerializerInterface
  37. */
  38. private $serializer;
  39. /**
  40. * @param ConfigInterface $config
  41. * @param CacheInterface $cache
  42. * @param string $cacheId
  43. * @param SerializerInterface $serializer
  44. */
  45. public function __construct(
  46. ConfigInterface $config,
  47. CacheInterface $cache,
  48. $cacheId = 'connection_config_cache',
  49. SerializerInterface $serializer = null
  50. ) {
  51. $this->config = $config;
  52. $this->cache = $cache;
  53. $this->cacheId = $cacheId;
  54. $this->serializer = $serializer ?: ObjectManager::getInstance()->get(SerializerInterface::class);
  55. }
  56. /**
  57. * Get flag value
  58. *
  59. * @return bool
  60. */
  61. public function isSingleQuery()
  62. {
  63. if (!isset($this->data['checkout'])) {
  64. $this->initData();
  65. }
  66. return $this->data['checkout'];
  67. }
  68. /**
  69. * Initialise data for configuration
  70. * @return void
  71. */
  72. protected function initData()
  73. {
  74. $data = $this->cache->load($this->cacheId);
  75. if (false === $data) {
  76. $singleQuery = $this->config->getConnectionName('checkout_setup') == 'default' ? true : false;
  77. $data['checkout'] = $singleQuery;
  78. $this->cache->save($this->serializer->serialize($data), $this->cacheId, $this->cacheTags);
  79. } else {
  80. $data = $this->serializer->unserialize($data);
  81. }
  82. $this->merge($data);
  83. }
  84. /**
  85. * Merge config data to the object
  86. *
  87. * @param array $config
  88. * @return void
  89. */
  90. public function merge(array $config)
  91. {
  92. $this->data = array_replace_recursive($this->data, $config);
  93. }
  94. }