mirror of
https://github.com/Codappix/search_core.git
synced 2024-12-22 19:16:11 +01:00
TASK: Refactor data processing
Use own service to handle data processing. Classes like indexer should not know anything about the structure and how to process the data. Also rename record to data, as we can process just any data in form of an array. Relates: #116
This commit is contained in:
parent
fef2bdac89
commit
2998c43ba8
9 changed files with 98 additions and 47 deletions
|
@ -25,7 +25,7 @@ namespace Codappix\SearchCore\DataProcessing;
|
|||
*/
|
||||
class CopyToProcessor implements ProcessorInterface
|
||||
{
|
||||
public function processRecord(array $record, array $configuration) : array
|
||||
public function processData(array $record, array $configuration) : array
|
||||
{
|
||||
$all = [];
|
||||
|
||||
|
|
|
@ -25,7 +25,7 @@ namespace Codappix\SearchCore\DataProcessing;
|
|||
*/
|
||||
class GeoPointProcessor implements ProcessorInterface
|
||||
{
|
||||
public function processRecord(array $record, array $configuration) : array
|
||||
public function processData(array $record, array $configuration) : array
|
||||
{
|
||||
if (! $this->canApply($record, $configuration)) {
|
||||
return $record;
|
||||
|
|
|
@ -21,14 +21,13 @@ namespace Codappix\SearchCore\DataProcessing;
|
|||
*/
|
||||
|
||||
/**
|
||||
* All DataProcessing Processors should implement this interface, otherwise they
|
||||
* will not be executed.
|
||||
* All DataProcessing Processors should implement this interface.
|
||||
*/
|
||||
interface ProcessorInterface
|
||||
{
|
||||
/**
|
||||
* Processes the given record.
|
||||
* Processes the given data.
|
||||
* Also retrieves the configuration for this processor instance.
|
||||
*/
|
||||
public function processRecord(array $record, array $configuration) : array;
|
||||
public function processData(array $record, array $configuration) : array;
|
||||
}
|
||||
|
|
|
@ -27,7 +27,7 @@ use TYPO3\CMS\Core\Utility\GeneralUtility;
|
|||
*/
|
||||
class RemoveProcessor implements ProcessorInterface
|
||||
{
|
||||
public function processRecord(array $record, array $configuration) : array
|
||||
public function processData(array $record, array $configuration) : array
|
||||
{
|
||||
if (!isset($configuration['fields'])) {
|
||||
return $record;
|
||||
|
|
56
Classes/DataProcessing/Service.php
Normal file
56
Classes/DataProcessing/Service.php
Normal file
|
@ -0,0 +1,56 @@
|
|||
<?php
|
||||
namespace Codappix\SearchCore\DataProcessing;
|
||||
|
||||
/*
|
||||
* Copyright (C) 2018 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 TYPO3\CMS\Extbase\Object\ObjectManagerInterface;
|
||||
|
||||
/**
|
||||
* Eases work with data processing.
|
||||
*/
|
||||
class Service
|
||||
{
|
||||
/**
|
||||
* @var ObjectManagerInterface
|
||||
*/
|
||||
protected $objectManager;
|
||||
|
||||
public function __construct(ObjectManagerInterface $objectManager)
|
||||
{
|
||||
$this->objectManager = $objectManager;
|
||||
}
|
||||
|
||||
/**
|
||||
* Executes the dataprocessor depending on configuration and returns the result.
|
||||
*
|
||||
* @param array|string $configuration Either the full configuration or only the class name.
|
||||
*/
|
||||
public function executeDataProcessor($configuration, array $data) : array
|
||||
{
|
||||
if (is_string($configuration)) {
|
||||
$configuration = [
|
||||
'_typoScriptNodeValue' => $configuration,
|
||||
];
|
||||
}
|
||||
|
||||
return $this->objectManager->get($configuration['_typoScriptNodeValue'])
|
||||
->processData($data, $configuration);
|
||||
}
|
||||
}
|
|
@ -43,6 +43,12 @@ abstract class AbstractIndexer implements IndexerInterface
|
|||
*/
|
||||
protected $identifier = '';
|
||||
|
||||
/**
|
||||
* @var \Codappix\SearchCore\DataProcessing\Service
|
||||
* @inject
|
||||
*/
|
||||
protected $dataProcessorService;
|
||||
|
||||
/**
|
||||
* @var \TYPO3\CMS\Core\Log\Logger
|
||||
*/
|
||||
|
@ -134,17 +140,7 @@ abstract class AbstractIndexer implements IndexerInterface
|
|||
{
|
||||
try {
|
||||
foreach ($this->configuration->get('indexing.' . $this->identifier . '.dataProcessing') as $configuration) {
|
||||
$className = '';
|
||||
if (is_string($configuration)) {
|
||||
$className = $configuration;
|
||||
$configuration = [];
|
||||
} else {
|
||||
$className = $configuration['_typoScriptNodeValue'];
|
||||
}
|
||||
$dataProcessor = GeneralUtility::makeInstance($className);
|
||||
if ($dataProcessor instanceof ProcessorInterface) {
|
||||
$record = $dataProcessor->processRecord($record, $configuration);
|
||||
}
|
||||
$record = $this->dataProcessorService->executeDataProcessor($configuration, $record);
|
||||
}
|
||||
} catch (InvalidArgumentException $e) {
|
||||
// Nothing to do.
|
||||
|
|
|
@ -27,15 +27,15 @@ class CopyToProcessorTest extends AbstractUnitTestCase
|
|||
{
|
||||
/**
|
||||
* @test
|
||||
* @dataProvider getPossibleRecordConfigurationCombinations
|
||||
* @dataProvider getPossibleDataConfigurationCombinations
|
||||
*/
|
||||
public function fieldsAreCopiedAsConfigured(array $record, array $configuration, array $expectedRecord)
|
||||
public function fieldsAreCopiedAsConfigured(array $record, array $configuration, array $expectedData)
|
||||
{
|
||||
$subject = new CopyToProcessor();
|
||||
$processedRecord = $subject->processRecord($record, $configuration);
|
||||
$processedData = $subject->processData($record, $configuration);
|
||||
$this->assertSame(
|
||||
$expectedRecord,
|
||||
$processedRecord,
|
||||
$expectedData,
|
||||
$processedData,
|
||||
'The processor did not return the expected processed record.'
|
||||
);
|
||||
}
|
||||
|
@ -43,7 +43,7 @@ class CopyToProcessorTest extends AbstractUnitTestCase
|
|||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getPossibleRecordConfigurationCombinations()
|
||||
public function getPossibleDataConfigurationCombinations()
|
||||
{
|
||||
return [
|
||||
'Copy all fields to new field' => [
|
||||
|
@ -54,7 +54,7 @@ class CopyToProcessorTest extends AbstractUnitTestCase
|
|||
'configuration' => [
|
||||
'to' => 'new_field',
|
||||
],
|
||||
'expectedRecord' => [
|
||||
'expectedData' => [
|
||||
'field 1' => 'Some content like lorem',
|
||||
'field 2' => 'Some more content like ipsum',
|
||||
'new_field' => 'Some content like lorem' . PHP_EOL . 'Some more content like ipsum',
|
||||
|
@ -71,7 +71,7 @@ class CopyToProcessorTest extends AbstractUnitTestCase
|
|||
'configuration' => [
|
||||
'to' => 'new_field',
|
||||
],
|
||||
'expectedRecord' => [
|
||||
'expectedData' => [
|
||||
'field 1' => 'Some content like lorem',
|
||||
'field with sub2' => [
|
||||
'Tag 1',
|
||||
|
|
|
@ -27,15 +27,15 @@ class GeoPointProcessorTest extends AbstractUnitTestCase
|
|||
{
|
||||
/**
|
||||
* @test
|
||||
* @dataProvider getPossibleRecordConfigurationCombinations
|
||||
* @dataProvider getPossibleDataConfigurationCombinations
|
||||
*/
|
||||
public function geoPointsAreAddedAsConfigured(array $record, array $configuration, array $expectedRecord)
|
||||
public function geoPointsAreAddedAsConfigured(array $record, array $configuration, array $expectedData)
|
||||
{
|
||||
$subject = new GeoPointProcessor();
|
||||
$processedRecord = $subject->processRecord($record, $configuration);
|
||||
$processedData = $subject->processData($record, $configuration);
|
||||
$this->assertSame(
|
||||
$expectedRecord,
|
||||
$processedRecord,
|
||||
$expectedData,
|
||||
$processedData,
|
||||
'The processor did not return the expected processed record.'
|
||||
);
|
||||
}
|
||||
|
@ -43,7 +43,7 @@ class GeoPointProcessorTest extends AbstractUnitTestCase
|
|||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getPossibleRecordConfigurationCombinations()
|
||||
public function getPossibleDataConfigurationCombinations()
|
||||
{
|
||||
return [
|
||||
'Create new field with existing lat and lng' => [
|
||||
|
@ -56,7 +56,7 @@ class GeoPointProcessorTest extends AbstractUnitTestCase
|
|||
'lat' => 'lat',
|
||||
'lon' => 'lng',
|
||||
],
|
||||
'expectedRecord' => [
|
||||
'expectedData' => [
|
||||
'lat' => 23.232,
|
||||
'lng' => 45.43,
|
||||
'location' => [
|
||||
|
@ -73,7 +73,7 @@ class GeoPointProcessorTest extends AbstractUnitTestCase
|
|||
'configuration' => [
|
||||
'to' => 'location',
|
||||
],
|
||||
'expectedRecord' => [
|
||||
'expectedData' => [
|
||||
'lat' => 23.232,
|
||||
'lng' => 45.43,
|
||||
],
|
||||
|
@ -88,7 +88,7 @@ class GeoPointProcessorTest extends AbstractUnitTestCase
|
|||
'lat' => 'lat',
|
||||
'lon' => 'lng',
|
||||
],
|
||||
'expectedRecord' => [
|
||||
'expectedData' => [
|
||||
'lat' => '',
|
||||
'lng' => '',
|
||||
],
|
||||
|
@ -103,7 +103,7 @@ class GeoPointProcessorTest extends AbstractUnitTestCase
|
|||
'lat' => 'lat',
|
||||
'lon' => 'lng',
|
||||
],
|
||||
'expectedRecord' => [
|
||||
'expectedData' => [
|
||||
'lat' => 'av',
|
||||
'lng' => 'dsf',
|
||||
],
|
||||
|
|
|
@ -27,15 +27,15 @@ class RemoveProcessorTest extends AbstractUnitTestCase
|
|||
{
|
||||
/**
|
||||
* @test
|
||||
* @dataProvider getPossibleRecordConfigurationCombinations
|
||||
* @dataProvider getPossibleDataConfigurationCombinations
|
||||
*/
|
||||
public function fieldsAreCopiedAsConfigured(array $record, array $configuration, array $expectedRecord)
|
||||
public function fieldsAreCopiedAsConfigured(array $record, array $configuration, array $expectedData)
|
||||
{
|
||||
$subject = new RemoveProcessor();
|
||||
$processedRecord = $subject->processRecord($record, $configuration);
|
||||
$processedData = $subject->processData($record, $configuration);
|
||||
$this->assertSame(
|
||||
$expectedRecord,
|
||||
$processedRecord,
|
||||
$expectedData,
|
||||
$processedData,
|
||||
'The processor did not return the expected processed record.'
|
||||
);
|
||||
}
|
||||
|
@ -43,7 +43,7 @@ class RemoveProcessorTest extends AbstractUnitTestCase
|
|||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getPossibleRecordConfigurationCombinations()
|
||||
public function getPossibleDataConfigurationCombinations()
|
||||
{
|
||||
return [
|
||||
'Nothing configured' => [
|
||||
|
@ -56,7 +56,7 @@ class RemoveProcessorTest extends AbstractUnitTestCase
|
|||
],
|
||||
'configuration' => [
|
||||
],
|
||||
'expectedRecord' => [
|
||||
'expectedData' => [
|
||||
'field 1' => 'Some content like lorem',
|
||||
'field with sub2' => [
|
||||
'Tag 1',
|
||||
|
@ -76,7 +76,7 @@ class RemoveProcessorTest extends AbstractUnitTestCase
|
|||
'fields' => 'field with sub2',
|
||||
'_typoScriptNodeValue' => 'Codappix\SearchCore\DataProcessing\RemoveProcessor',
|
||||
],
|
||||
'expectedRecord' => [
|
||||
'expectedData' => [
|
||||
'field 1' => 'Some content like lorem',
|
||||
],
|
||||
],
|
||||
|
@ -92,7 +92,7 @@ class RemoveProcessorTest extends AbstractUnitTestCase
|
|||
'fields' => 'non existing',
|
||||
'_typoScriptNodeValue' => 'Codappix\SearchCore\DataProcessing\RemoveProcessor',
|
||||
],
|
||||
'expectedRecord' => [
|
||||
'expectedData' => [
|
||||
'field 1' => 'Some content like lorem',
|
||||
'field with sub2' => [
|
||||
'Tag 1',
|
||||
|
@ -113,7 +113,7 @@ class RemoveProcessorTest extends AbstractUnitTestCase
|
|||
'fields' => 'field 3, field with sub2',
|
||||
'_typoScriptNodeValue' => 'Codappix\SearchCore\DataProcessing\RemoveProcessor',
|
||||
],
|
||||
'expectedRecord' => [
|
||||
'expectedData' => [
|
||||
'field 1' => 'Some content like lorem',
|
||||
],
|
||||
],
|
||||
|
@ -125,7 +125,7 @@ class RemoveProcessorTest extends AbstractUnitTestCase
|
|||
'fields' => 'field 1',
|
||||
'_typoScriptNodeValue' => 'Codappix\SearchCore\DataProcessing\RemoveProcessor',
|
||||
],
|
||||
'expectedRecord' => [
|
||||
'expectedData' => [
|
||||
],
|
||||
],
|
||||
];
|
||||
|
|
Loading…
Reference in a new issue