search_core/Classes/Domain/Service/DataHandler.php

132 lines
3.8 KiB
PHP
Raw Normal View History

2016-12-09 13:19:35 +01:00
<?php
namespace Leonmrni\SearchCore\Domain\Service;
/*
* 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.
*/
use Leonmrni\SearchCore\Configuration\ConfigurationContainerInterface;
use Leonmrni\SearchCore\Domain\Index\TcaIndexer;
2016-12-09 13:19:35 +01:00
use TYPO3\CMS\Core\SingletonInterface as Singleton;
use TYPO3\CMS\Core\Utility\GeneralUtility;
2016-12-09 13:19:35 +01:00
/**
* Handles all data related things like updates, deletes and inserts.
*
* This is the place to add mappings of further parts to adjust the data before
* sending ot to connection.
*
* TODO: Probably a candidate for deletion. Currently this class makes use of
* extbase DI. We have to resolve this somehow.
*
* I think we keep it for easier testing and DI.
2016-12-09 13:19:35 +01:00
*/
class DataHandler implements Singleton
{
/**
* TODO: Only inject on first use?!
*
2016-12-09 13:19:35 +01:00
* @var \Leonmrni\SearchCore\Connection\ConnectionInterface
* @inject
*/
protected $connection;
/**
* @var \Leonmrni\SearchCore\Domain\Index\IndexerFactory
* @inject
*/
protected $indexerFactory;
/**
* @var ConfigurationContainerInterface
*/
protected $configuration;
2016-12-09 13:19:35 +01:00
/**
* @var \TYPO3\CMS\Core\Log\Logger
*/
protected $logger;
/**
* Inject log manager to get concrete logger from it.
*
* @param \TYPO3\CMS\Core\Log\LogManager $logManager
*/
public function injectLogger(\TYPO3\CMS\Core\Log\LogManager $logManager)
{
$this->logger = $logManager->getLogger(__CLASS__);
}
/**
* @param ConfigurationContainerInterface $configuration
*/
public function __construct(ConfigurationContainerInterface $configuration)
{
$this->configuration = $configuration;
}
/**
* Get all tables that are allowed for indexing.
*
* @return array<String>
*/
public function getAllowedTablesForIndexing()
{
$tables = [];
foreach ($this->configuration->get('indexing') as $tableName => $indexConfiguration) {
if ($indexConfiguration['indexer'] === TcaIndexer::class) {
$tables[] = $tableName;
continue;
}
// TODO: Support custom indexer.
// Define "interface" / option which is used.
}
return $tables;
}
2016-12-09 13:19:35 +01:00
/**
* @param string $table
* @param array $record
2016-12-09 13:19:35 +01:00
*/
public function add($table, array $record)
2016-12-09 13:19:35 +01:00
{
$this->logger->debug('Record received for add.', [$table, $record]);
$this->indexerFactory->getIndexer($table)->indexDocument($record['uid']);
2016-12-09 13:19:35 +01:00
}
/**
* @param string $table
*/
public function update($table, array $record)
2016-12-09 13:19:35 +01:00
{
$this->logger->debug('Record received for update.', [$table, $record]);
$this->indexerFactory->getIndexer($table)->indexDocument($record['uid']);
2016-12-09 13:19:35 +01:00
}
/**
* @param string $table
* @param int $identifier
2016-12-09 13:19:35 +01:00
*/
public function delete($table, $identifier)
2016-12-09 13:19:35 +01:00
{
$this->logger->debug('Record received for delete.', [$table, $identifier]);
$this->connection->deleteDocument($table, $identifier);
2016-12-09 13:19:35 +01:00
}
}