EngineResolver.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Search\Model;
  7. use Magento\Framework\App\Config\ScopeConfigInterface;
  8. use Magento\Framework\Search\EngineResolverInterface;
  9. use Psr\Log\LoggerInterface;
  10. /**
  11. * Search engine resolver model.
  12. *
  13. * @api
  14. * @since 100.1.0
  15. */
  16. class EngineResolver implements EngineResolverInterface
  17. {
  18. /**
  19. * MySQL search engine
  20. */
  21. const CATALOG_SEARCH_MYSQL_ENGINE = 'mysql';
  22. /**
  23. * @var ScopeConfigInterface
  24. * @since 100.1.0
  25. */
  26. protected $scopeConfig;
  27. /**
  28. * Path to catalog search engine
  29. * @var string
  30. * @since 100.1.0
  31. */
  32. protected $path;
  33. /**
  34. * Scope type
  35. * @var string
  36. * @since 100.1.0
  37. */
  38. protected $scopeType;
  39. /**
  40. * Scope code
  41. * @var null|string
  42. * @since 100.1.0
  43. */
  44. protected $scopeCode;
  45. /**
  46. * Available engines
  47. * @var array
  48. */
  49. private $engines = [];
  50. /**
  51. * @var LoggerInterface
  52. */
  53. private $logger;
  54. /**
  55. * @param ScopeConfigInterface $scopeConfig
  56. * @param array $engines
  57. * @param LoggerInterface $logger
  58. * @param string $path
  59. * @param string $scopeType
  60. * @param string $scopeCode
  61. */
  62. public function __construct(
  63. ScopeConfigInterface $scopeConfig,
  64. array $engines,
  65. LoggerInterface $logger,
  66. $path,
  67. $scopeType,
  68. $scopeCode = null
  69. ) {
  70. $this->scopeConfig = $scopeConfig;
  71. $this->path = $path;
  72. $this->scopeType = $scopeType;
  73. $this->scopeCode = $scopeCode;
  74. $this->engines = $engines;
  75. $this->logger = $logger;
  76. }
  77. /**
  78. * Returns Current Search Engine
  79. *
  80. * It returns string identifier of Search Engine that is currently chosen in configuration
  81. *
  82. * @return string
  83. * @since 100.1.0
  84. */
  85. public function getCurrentSearchEngine()
  86. {
  87. $engine = $this->scopeConfig->getValue(
  88. $this->path,
  89. $this->scopeType,
  90. $this->scopeCode
  91. );
  92. if (in_array($engine, $this->engines)) {
  93. return $engine;
  94. } else {
  95. $this->logger->error(
  96. $engine . ' search engine doesn\'t exists. Falling back to ' . self::CATALOG_SEARCH_MYSQL_ENGINE
  97. );
  98. return self::CATALOG_SEARCH_MYSQL_ENGINE;
  99. }
  100. }
  101. }