diff --git a/src/Commands/State/RequestsCommand.php b/src/Commands/State/RequestsCommand.php index ef4083a7..5c50cf69 100644 --- a/src/Commands/State/RequestsCommand.php +++ b/src/Commands/State/RequestsCommand.php @@ -65,6 +65,7 @@ class RequestsCommand extends Command return self::SUCCESS; } + $queued = []; $requests = $this->cache->get('requests', []); if (count($requests) < 1) { @@ -85,6 +86,10 @@ class RequestsCommand extends Command Options::DEBUG_TRACE => $input->getOption('trace') ]); + $fn = function (iState $state) use (&$queued) { + $queued[$state->id] = $state; + }; + foreach ($requests as $request) { $entity = ag($request, 'entity'); assert($entity instanceof iState); @@ -100,9 +105,14 @@ class RequestsCommand extends Command $this->mapper->add($entity, [ Options::IMPORT_METADATA_ONLY => (bool)ag($options, Options::IMPORT_METADATA_ONLY), + Options::STATE_UPDATE_EVENT => $fn, ]); } + foreach ($queued as $item) { + queuePush($item); + } + $operations = $this->mapper->commit(); $a = [ diff --git a/src/Libs/Mappers/Import/DirectMapper.php b/src/Libs/Mappers/Import/DirectMapper.php index 4d2b2d93..daeae896 100644 --- a/src/Libs/Mappers/Import/DirectMapper.php +++ b/src/Libs/Mappers/Import/DirectMapper.php @@ -104,6 +104,7 @@ final class DirectMapper implements iImport $metadataOnly = true === (bool)ag($opts, Options::IMPORT_METADATA_ONLY); $inDryRunMode = $this->inDryRunMode(); + $onStateUpdate = ag($opts, Options::STATE_UPDATE_EVENT, null); /** * Handle adding new item logic. @@ -149,6 +150,10 @@ final class DirectMapper implements iImport $entity->id = random_int((int)(PHP_INT_MAX / 2), PHP_INT_MAX); } else { $entity = $this->db->insert($entity); + + if (null !== $onStateUpdate && true === $entity->isWatched()) { + $onStateUpdate($entity); + } } $this->logger->notice('MAPPER: [%(backend)] added [%(title)] as new item.', [ @@ -256,6 +261,10 @@ final class DirectMapper implements iImport if (false === $inDryRunMode) { $this->db->update($local); + + if (null !== $onStateUpdate) { + $onStateUpdate($local); + } } $this->logger->notice('MAPPER: [%(backend)] marked [%(title)] as unplayed.', [ @@ -351,12 +360,12 @@ final class DirectMapper implements iImport } $keys = $opts['diff_keys'] ?? array_flip( - array_keys_diff( - base: array_flip(iState::ENTITY_KEYS), - list: iState::ENTITY_IGNORE_DIFF_CHANGES, - has: false - ) - ); + array_keys_diff( + base: array_flip(iState::ENTITY_KEYS), + list: iState::ENTITY_IGNORE_DIFF_CHANGES, + has: false + ) + ); if (true === (clone $cloned)->apply(entity: $entity, fields: $keys)->isChanged(fields: $keys)) { try { @@ -373,6 +382,10 @@ final class DirectMapper implements iImport if ($cloned->isWatched() !== $local->isWatched()) { $message = 'MAPPER: [%(backend)] updated and marked [%(title)] as [%(state)].'; + + if (null !== $onStateUpdate) { + $onStateUpdate($local); + } } if (count($changes) >= 1) { diff --git a/src/Libs/Options.php b/src/Libs/Options.php index 301b0af5..c65962a1 100644 --- a/src/Libs/Options.php +++ b/src/Libs/Options.php @@ -20,6 +20,7 @@ final class Options public const MISMATCH_DEEP_SCAN = 'MISMATCH_DEEP_SCAN'; public const DISABLE_GUID = 'DISABLE_GUID'; public const LIBRARY_SEGMENT = 'LIBRARY_SEGMENT'; + public const STATE_UPDATE_EVENT = 'STATE_UPDATE_EVENT'; private function __construct() {