Adapter.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Search\Adapter\Mysql;
  7. use Magento\Framework\App\ResourceConnection;
  8. use Magento\Framework\DB\Ddl\Table;
  9. use Magento\Framework\Search\Adapter\Mysql\Aggregation\Builder as AggregationBuilder;
  10. use Magento\Framework\Search\AdapterInterface;
  11. use Magento\Framework\Search\RequestInterface;
  12. /**
  13. * MySQL Search Adapter
  14. *
  15. * @deprecated 102.0.0
  16. * @see \Magento\ElasticSearch
  17. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  18. */
  19. class Adapter implements AdapterInterface
  20. {
  21. /**
  22. * Mapper instance
  23. *
  24. * @var Mapper
  25. */
  26. protected $mapper;
  27. /**
  28. * Response Factory
  29. *
  30. * @var ResponseFactory
  31. */
  32. protected $responseFactory;
  33. /**
  34. * @var \Magento\Framework\App\ResourceConnection
  35. */
  36. private $resource;
  37. /**
  38. * @var AggregationBuilder
  39. */
  40. private $aggregationBuilder;
  41. /**
  42. * @var TemporaryStorageFactory
  43. */
  44. private $temporaryStorageFactory;
  45. /**
  46. * @param Mapper $mapper
  47. * @param ResponseFactory $responseFactory
  48. * @param ResourceConnection $resource
  49. * @param AggregationBuilder $aggregationBuilder
  50. * @param TemporaryStorageFactory $temporaryStorageFactory
  51. */
  52. public function __construct(
  53. Mapper $mapper,
  54. ResponseFactory $responseFactory,
  55. ResourceConnection $resource,
  56. AggregationBuilder $aggregationBuilder,
  57. TemporaryStorageFactory $temporaryStorageFactory
  58. ) {
  59. $this->mapper = $mapper;
  60. $this->responseFactory = $responseFactory;
  61. $this->resource = $resource;
  62. $this->aggregationBuilder = $aggregationBuilder;
  63. $this->temporaryStorageFactory = $temporaryStorageFactory;
  64. }
  65. /**
  66. * @inheritdoc
  67. * @throws \LogicException
  68. */
  69. public function query(RequestInterface $request)
  70. {
  71. $query = $this->mapper->buildQuery($request);
  72. $temporaryStorage = $this->temporaryStorageFactory->create();
  73. $table = $temporaryStorage->storeDocumentsFromSelect($query);
  74. $documents = $this->getDocuments($table);
  75. $aggregations = $this->aggregationBuilder->build($request, $table, $documents);
  76. $response = [
  77. 'documents' => $documents,
  78. 'aggregations' => $aggregations,
  79. ];
  80. return $this->responseFactory->create($response);
  81. }
  82. /**
  83. * Executes query and return raw response
  84. *
  85. * @param Table $table
  86. * @return array
  87. * @throws \Zend_Db_Exception
  88. */
  89. private function getDocuments(Table $table)
  90. {
  91. $connection = $this->getConnection();
  92. $select = $connection->select();
  93. $select->from($table->getName(), ['entity_id', 'score']);
  94. return $connection->fetchAssoc($select);
  95. }
  96. /**
  97. * Get connection.
  98. *
  99. * @return false|\Magento\Framework\DB\Adapter\AdapterInterface
  100. */
  101. private function getConnection()
  102. {
  103. return $this->resource->getConnection();
  104. }
  105. }