2016-12-09 13:19:35 +01:00
|
|
|
<?php
|
2017-07-06 23:48:47 +02:00
|
|
|
namespace Codappix\SearchCore\Domain\Search;
|
2016-12-09 13:19:35 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (C) 2016 Daniel Siepmann <coding@daniel-siepmann.de>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
|
|
|
* 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
2017-07-06 23:48:47 +02:00
|
|
|
use Codappix\SearchCore\Configuration\ConfigurationContainerInterface;
|
|
|
|
use Codappix\SearchCore\Configuration\InvalidArgumentException;
|
|
|
|
use Codappix\SearchCore\Connection\ConnectionInterface;
|
|
|
|
use Codappix\SearchCore\Connection\SearchRequestInterface;
|
|
|
|
use Codappix\SearchCore\Connection\SearchResultInterface;
|
2018-03-06 09:04:47 +01:00
|
|
|
use Codappix\SearchCore\DataProcessing\Service as DataProcessorService;
|
2017-07-06 23:48:47 +02:00
|
|
|
use Codappix\SearchCore\Domain\Model\FacetRequest;
|
2018-03-06 09:04:47 +01:00
|
|
|
use Codappix\SearchCore\Domain\Model\SearchResult;
|
2017-10-29 17:08:33 +01:00
|
|
|
use TYPO3\CMS\Core\Utility\ArrayUtility;
|
2017-07-06 12:03:52 +02:00
|
|
|
use TYPO3\CMS\Extbase\Object\ObjectManagerInterface;
|
2016-12-09 13:19:35 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Service to process a search request.
|
|
|
|
*/
|
|
|
|
class SearchService
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var ConnectionInterface
|
|
|
|
*/
|
|
|
|
protected $connection;
|
|
|
|
|
2017-07-06 12:03:52 +02:00
|
|
|
/**
|
|
|
|
* @var ConfigurationContainerInterface
|
|
|
|
*/
|
|
|
|
protected $configuration;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var ObjectManagerInterface
|
|
|
|
*/
|
|
|
|
protected $objectManager;
|
|
|
|
|
2018-03-06 09:04:47 +01:00
|
|
|
/**
|
|
|
|
* @var DataProcessorService
|
|
|
|
*/
|
|
|
|
protected $dataProcessorService;
|
|
|
|
|
2016-12-09 13:19:35 +01:00
|
|
|
/**
|
|
|
|
* @param ConnectionInterface $connection
|
2017-07-06 12:03:52 +02:00
|
|
|
* @param ConfigurationContainerInterface $configuration
|
|
|
|
* @param ObjectManagerInterface $objectManager
|
2018-03-06 09:04:47 +01:00
|
|
|
* @param DataProcessorService $dataProcessorService
|
2016-12-09 13:19:35 +01:00
|
|
|
*/
|
2017-07-06 12:03:52 +02:00
|
|
|
public function __construct(
|
|
|
|
ConnectionInterface $connection,
|
|
|
|
ConfigurationContainerInterface $configuration,
|
2018-03-06 09:04:47 +01:00
|
|
|
ObjectManagerInterface $objectManager,
|
|
|
|
DataProcessorService $dataProcessorService
|
2017-07-06 12:03:52 +02:00
|
|
|
) {
|
2016-12-09 13:19:35 +01:00
|
|
|
$this->connection = $connection;
|
2017-07-06 12:03:52 +02:00
|
|
|
$this->configuration = $configuration;
|
|
|
|
$this->objectManager = $objectManager;
|
2018-03-06 09:04:47 +01:00
|
|
|
$this->dataProcessorService = $dataProcessorService;
|
2016-12-09 13:19:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param SearchRequestInterface $searchRequest
|
|
|
|
* @return SearchResultInterface
|
|
|
|
*/
|
|
|
|
public function search(SearchRequestInterface $searchRequest)
|
|
|
|
{
|
2017-07-18 10:44:39 +02:00
|
|
|
$this->addSize($searchRequest);
|
2017-07-06 12:03:52 +02:00
|
|
|
$this->addConfiguredFacets($searchRequest);
|
2017-09-15 21:35:52 +02:00
|
|
|
$this->addConfiguredFilters($searchRequest);
|
2017-07-06 12:03:52 +02:00
|
|
|
|
2018-03-06 09:04:47 +01:00
|
|
|
// Add connection to request to enable paginate widget support
|
|
|
|
$searchRequest->setConnection($this->connection);
|
|
|
|
$searchRequest->setSearchService($this);
|
|
|
|
|
|
|
|
return $this->processResult($this->connection->search($searchRequest));
|
2016-12-09 13:19:35 +01:00
|
|
|
}
|
2017-07-06 12:03:52 +02:00
|
|
|
|
2017-07-18 10:44:39 +02:00
|
|
|
/**
|
|
|
|
* Add configured size of search result items to request.
|
|
|
|
*
|
|
|
|
* @param SearchRequestInterface $searchRequest
|
|
|
|
*/
|
|
|
|
protected function addSize(SearchRequestInterface $searchRequest)
|
|
|
|
{
|
2017-08-08 17:07:23 +02:00
|
|
|
$searchRequest->setLimit(
|
2017-07-20 13:48:27 +02:00
|
|
|
$this->configuration->getIfExists('searching.size') ?: 10
|
|
|
|
);
|
2017-07-18 10:44:39 +02:00
|
|
|
}
|
|
|
|
|
2017-07-06 12:03:52 +02:00
|
|
|
/**
|
|
|
|
* Add facets from configuration to request.
|
|
|
|
*
|
|
|
|
* @param SearchRequestInterface $searchRequest
|
|
|
|
*/
|
|
|
|
protected function addConfiguredFacets(SearchRequestInterface $searchRequest)
|
|
|
|
{
|
|
|
|
$facetsConfig = $this->configuration->getIfExists('searching.facets');
|
|
|
|
if ($facetsConfig === null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($facetsConfig as $identifier => $facetConfig) {
|
|
|
|
$searchRequest->addFacet($this->objectManager->get(
|
|
|
|
FacetRequest::class,
|
|
|
|
$identifier,
|
2018-03-06 11:36:05 +01:00
|
|
|
$facetConfig
|
2017-07-06 12:03:52 +02:00
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
2017-09-15 21:35:52 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Add filters from configuration, e.g. flexform or TypoScript.
|
|
|
|
*
|
|
|
|
* @param SearchRequestInterface $searchRequest
|
|
|
|
*/
|
|
|
|
protected function addConfiguredFilters(SearchRequestInterface $searchRequest)
|
|
|
|
{
|
|
|
|
try {
|
2017-10-29 17:08:33 +01:00
|
|
|
$filter = $searchRequest->getFilter();
|
|
|
|
|
|
|
|
ArrayUtility::mergeRecursiveWithOverrule(
|
|
|
|
$filter,
|
|
|
|
$this->configuration->get('searching.filter'),
|
|
|
|
true,
|
|
|
|
false
|
|
|
|
);
|
|
|
|
|
|
|
|
$searchRequest->setFilter($filter);
|
2017-09-15 21:35:52 +02:00
|
|
|
} catch (InvalidArgumentException $e) {
|
|
|
|
// Nothing todo, no filter configured.
|
|
|
|
}
|
|
|
|
}
|
2018-03-06 09:04:47 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Processes the result, e.g. applies configured data processing to result.
|
|
|
|
*/
|
|
|
|
public function processResult(SearchResultInterface $searchResult) : SearchResultInterface
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
$newSearchResultItems = [];
|
|
|
|
foreach ($this->configuration->get('searching.dataProcessing') as $configuration) {
|
|
|
|
foreach ($searchResult as $resultItem) {
|
|
|
|
$newSearchResultItems[] = $this->dataProcessorService->executeDataProcessor(
|
|
|
|
$configuration,
|
|
|
|
$resultItem->getPlainData()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->objectManager->get(
|
|
|
|
SearchResult::class,
|
|
|
|
$searchResult,
|
|
|
|
$newSearchResultItems
|
|
|
|
);
|
2018-03-06 16:39:07 +01:00
|
|
|
} catch (InvalidArgumentException $e) {
|
2018-03-06 09:04:47 +01:00
|
|
|
return $searchResult;
|
|
|
|
}
|
|
|
|
}
|
2016-12-09 13:19:35 +01:00
|
|
|
}
|