Messages.php in Acquia Search 3.x
File
src/Helper/Messages.php
View source
<?php
namespace Drupal\acquia_search\Helper;
use Drupal\acquia_search\Plugin\SolrConnector\SearchApiSolrAcquiaConnector;
use Drupal\Core\Render\Markup;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\search_api\Entity\Server;
class Messages {
public static function showReadOnlyModeWarning() {
$message = Messages::getReadOnlyModeWarning();
\Drupal::messenger()
->addWarning($message);
}
public static function showNoPreferredCoreError() {
$message = Messages::getNoPreferredCoreError();
\Drupal::messenger()
->addWarning(Markup::create($message));
}
public static function getNoPreferredCoreError() : string {
$possible_cores = Runtime::getPreferredSearchCoreService()
->getListOfPossibleCores();
$messages[] = t('Could not find a Solr core corresponding to your website and environment.');
if (!empty($possible_cores)) {
$messages[] = t('These cores were expected but not found in your subscription: @list.', [
'@list' => implode(', ', $possible_cores),
]);
}
$available_cores = Runtime::getPreferredSearchCoreService()
->getListOfAvailableCores();
if (!empty($available_cores)) {
$messages[] = t('Your subscription contains these cores: @list.', [
'@list' => implode(', ', $available_cores),
]);
}
else {
$messages[] = t('Your subscription contains no cores.');
}
$messages[] = t('To fix this problem, please read <a href="@url">our documentation</a>.', [
'@url' => 'https://docs.acquia.com/acquia-search/multiple-cores/',
]);
return implode(' ', $messages);
}
public static function getReadOnlyModeWarning() : string {
return (string) t('The read-only mode is set in the configuration of the Acquia Search Solr module.');
}
public static function getSearchStatusMessage(Server $server) {
$backend = $server
->getBackend();
$configuration = $backend
->getSolrConnector()
->getConfiguration();
$items[] = Messages::getServerIdMessage($server
->id());
if (Runtime::getPreferredSearchCoreService()
->isPreferredCoreAvailable()) {
$items[] = Messages::getServerUrlMessage($configuration);
if (isset($configuration['overridden_by_acquia_search'])) {
$items[] = Messages::getOverriddenModeMessage($configuration['overridden_by_acquia_search']);
}
$items[] = Messages::getServerAvailabilityMessage($server);
$items[] = Messages::getServerAuthCheckMessage($server);
}
else {
$items[] = [
'#markup' => '<span class="color-error">' . Messages::getNoPreferredCoreError() . '</span>',
];
}
$list = [
'#theme' => 'item_list',
'#items' => $items,
];
$list = \Drupal::service('renderer')
->renderRoot($list);
$msg = t('Connection managed by Acquia Search Solr module.') . $list;
return Markup::create((string) $msg);
}
public static function getOverriddenModeMessage(int $override) {
switch ($override) {
case SearchApiSolrAcquiaConnector::READ_ONLY:
return [
'#markup' => '<span class="color-warning">' . t('Acquia Search Solr module automatically enforced read-only mode on this connection.') . '</span>',
];
case SearchApiSolrAcquiaConnector::OVERRIDE_AUTO_SET:
return t('Acquia Search Solr module automatically selected the proper Solr connection based on the detected environment and configuration.');
}
}
public static function getServerUrlMessage(array $configuration) : TranslatableMarkup {
if (empty($configuration['host'])) {
$url = t('N/A');
}
else {
$url = $configuration['scheme'] . '://' . $configuration['host'] . ':' . $configuration['port'] . $configuration['path'];
}
return t('URL: @url', [
'@url' => $url,
]);
}
public static function getServerIdMessage($server_id) : TranslatableMarkup {
return t('search_api_solr.module server ID: @id', [
'@id' => $server_id,
]);
}
public static function getServerAvailabilityMessage(Server $server) {
if ($server
->getBackend()
->getSolrConnector()
->pingCore()) {
return t('Solr core is currently reachable and up.');
}
return [
'#markup' => '<span class="color-error">' . t('Solr core is currently unreachable.') . '</span>',
];
}
public static function getServerAuthCheckMessage(Server $server) {
if ($server
->getBackend()
->getSolrConnector()
->pingServer()) {
return t('Requests to Solr core are passing authentication checks.');
}
return [
'#markup' => '<span class="color-error">' . t('Solr core authentication check fails.') . '</span>',
];
}
}