meilisearch/Classes/System/Meilisearch/Service/MeilisearchReadService.php

121 lines
3.9 KiB
PHP
Raw Normal View History

2021-04-17 00:26:33 +02:00
<?php
2021-04-17 21:20:54 +02:00
namespace WapplerSystems\Meilisearch\System\Meilisearch\Service;
2021-04-17 00:26:33 +02:00
/***************************************************************
* Copyright notice
*
* (c) 2009-2017 Timo Hund <timo.hund@dkd.de>
* All rights reserved
*
* This script is part of the TYPO3 project. The TYPO3 project 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 3 of the License, or
* (at your option) any later version.
*
* The GNU General Public License can be found at
* http://www.gnu.org/copyleft/gpl.html.
*
* This script 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.
*
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
use WapplerSystems\Meilisearch\Domain\Search\Query\Query;
2021-04-17 21:20:54 +02:00
use WapplerSystems\Meilisearch\System\Meilisearch\ResponseAdapter;
use WapplerSystems\Meilisearch\System\Meilisearch\MeilisearchCommunicationException;
use WapplerSystems\Meilisearch\System\Meilisearch\MeilisearchInternalServerErrorException;
use WapplerSystems\Meilisearch\System\Meilisearch\MeilisearchUnavailableException;
2021-04-17 00:26:33 +02:00
use Solarium\Exception\HttpException;
/**
2021-04-17 21:20:54 +02:00
* Class MeilisearchReadService
2021-04-17 00:26:33 +02:00
*/
2021-04-17 21:20:54 +02:00
class MeilisearchReadService extends AbstractMeilisearchService
2021-04-17 00:26:33 +02:00
{
/**
* @var bool
*/
protected $hasSearched = false;
/**
* @var ResponseAdapter
*/
protected $responseCache = null;
/**
* Performs a search.
*
* @param Query $query
2021-04-17 21:20:54 +02:00
* @return ResponseAdapter Meilisearch response
* @throws \RuntimeException if Meilisearch returns a HTTP status code other than 200
2021-04-17 00:26:33 +02:00
*/
public function search($query)
{
try {
$request = $this->client->createRequest($query);
$response = $this->executeRequest($request);
$this->hasSearched = true;
$this->responseCache = $response;
} catch (HttpException $e) {
$this->handleErrorResponses($e);
}
return $response;
}
/**
* Returns whether a search has been executed or not.
*
* @return bool TRUE if a search has been executed, FALSE otherwise
*/
public function hasSearched()
{
return $this->hasSearched;
}
/**
* Gets the most recent response (if any)
*
* @return ResponseAdapter Most recent response, or NULL if a search has not been executed yet.
*/
public function getResponse()
{
return $this->responseCache;
}
/**
2021-04-17 21:20:54 +02:00
* This method maps the failed meilisearch requests to a meaningful exception.
2021-04-17 00:26:33 +02:00
*
* @param HttpException $exception
2021-04-17 21:20:54 +02:00
* @throws MeilisearchCommunicationException
2021-04-17 00:26:33 +02:00
* @return HttpException
*/
protected function handleErrorResponses(HttpException $exception)
{
$status = $exception->getCode();
$message = $exception->getStatusMessage();
2021-04-17 21:20:54 +02:00
$meilisearchRespone = new ResponseAdapter($exception->getBody());
2021-04-17 00:26:33 +02:00
if ($status === 0 || $status === 502) {
2021-04-17 21:20:54 +02:00
$e = new MeilisearchUnavailableException('Meilisearch Server not available: ' . $message, 1505989391);
$e->setMeilisearchResponse($meilisearchRespone);
2021-04-17 00:26:33 +02:00
throw $e;
}
if ($status === 500) {
2021-04-17 21:20:54 +02:00
$e = new MeilisearchInternalServerErrorException('Internal Server error during search: ' . $message, 1505989897);
$e->setMeilisearchResponse($meilisearchRespone);
2021-04-17 00:26:33 +02:00
throw $e;
}
2021-04-17 21:20:54 +02:00
$e = new MeilisearchCommunicationException('Invalid query. Meilisearch returned an error: ' . $status . ' ' . $message, 1293109870);
$e->setMeilisearchResponse($meilisearchRespone);
2021-04-17 00:26:33 +02:00
throw $e;
}
}