10
10
use App \Libs \Mappers \ImportInterface as iImport ;
11
11
use App \Libs \Message ;
12
12
use App \Libs \Options ;
13
+ use DateInterval ;
13
14
use DateTimeInterface as iDate ;
14
15
use Exception ;
15
16
use PDOException ;
16
17
use Psr \Log \LoggerInterface as iLogger ;
18
+ use Psr \SimpleCache \CacheInterface ;
19
+ use Psr \SimpleCache \InvalidArgumentException ;
17
20
18
21
final class DirectMapper implements iImport
19
22
{
@@ -43,8 +46,12 @@ final class DirectMapper implements iImport
43
46
protected array $ options = [];
44
47
45
48
protected bool $ fullyLoaded = false ;
49
+ /**
50
+ * @var array<string,iState> List of items with play progress.
51
+ */
52
+ protected array $ progressItems = [];
46
53
47
- public function __construct (protected iLogger $ logger , protected iDB $ db )
54
+ public function __construct (protected iLogger $ logger , protected iDB $ db, protected CacheInterface $ cache )
48
55
{
49
56
}
50
57
@@ -297,8 +304,12 @@ public function add(iState $entity, array $opts = []): self
297
304
return $ this ;
298
305
}
299
306
307
+ $ newPlayProgress = (int )ag ($ entity ->getMetadata ($ entity ->via ), iState::COLUMN_META_DATA_PROGRESS );
308
+ $ oldPlayProgress = (int )ag ($ cloned ->getMetadata ($ entity ->via ), iState::COLUMN_META_DATA_PROGRESS );
309
+ $ playChanged = $ newPlayProgress != $ oldPlayProgress ;
310
+
300
311
// -- this sometimes leads to never ending updates as data from backends conflicts.
301
- if (true === (bool )ag ($ this ->options , Options::MAPPER_ALWAYS_UPDATE_META )) {
312
+ if ($ playChanged || true === (bool )ag ($ this ->options , Options::MAPPER_ALWAYS_UPDATE_META )) {
302
313
if (true === (clone $ cloned )->apply (entity: $ entity , fields: $ keys )->isChanged (fields: $ keys )) {
303
314
try {
304
315
$ local = $ local ->apply (
@@ -311,16 +322,30 @@ public function add(iState $entity, array $opts = []): self
311
322
$ changes = $ local ->diff (fields: $ keys );
312
323
313
324
if (count ($ changes ) >= 1 ) {
314
- $ this ->logger ->notice ('MAPPER: [{backend}] updated [{title}] metadata. ' , [
315
- 'id ' => $ cloned ->id ,
316
- 'backend ' => $ entity ->via ,
317
- 'title ' => $ cloned ->getName (),
318
- 'changes ' => $ changes ,
319
- ]);
325
+ $ this ->logger ->notice (
326
+ $ playChanged ? 'MAPPER: [{backend}] updated [{title}] due to play progress change. ' : 'MAPPER: [{backend}] updated [{title}] metadata. ' ,
327
+ [
328
+ 'id ' => $ cloned ->id ,
329
+ 'backend ' => $ entity ->via ,
330
+ 'title ' => $ cloned ->getName (),
331
+ 'changes ' => $ changes ,
332
+ ]
333
+ );
320
334
}
321
335
322
336
if (false === $ inDryRunMode ) {
323
337
$ this ->db ->update ($ local );
338
+
339
+ if (true === $ entity ->hasPlayProgress ()) {
340
+ $ itemId = r ('{type}://{id}:{tainted}@{backend} ' , [
341
+ 'type ' => $ entity ->type ,
342
+ 'backend ' => $ entity ->via ,
343
+ 'tainted ' => $ entity ->isTainted () ? 'tainted ' : 'untainted ' ,
344
+ 'id ' => ag ($ entity ->getMetadata ($ entity ->via ), iState::COLUMN_ID , '?? ' ),
345
+ ]);
346
+
347
+ $ this ->progressItems [$ itemId ] = $ entity ;
348
+ }
324
349
}
325
350
326
351
if (null === ($ this ->changed [$ local ->id ] ?? null )) {
@@ -510,8 +535,19 @@ public function remove(iState $entity): bool
510
535
return $ this ->db ->remove ($ entity );
511
536
}
512
537
513
- public function commit (): mixed
538
+ public function commit (): array
514
539
{
540
+ if (count ($ this ->progressItems ) >= 1 ) {
541
+ try {
542
+ $ progress = $ this ->cache ->get ('progress ' , []);
543
+ foreach ($ this ->progressItems as $ itemId => $ entity ) {
544
+ $ progress [$ itemId ] = $ entity ;
545
+ }
546
+ $ this ->cache ->set ('progress ' , $ progress , new DateInterval ('P1D ' ));
547
+ } catch (InvalidArgumentException ) {
548
+ }
549
+ }
550
+
515
551
$ list = $ this ->actions ;
516
552
517
553
$ this ->reset ();
@@ -532,7 +568,7 @@ public function reset(): self
532
568
];
533
569
534
570
$ this ->fullyLoaded = false ;
535
- $ this ->changed = $ this ->objects = $ this ->pointers = [];
571
+ $ this ->changed = $ this ->objects = $ this ->pointers = $ this -> progressItems = [];
536
572
537
573
return $ this ;
538
574
}
0 commit comments