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:
Daniel Siepmann 2018-03-01 08:03:51 +01:00
parent fef2bdac89
commit 2998c43ba8
Signed by: Daniel Siepmann
GPG key ID: 33D6629915560EF4
9 changed files with 98 additions and 47 deletions

View file

@ -25,7 +25,7 @@ namespace Codappix\SearchCore\DataProcessing;
*/ */
class CopyToProcessor implements ProcessorInterface class CopyToProcessor implements ProcessorInterface
{ {
public function processRecord(array $record, array $configuration) : array public function processData(array $record, array $configuration) : array
{ {
$all = []; $all = [];

View file

@ -25,7 +25,7 @@ namespace Codappix\SearchCore\DataProcessing;
*/ */
class GeoPointProcessor implements ProcessorInterface 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)) { if (! $this->canApply($record, $configuration)) {
return $record; return $record;

View file

@ -21,14 +21,13 @@ namespace Codappix\SearchCore\DataProcessing;
*/ */
/** /**
* All DataProcessing Processors should implement this interface, otherwise they * All DataProcessing Processors should implement this interface.
* will not be executed.
*/ */
interface ProcessorInterface interface ProcessorInterface
{ {
/** /**
* Processes the given record. * Processes the given data.
* Also retrieves the configuration for this processor instance. * Also retrieves the configuration for this processor instance.
*/ */
public function processRecord(array $record, array $configuration) : array; public function processData(array $record, array $configuration) : array;
} }

View file

@ -27,7 +27,7 @@ use TYPO3\CMS\Core\Utility\GeneralUtility;
*/ */
class RemoveProcessor implements ProcessorInterface class RemoveProcessor implements ProcessorInterface
{ {
public function processRecord(array $record, array $configuration) : array public function processData(array $record, array $configuration) : array
{ {
if (!isset($configuration['fields'])) { if (!isset($configuration['fields'])) {
return $record; return $record;

View 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);
}
}

View file

@ -43,6 +43,12 @@ abstract class AbstractIndexer implements IndexerInterface
*/ */
protected $identifier = ''; protected $identifier = '';
/**
* @var \Codappix\SearchCore\DataProcessing\Service
* @inject
*/
protected $dataProcessorService;
/** /**
* @var \TYPO3\CMS\Core\Log\Logger * @var \TYPO3\CMS\Core\Log\Logger
*/ */
@ -134,17 +140,7 @@ abstract class AbstractIndexer implements IndexerInterface
{ {
try { try {
foreach ($this->configuration->get('indexing.' . $this->identifier . '.dataProcessing') as $configuration) { foreach ($this->configuration->get('indexing.' . $this->identifier . '.dataProcessing') as $configuration) {
$className = ''; $record = $this->dataProcessorService->executeDataProcessor($configuration, $record);
if (is_string($configuration)) {
$className = $configuration;
$configuration = [];
} else {
$className = $configuration['_typoScriptNodeValue'];
}
$dataProcessor = GeneralUtility::makeInstance($className);
if ($dataProcessor instanceof ProcessorInterface) {
$record = $dataProcessor->processRecord($record, $configuration);
}
} }
} catch (InvalidArgumentException $e) { } catch (InvalidArgumentException $e) {
// Nothing to do. // Nothing to do.

View file

@ -27,15 +27,15 @@ class CopyToProcessorTest extends AbstractUnitTestCase
{ {
/** /**
* @test * @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(); $subject = new CopyToProcessor();
$processedRecord = $subject->processRecord($record, $configuration); $processedData = $subject->processData($record, $configuration);
$this->assertSame( $this->assertSame(
$expectedRecord, $expectedData,
$processedRecord, $processedData,
'The processor did not return the expected processed record.' 'The processor did not return the expected processed record.'
); );
} }
@ -43,7 +43,7 @@ class CopyToProcessorTest extends AbstractUnitTestCase
/** /**
* @return array * @return array
*/ */
public function getPossibleRecordConfigurationCombinations() public function getPossibleDataConfigurationCombinations()
{ {
return [ return [
'Copy all fields to new field' => [ 'Copy all fields to new field' => [
@ -54,7 +54,7 @@ class CopyToProcessorTest extends AbstractUnitTestCase
'configuration' => [ 'configuration' => [
'to' => 'new_field', 'to' => 'new_field',
], ],
'expectedRecord' => [ 'expectedData' => [
'field 1' => 'Some content like lorem', 'field 1' => 'Some content like lorem',
'field 2' => 'Some more content like ipsum', 'field 2' => 'Some more content like ipsum',
'new_field' => 'Some content like lorem' . PHP_EOL . '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' => [ 'configuration' => [
'to' => 'new_field', 'to' => 'new_field',
], ],
'expectedRecord' => [ 'expectedData' => [
'field 1' => 'Some content like lorem', 'field 1' => 'Some content like lorem',
'field with sub2' => [ 'field with sub2' => [
'Tag 1', 'Tag 1',

View file

@ -27,15 +27,15 @@ class GeoPointProcessorTest extends AbstractUnitTestCase
{ {
/** /**
* @test * @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(); $subject = new GeoPointProcessor();
$processedRecord = $subject->processRecord($record, $configuration); $processedData = $subject->processData($record, $configuration);
$this->assertSame( $this->assertSame(
$expectedRecord, $expectedData,
$processedRecord, $processedData,
'The processor did not return the expected processed record.' 'The processor did not return the expected processed record.'
); );
} }
@ -43,7 +43,7 @@ class GeoPointProcessorTest extends AbstractUnitTestCase
/** /**
* @return array * @return array
*/ */
public function getPossibleRecordConfigurationCombinations() public function getPossibleDataConfigurationCombinations()
{ {
return [ return [
'Create new field with existing lat and lng' => [ 'Create new field with existing lat and lng' => [
@ -56,7 +56,7 @@ class GeoPointProcessorTest extends AbstractUnitTestCase
'lat' => 'lat', 'lat' => 'lat',
'lon' => 'lng', 'lon' => 'lng',
], ],
'expectedRecord' => [ 'expectedData' => [
'lat' => 23.232, 'lat' => 23.232,
'lng' => 45.43, 'lng' => 45.43,
'location' => [ 'location' => [
@ -73,7 +73,7 @@ class GeoPointProcessorTest extends AbstractUnitTestCase
'configuration' => [ 'configuration' => [
'to' => 'location', 'to' => 'location',
], ],
'expectedRecord' => [ 'expectedData' => [
'lat' => 23.232, 'lat' => 23.232,
'lng' => 45.43, 'lng' => 45.43,
], ],
@ -88,7 +88,7 @@ class GeoPointProcessorTest extends AbstractUnitTestCase
'lat' => 'lat', 'lat' => 'lat',
'lon' => 'lng', 'lon' => 'lng',
], ],
'expectedRecord' => [ 'expectedData' => [
'lat' => '', 'lat' => '',
'lng' => '', 'lng' => '',
], ],
@ -103,7 +103,7 @@ class GeoPointProcessorTest extends AbstractUnitTestCase
'lat' => 'lat', 'lat' => 'lat',
'lon' => 'lng', 'lon' => 'lng',
], ],
'expectedRecord' => [ 'expectedData' => [
'lat' => 'av', 'lat' => 'av',
'lng' => 'dsf', 'lng' => 'dsf',
], ],

View file

@ -27,15 +27,15 @@ class RemoveProcessorTest extends AbstractUnitTestCase
{ {
/** /**
* @test * @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(); $subject = new RemoveProcessor();
$processedRecord = $subject->processRecord($record, $configuration); $processedData = $subject->processData($record, $configuration);
$this->assertSame( $this->assertSame(
$expectedRecord, $expectedData,
$processedRecord, $processedData,
'The processor did not return the expected processed record.' 'The processor did not return the expected processed record.'
); );
} }
@ -43,7 +43,7 @@ class RemoveProcessorTest extends AbstractUnitTestCase
/** /**
* @return array * @return array
*/ */
public function getPossibleRecordConfigurationCombinations() public function getPossibleDataConfigurationCombinations()
{ {
return [ return [
'Nothing configured' => [ 'Nothing configured' => [
@ -56,7 +56,7 @@ class RemoveProcessorTest extends AbstractUnitTestCase
], ],
'configuration' => [ 'configuration' => [
], ],
'expectedRecord' => [ 'expectedData' => [
'field 1' => 'Some content like lorem', 'field 1' => 'Some content like lorem',
'field with sub2' => [ 'field with sub2' => [
'Tag 1', 'Tag 1',
@ -76,7 +76,7 @@ class RemoveProcessorTest extends AbstractUnitTestCase
'fields' => 'field with sub2', 'fields' => 'field with sub2',
'_typoScriptNodeValue' => 'Codappix\SearchCore\DataProcessing\RemoveProcessor', '_typoScriptNodeValue' => 'Codappix\SearchCore\DataProcessing\RemoveProcessor',
], ],
'expectedRecord' => [ 'expectedData' => [
'field 1' => 'Some content like lorem', 'field 1' => 'Some content like lorem',
], ],
], ],
@ -92,7 +92,7 @@ class RemoveProcessorTest extends AbstractUnitTestCase
'fields' => 'non existing', 'fields' => 'non existing',
'_typoScriptNodeValue' => 'Codappix\SearchCore\DataProcessing\RemoveProcessor', '_typoScriptNodeValue' => 'Codappix\SearchCore\DataProcessing\RemoveProcessor',
], ],
'expectedRecord' => [ 'expectedData' => [
'field 1' => 'Some content like lorem', 'field 1' => 'Some content like lorem',
'field with sub2' => [ 'field with sub2' => [
'Tag 1', 'Tag 1',
@ -113,7 +113,7 @@ class RemoveProcessorTest extends AbstractUnitTestCase
'fields' => 'field 3, field with sub2', 'fields' => 'field 3, field with sub2',
'_typoScriptNodeValue' => 'Codappix\SearchCore\DataProcessing\RemoveProcessor', '_typoScriptNodeValue' => 'Codappix\SearchCore\DataProcessing\RemoveProcessor',
], ],
'expectedRecord' => [ 'expectedData' => [
'field 1' => 'Some content like lorem', 'field 1' => 'Some content like lorem',
], ],
], ],
@ -125,7 +125,7 @@ class RemoveProcessorTest extends AbstractUnitTestCase
'fields' => 'field 1', 'fields' => 'field 1',
'_typoScriptNodeValue' => 'Codappix\SearchCore\DataProcessing\RemoveProcessor', '_typoScriptNodeValue' => 'Codappix\SearchCore\DataProcessing\RemoveProcessor',
], ],
'expectedRecord' => [ 'expectedData' => [
], ],
], ],
]; ];