Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 79 additions & 16 deletions lib/Service/MappingService.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,6 @@
use OCA\OpenConnector\Twig\MappingExtension;
use OCA\OpenConnector\Twig\MappingRuntimeLoader;
use OCA\OpenRegister\Service\FileService;
use OCP\Files\IRootFolder;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
//use Twig\Environment;
//use Twig\Error\LoaderError;
//use Twig\Error\SyntaxError;
use Adbar\Dot;
use Twig\Environment;
use Twig\Error\LoaderError;
Expand All @@ -24,6 +18,21 @@
use Throwable;
use Exception;

/**
* Mapping service that delegates core execution to OpenRegister's MappingService
* when available, falling back to its own implementation.
*
* @deprecated The mapping engine has moved to OpenRegister. This service delegates
* to OCA\OpenRegister\Service\MappingService for executeMapping().
*
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
* @SuppressWarnings(PHPMD.ExcessiveClassLength)
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.NPathComplexity)
* @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
* @SuppressWarnings(PHPMD.ExcessiveMethodLength) Mapping execution requires comprehensive handling
* @SuppressWarnings(PHPMD.BooleanArgumentFlag) $list parameter clearly indicates list processing mode
*/
class MappingService
{
/**
Expand All @@ -33,11 +42,22 @@ class MappingService
*/
private Environment $twig;

/**
* The OpenRegister mapping service (if available).
*
* @var \OCA\OpenRegister\Service\MappingService|null
*/
private $openRegisterMappingService = null;

/**
* Setting up the base class with required services.
*
* @param ArrayLoader $loader The ArrayLoader for Twig.
* @param MappingMapper $mappingMapper The mapping mapper.
* @param CallService $callService The call service.
* @param SourceMapper $sourceMapper The source mapper.
* @param FileService $fileService The file service.
* @param ObjectService $objectService The object service.
*/
public function __construct(
ArrayLoader $loader,
Expand All @@ -51,6 +71,20 @@ public function __construct(
$this->twig->addExtension(new MappingExtension());
$this->twig->addRuntimeLoader(new MappingRuntimeLoader(mappingService: $this, mappingMapper: $this->mappingMapper, callService: $callService, sourceMapper: $sourceMapper, fileService: $fileService, objectService: $objectService->getOpenRegisters()));

// Try to load OpenRegister's MappingService for delegation.
try {
$container = \OC::$server;
$this->openRegisterMappingService = $container->get(
\OCA\OpenRegister\Service\MappingService::class
);
} catch (\Throwable $e) {
// OpenRegister not available, falling back to local implementation.
$this->openRegisterMappingService = null;
\OC::$server->getLogger()->info(
'OpenConnector MappingService: OpenRegister not available, using local implementation',
['app' => 'openconnector']
);
}
}//end __construct()

/**
Expand Down Expand Up @@ -83,6 +117,9 @@ public function encodeArrayKeys(array $array, string $toReplace, string $replace
/**
* Maps (transforms) an array (input) to a different array (output).
*
* Delegates to OpenRegister's MappingService when available, otherwise
* uses the local implementation.
*
* @param Mapping $mapping The mapping object that forms the recipe for the mapping
* @param array $input The array that need to be mapped (transformed) otherwise known as input
* @param bool $list Whether we want a list instead of a single item
Expand All @@ -93,7 +130,42 @@ public function encodeArrayKeys(array $array, string $toReplace, string $replace
*/
public function executeMapping(Mapping $mapping, array $input, bool $list = false): array
{
// Delegate to OpenRegister's MappingService if available.
if ($this->openRegisterMappingService !== null) {
$orMapping = new \OCA\OpenRegister\Db\Mapping();
$orMapping->hydrate([
'name' => $mapping->getName(),
'mapping' => $mapping->getMapping(),
'unset' => ($mapping->getUnset() ?? []),
'cast' => ($mapping->getCast() ?? []),
'passThrough' => $mapping->getPassThrough(),
]);

return $this->openRegisterMappingService->executeMapping(
mapping: $orMapping,
input: $input,
list: $list
);
}

return $this->executeMappingLocal($mapping, $input, $list);
}//end executeMapping()

/**
* Local mapping execution (fallback when OpenRegister is not available).
*
* @param Mapping $mapping The mapping object
* @param array $input The input array
* @param bool $list Whether to process as list
*
* @return array The mapped output
*
* @throws Exception When mapping fails
*
* @SuppressWarnings(PHPMD.ElseExpression)
*/
private function executeMappingLocal(Mapping $mapping, array $input, bool $list = false): array
{
// Check for list
if ($list === true) {
$list = [];
Expand Down Expand Up @@ -122,16 +194,12 @@ public function executeMapping(Mapping $mapping, array $input, bool $list = fals
$originalInput = $input;
$input = $this->encodeArrayKeys($input, '.', '.');

// @todo: error logging

// Determine pass through.
// Let's get the dot array based on https://github.com/adbario/php-dot-notation.
if ($mapping->getPassThrough()) {
$dotArray = new Dot($input);
// @todo: error logging
} else {
$dotArray = new Dot();
// @todo: error logging
}
$dotInput = new Dot($input);

Expand Down Expand Up @@ -160,7 +228,6 @@ public function executeMapping(Mapping $mapping, array $input, bool $list = fals
$unsets = ($mapping->getUnset() ?? []);
foreach ($unsets as $unset) {
if ($dotArray->has($unset) === false) {
// @todo: error logging
continue;
}

Expand All @@ -172,7 +239,6 @@ public function executeMapping(Mapping $mapping, array $input, bool $list = fals

foreach ($casts as $key => $cast) {
if ($dotArray->has($key) === false) {
// @todo: error logging
continue;
}

Expand All @@ -181,7 +247,6 @@ public function executeMapping(Mapping $mapping, array $input, bool $list = fals
}

if ($cast === false) {
// @todo: error logging
continue;
}

Expand Down Expand Up @@ -214,7 +279,7 @@ public function executeMapping(Mapping $mapping, array $input, bool $list = fals

return $output;

}//end mapping()
}//end executeMappingLocal()

/**
* Handles a single cast.
Expand Down Expand Up @@ -376,8 +441,6 @@ private function handleCast(Dot $dotArray, string $key, string $cast)
$value = number_format($value, 2, ',', '.');
break;
default:
// @todo: error handling
//isset($this->style) === true && $this->style->info('Trying to cast to an unsupported cast type: '.$cast);
break;
}//end switch

Expand Down
Loading