9
9
use App \Libs \Mappers \ImportInterface as iImport ;
10
10
use App \Libs \Message ;
11
11
use App \Libs \Options ;
12
+ use DateInterval ;
12
13
use DateTimeInterface as iDate ;
13
14
use PDOException ;
14
15
use Psr \Log \LoggerInterface as iLogger ;
16
+ use Psr \SimpleCache \CacheInterface ;
17
+ use Psr \SimpleCache \InvalidArgumentException ;
15
18
16
19
final class MemoryMapper implements iImport
17
20
{
@@ -32,11 +35,16 @@ final class MemoryMapper implements iImport
32
35
*/
33
36
protected array $ changed = [];
34
37
38
+ /**
39
+ * @var array<int,iState> List of items with play progress.
40
+ */
41
+ protected array $ progressItems = [];
42
+
35
43
protected array $ options = [];
36
44
37
45
protected bool $ fullyLoaded = false ;
38
46
39
- public function __construct (protected iLogger $ logger , protected iDB $ db )
47
+ public function __construct (protected iLogger $ logger , protected iDB $ db, protected CacheInterface $ cache )
40
48
{
41
49
}
42
50
@@ -214,8 +222,12 @@ public function add(iState $entity, array $opts = []): self
214
222
return $ this ;
215
223
}
216
224
225
+ $ newPlayProgress = (int )ag ($ entity ->getMetadata ($ entity ->via ), iState::COLUMN_META_DATA_PROGRESS );
226
+ $ oldPlayProgress = (int )ag ($ cloned ->getMetadata ($ entity ->via ), iState::COLUMN_META_DATA_PROGRESS );
227
+ $ playChanged = $ newPlayProgress != $ oldPlayProgress ;
228
+
217
229
// -- this sometimes leads to never ending updates as data from backends conflicts.
218
- if (true === (bool )ag ($ this ->options , Options::MAPPER_ALWAYS_UPDATE_META )) {
230
+ if ($ playChanged || true === (bool )ag ($ this ->options , Options::MAPPER_ALWAYS_UPDATE_META )) {
219
231
if (true === (clone $ cloned )->apply (entity: $ entity , fields: $ keys )->isChanged (fields: $ keys )) {
220
232
$ this ->changed [$ pointer ] = $ pointer ;
221
233
Message::increment ("{$ entity ->via }. {$ entity ->type }.updated " );
@@ -230,13 +242,26 @@ public function add(iState $entity, array $opts = []): self
230
242
$ changes = $ this ->objects [$ pointer ]->diff (fields: $ keys );
231
243
232
244
if (count ($ changes ) >= 1 ) {
233
- $ this ->logger ->notice ('MAPPER: [{backend}] updated [{title}] metadata. ' , [
234
- 'id ' => $ cloned ->id ,
235
- 'backend ' => $ entity ->via ,
236
- 'title ' => $ cloned ->getName (),
237
- 'changes ' => $ changes ,
238
- 'fields ' => implode (', ' , $ keys ),
239
- ]);
245
+ $ this ->logger ->notice (
246
+ $ playChanged ? 'MAPPER: [{backend}] updated [{title}] due to play progress change. ' : 'MAPPER: [{backend}] updated [{title}] metadata. ' ,
247
+ [
248
+ 'id ' => $ cloned ->id ,
249
+ 'backend ' => $ entity ->via ,
250
+ 'title ' => $ cloned ->getName (),
251
+ 'changes ' => $ changes ,
252
+ 'fields ' => implode (', ' , $ keys ),
253
+ ]
254
+ );
255
+ if (true === $ entity ->hasPlayProgress ()) {
256
+ $ itemId = r ('{type}://{id}:{tainted}@{backend} ' , [
257
+ 'type ' => $ entity ->type ,
258
+ 'backend ' => $ entity ->via ,
259
+ 'tainted ' => $ entity ->isTainted () ? 'tainted ' : 'untainted ' ,
260
+ 'id ' => ag ($ entity ->getMetadata ($ entity ->via ), iState::COLUMN_ID , '?? ' ),
261
+ ]);
262
+
263
+ $ this ->progressItems [$ itemId ] = $ entity ;
264
+ }
240
265
}
241
266
242
267
return $ this ;
@@ -380,6 +405,19 @@ public function remove(iState $entity): bool
380
405
381
406
public function commit (): mixed
382
407
{
408
+ if (true !== $ this ->inDryRunMode ()) {
409
+ if (count ($ this ->progressItems ) >= 1 ) {
410
+ try {
411
+ $ progress = $ this ->cache ->get ('progress ' , []);
412
+ foreach ($ this ->progressItems as $ itemId => $ entity ) {
413
+ $ progress [$ itemId ] = $ entity ;
414
+ }
415
+ $ this ->cache ->set ('progress ' , $ progress , new DateInterval ('P1D ' ));
416
+ } catch (InvalidArgumentException ) {
417
+ }
418
+ }
419
+ }
420
+
383
421
$ state = $ this ->db ->transactional (function (iDB $ db ) {
384
422
$ list = [
385
423
iState::TYPE_MOVIE => ['added ' => 0 , 'updated ' => 0 , 'failed ' => 0 ],
@@ -437,7 +475,7 @@ public function has(iState $entity): bool
437
475
public function reset (): self
438
476
{
439
477
$ this ->fullyLoaded = false ;
440
- $ this ->objects = $ this ->changed = $ this ->pointers = [];
478
+ $ this ->objects = $ this ->changed = $ this ->pointers = $ this -> progressItems = [];
441
479
442
480
return $ this ;
443
481
}
0 commit comments