Commit c76d757d authored by Simon Gadient's avatar Simon Gadient

[RFT] Separation of frontend mapping functionality

Separate the frontend mapping functionality and persist the mapping
configuration for simpler and more consistent usage. This allowed to
exclude the additional properties from the SpreadsheetImportService and
move it to the controllers for the individual implementations.

refs KIME-4583
parent 5207e2db
...@@ -11,42 +11,76 @@ namespace WE\SpreadsheetImport; ...@@ -11,42 +11,76 @@ namespace WE\SpreadsheetImport;
* The TYPO3 project - inspiring people to share! * * The TYPO3 project - inspiring people to share! *
* */ * */
use TYPO3\Flow\Annotations as Flow;
use TYPO3\Flow\Mvc\ActionRequest; use TYPO3\Flow\Mvc\ActionRequest;
use WE\SpreadsheetImport\Annotations\Mapping; use WE\SpreadsheetImport\Annotations\Mapping;
/** /**
* Utility class of basic FE mapping functionality for simple usage on separate implementations. * Service class of basic FE mapping functionality for simple usage on separate implementations.
*
* @Flow\Scope("singleton")
*/ */
class FrontendMappingUtility { class FrontendMappingService {
/** /**
* @param \WE\SpreadsheetImport\SpreadsheetImportService $spreadsheetImportService * @Flow\Inject
* @var \WE\SpreadsheetImport\SpreadsheetImportService
*/
protected $spreadsheetImportService;
/**
* @Flow\InjectConfiguration
* @var array
*/
protected $settings;
/**
* @param string $context
* @param \TYPO3\Flow\Mvc\ActionRequest $request * @param \TYPO3\Flow\Mvc\ActionRequest $request
* *
* @return array * @return array
*/ */
public static function getSpreadsheetImportMappingByRequest(SpreadsheetImportService $spreadsheetImportService, ActionRequest $request) { public function getContextArgumentsForRequest($context, ActionRequest $request) {
$arguments = array();
$contextArguments = $this->settings[$context]['arguments'];
if (is_array($contextArguments)) {
foreach ($contextArguments as $contextArgument) {
$name = $contextArgument['name'];
$default = isset($contextArgument['default']) ? $contextArgument['default'] : NULL;
$arguments[$name] = $request->hasArgument($name) ? $request->getArgument($name) : $default;
}
}
return $arguments;
}
/**
* @param array $mappingProperties
* @param array $columns
*
* @return array
*/
public function getSpreadsheetImportMapping($mappingProperties, $columns = array()) {
$mappings = array(); $mappings = array();
$domainMappingProperties = $spreadsheetImportService->getMappingProperties(); foreach ($mappingProperties as $property => $mapping) {
foreach ($domainMappingProperties as $property => $mapping) { $column = isset($columns[$property]) ? $columns[$property] : '';
$columnMapping = array('column' => $request->getArgument($property), 'mapping' => $mapping); $columnMapping = array('column' => $column, 'mapping' => $mapping);
$mappings[$property] = $columnMapping; $mappings[$property] = $columnMapping;
} }
return $mappings; return $mappings;
} }
/** /**
* @param \WE\SpreadsheetImport\SpreadsheetImportService $spreadsheetImportService * @param array $mapping
* @param int $record * @param int $record
* *
* @return array * @return array
*/ */
public static function getMappingPreview(SpreadsheetImportService $spreadsheetImportService, $record) { public function getMappingPreview($mapping, $record) {
$domainMappingProperties = $spreadsheetImportService->getMappingProperties(); $previewObject = $this->spreadsheetImportService->getObjectByRow($record);
$previewObject = $spreadsheetImportService->getObjectByRow($record);
$preview = array(); $preview = array();
/** @var Mapping $mapping */ foreach ($mapping as $property => $columnMapping) {
foreach ($domainMappingProperties as $property => $mapping) { /** @var Mapping $mapping */
$mapping = $columnMapping['mapping'];
$getter = empty($mapping->getter) ? 'get' . ucfirst($property) : $mapping->getter; $getter = empty($mapping->getter) ? 'get' . ucfirst($property) : $mapping->getter;
$preview[$property] = $previewObject->$getter(); $preview[$property] = $previewObject->$getter();
} }
......
...@@ -20,6 +20,7 @@ use WE\SpreadsheetImport\Domain\Model\SpreadsheetImport; ...@@ -20,6 +20,7 @@ use WE\SpreadsheetImport\Domain\Model\SpreadsheetImport;
* @Flow\Scope("singleton") * @Flow\Scope("singleton")
*/ */
class SpreadsheetImportService { class SpreadsheetImportService {
/** /**
* @var SpreadsheetImport * @var SpreadsheetImport
*/ */
...@@ -30,16 +31,6 @@ class SpreadsheetImportService { ...@@ -30,16 +31,6 @@ class SpreadsheetImportService {
*/ */
protected $domain; protected $domain;
/**
* @var array
*/
protected $mappingProperties;
/**
* @var array
*/
protected $inverseSpreadsheetImportMapping;
/** /**
* @Flow\InjectConfiguration * @Flow\InjectConfiguration
* @var array * @var array
...@@ -76,6 +67,13 @@ class SpreadsheetImportService { ...@@ -76,6 +67,13 @@ class SpreadsheetImportService {
*/ */
protected $validatorResolver; protected $validatorResolver;
/**
* Inverse SpreadsheetImport mapping array
*
* @var array
*/
private $inverseSpreadsheetImportMapping;
/** /**
* @param \WE\SpreadsheetImport\Domain\Model\SpreadsheetImport $spreadsheetImport * @param \WE\SpreadsheetImport\Domain\Model\SpreadsheetImport $spreadsheetImport
* *
...@@ -84,65 +82,20 @@ class SpreadsheetImportService { ...@@ -84,65 +82,20 @@ class SpreadsheetImportService {
public function init(SpreadsheetImport $spreadsheetImport) { public function init(SpreadsheetImport $spreadsheetImport) {
$this->spreadsheetImport = $spreadsheetImport; $this->spreadsheetImport = $spreadsheetImport;
$this->domain = $this->settings[$spreadsheetImport->getContext()]['domain']; $this->domain = $this->settings[$spreadsheetImport->getContext()]['domain'];
$this->initDomainMappingProperties();
return $this; return $this;
} }
/** /**
* Initializes the properties declared by annotations. * @return array
*/ */
private function initDomainMappingProperties() { public function getAnnotationMappingProperties() {
$this->mappingProperties = array(); $mappingPropertyAnnotations = array();
$properties = $this->reflectionService->getPropertyNamesByAnnotation($this->domain, Mapping::class); $properties = $this->reflectionService->getPropertyNamesByAnnotation($this->domain, Mapping::class);
foreach ($properties as $property) { foreach ($properties as $property) {
$this->mappingProperties[$property] = $this->reflectionService->getPropertyAnnotation($this->domain, $property, Mapping::class); $mappingPropertyAnnotations[$property] = $this->reflectionService->getPropertyAnnotation($this->domain, $property, Mapping::class);
} }
} return $mappingPropertyAnnotations;
/**
* Return an inverse SpreadsheetImport mapping array. It flips the property and column attribute and returns it as a
* 3-dim array instead of a 2-dim array. The reason for that is the case when the same column is assigned to multiple
* properties.
*/
private function getInverseSpreadsheetImportMapping() {
if (empty($this->inverseSpreadsheetImportMapping)) {
$this->inverseSpreadsheetImportMapping = array();
foreach ($this->spreadsheetImport->getMapping() as $property => $columnMapping) {
$column = $columnMapping['column'];
$propertyMapping = array('property' => $property, 'mapping' => $columnMapping['mapping']);
$this->inverseSpreadsheetImportMapping[$column][] = $propertyMapping;
}
}
return $this->inverseSpreadsheetImportMapping;
}
/**
* Adds additional mapping properties to the domain mapping properties retrieved by annotations. This increases
* flexibility for dynamic property mapping.
*
* This is implemented for the single use case to support the Flow package Radmiraal.CouchDB
*
* @param array $additionalMappingProperties
*/
public function addAdditionalMappingProperties(array $additionalMappingProperties) {
$this->mappingProperties = array_merge($this->mappingProperties, $additionalMappingProperties);
}
/**
* @return array
*/
public function getMappingProperties() {
return $this->mappingProperties;
}
/**
* @param string $context
*
* @return array
*/
public function getArgumentsByContext($context) {
return $this->settings[$context]['arguments'];
} }
/** /**
...@@ -348,11 +301,28 @@ class SpreadsheetImportService { ...@@ -348,11 +301,28 @@ class SpreadsheetImportService {
$this->setObjectArgumentProperties($object); $this->setObjectArgumentProperties($object);
} }
/**
* Return an inverse SpreadsheetImport mapping array. It flips the property and column attribute and returns it as a
* 3-dim array instead of a 2-dim array. The reason for that is the case when the same column is assigned to multiple
* properties.
*/
private function getInverseSpreadsheetImportMapping() {
if (empty($this->inverseSpreadsheetImportMapping)) {
$this->inverseSpreadsheetImportMapping = array();
foreach ($this->spreadsheetImport->getMapping() as $property => $columnMapping) {
$column = $columnMapping['column'];
$propertyMapping = array('property' => $property, 'mapping' => $columnMapping['mapping']);
$this->inverseSpreadsheetImportMapping[$column][] = $propertyMapping;
}
}
return $this->inverseSpreadsheetImportMapping;
}
/** /**
* @param $object * @param $object
*/ */
private function setObjectArgumentProperties($object) { private function setObjectArgumentProperties($object) {
$contextArguments = $this->getArgumentsByContext($this->spreadsheetImport->getContext()); $contextArguments = $this->settings[$this->spreadsheetImport->getContext()]['arguments'];
if (is_array($contextArguments)) { if (is_array($contextArguments)) {
$arguments = $this->spreadsheetImport->getArguments(); $arguments = $this->spreadsheetImport->getArguments();
foreach ($contextArguments as $contextArgument) { foreach ($contextArguments as $contextArgument) {
......
...@@ -72,7 +72,7 @@ class SpreadsheetImportServiceTest extends FunctionalTestCase { ...@@ -72,7 +72,7 @@ class SpreadsheetImportServiceTest extends FunctionalTestCase {
* @test * @test
*/ */
public function getMappingPropertiesReturnsPropertiesWithMappingAnnotation() { public function getMappingPropertiesReturnsPropertiesWithMappingAnnotation() {
$properties = $this->spreadsheetImportService->getMappingProperties(); $properties = $this->spreadsheetImportService->getAnnotationMappingProperties();
$this->assertArrayHasKey('id', $properties); $this->assertArrayHasKey('id', $properties);
$this->assertArrayHasKey('name', $properties); $this->assertArrayHasKey('name', $properties);
/** @var Mapping $id */ /** @var Mapping $id */
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment