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

175 lines
5.0 KiB
PHP
Raw Permalink 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!
***************************************************************/
2021-04-24 04:44:44 +02:00
use MeiliSearch\Client;
use MeiliSearch\Exceptions\CommunicationException;
2021-04-17 00:26:33 +02:00
use WapplerSystems\Meilisearch\System\Configuration\TypoScriptConfiguration;
2021-04-17 21:20:54 +02:00
use WapplerSystems\Meilisearch\System\Logging\MeilisearchLogManager;
2021-04-24 04:44:44 +02:00
use WapplerSystems\Meilisearch\System\Meilisearch\MeilisearchConnection;
2021-04-17 00:26:33 +02:00
use WapplerSystems\Meilisearch\Util;
use TYPO3\CMS\Core\Utility\GeneralUtility;
2021-04-17 21:20:54 +02:00
abstract class AbstractMeilisearchService
2021-04-17 00:26:33 +02:00
{
/**
* @var TypoScriptConfiguration
*/
protected $configuration;
/**
2021-04-17 21:20:54 +02:00
* @var \WapplerSystems\Meilisearch\System\Logging\MeilisearchLogManager
2021-04-17 00:26:33 +02:00
*/
protected $logger = null;
/**
* @var Client
*/
protected $client = null;
2021-04-24 04:44:44 +02:00
/**
* @var MeilisearchConnection
*/
protected $meilisearchConnection = null;
2021-04-17 00:26:33 +02:00
/**
2021-04-17 21:20:54 +02:00
* MeilisearchReadService constructor.
2021-04-17 00:26:33 +02:00
*/
2021-04-24 04:44:44 +02:00
public function __construct(MeilisearchConnection $meilisearchConnection, Client $client, $typoScriptConfiguration = null, $logManager = null)
2021-04-17 00:26:33 +02:00
{
2021-04-24 04:44:44 +02:00
$this->meilisearchConnection = $meilisearchConnection;
2021-04-17 00:26:33 +02:00
$this->client = $client;
2021-04-17 21:20:54 +02:00
$this->configuration = $typoScriptConfiguration ?? Util::getMeilisearchConfiguration();
$this->logger = $logManager ?? GeneralUtility::makeInstance(MeilisearchLogManager::class, /** @scrutinizer ignore-type */ __CLASS__);
2021-04-17 00:26:33 +02:00
}
/**
* Returns the Solarium client
*
* @return ?Client
*/
public function getClient(): ?Client
{
return $this->client;
}
/**
2021-04-24 04:44:44 +02:00
* @return MeilisearchConnection
2021-04-17 00:26:33 +02:00
*/
2021-04-24 04:44:44 +02:00
public function getMeilisearchConnection(): ?MeilisearchConnection
2021-04-17 00:26:33 +02:00
{
2021-04-24 04:44:44 +02:00
return $this->meilisearchConnection;
2021-04-17 00:26:33 +02:00
}
/**
*
*/
public function __toString()
{
2021-04-24 04:44:44 +02:00
$siteConfiguration = $this->meilisearchConnection->getSiteConfiguration();
2021-04-29 18:33:05 +02:00
$strConnection = $siteConfiguration['scheme'].'://'.$siteConfiguration['host'].':'.$siteConfiguration['port'];
2021-04-17 00:26:33 +02:00
2021-04-24 04:44:44 +02:00
if (!$this->ping()) return $strConnection;
2021-04-17 00:26:33 +02:00
2021-04-29 18:33:05 +02:00
return $strConnection . ', Version: ' . $this->client->version()['pkgVersion'];
2021-04-17 00:26:33 +02:00
}
/**
* Build the log data and writes the message to the log
*
* @param integer $logSeverity
* @param string $message
* @param string $url
2021-04-17 21:20:54 +02:00
* @param ResponseAdapter $meilisearchResponse
2021-04-17 00:26:33 +02:00
* @param ?\Exception $exception
* @param string $contentSend
*/
2021-04-17 21:20:54 +02:00
protected function writeLog($logSeverity, $message, $url, $meilisearchResponse, $exception = null, $contentSend = '')
2021-04-17 00:26:33 +02:00
{
2021-04-17 21:20:54 +02:00
$logData = $this->buildLogDataFromResponse($meilisearchResponse, $exception, $url, $contentSend);
2021-04-17 00:26:33 +02:00
$this->logger->log($logSeverity, $message, $logData);
}
/**
2021-04-17 21:20:54 +02:00
* Parses the meilisearch information to build data for the logger.
2021-04-17 00:26:33 +02:00
*
2021-04-17 21:20:54 +02:00
* @param ResponseAdapter $meilisearchResponse
2021-04-17 00:26:33 +02:00
* @param ?\Exception $e
* @param string $url
* @param string $contentSend
* @return array
*/
2021-04-17 21:20:54 +02:00
protected function buildLogDataFromResponse(ResponseAdapter $meilisearchResponse, \Exception $e = null, $url = '', $contentSend = '')
2021-04-17 00:26:33 +02:00
{
2021-04-17 21:20:54 +02:00
$logData = ['query url' => $url, 'response' => (array)$meilisearchResponse];
2021-04-17 00:26:33 +02:00
if ($contentSend !== '') {
$logData['content'] = $contentSend;
}
if (!empty($e)) {
$logData['exception'] = $e->__toString();
return $logData;
}
2021-04-24 04:44:44 +02:00
// trigger data parsing
// @extensionScannerIgnoreLine
$meilisearchResponse->response;
$logData['response data'] = print_r($meilisearchResponse, true);
return $logData;
2021-04-17 00:26:33 +02:00
}
2021-04-24 04:44:44 +02:00
2021-04-17 00:26:33 +02:00
/**
2021-04-24 04:44:44 +02:00
* @return bool
2021-04-17 00:26:33 +02:00
*/
2021-04-24 04:44:44 +02:00
public function ping()
2021-04-17 00:26:33 +02:00
{
try {
2021-04-24 04:44:44 +02:00
$health = $this->client->health();
} catch (CommunicationException $ex) {
return false;
2021-04-17 00:26:33 +02:00
}
2021-04-24 04:44:44 +02:00
return is_array($health) && $health['status'] === 'available';
2021-04-17 00:26:33 +02:00
}
/**
* Returns the current time in milliseconds.
*
* @return double
*/
protected function getMilliseconds()
{
return GeneralUtility::milliseconds();
}
}