Skip to content

Commit 079e06e

Browse files
committed
add period id in tag-list, when normalizing a ScheduleEntry
1 parent 745c981 commit 079e06e

File tree

4 files changed

+18
-18
lines changed

4 files changed

+18
-18
lines changed

api/src/HttpCache/TagCollector.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use ApiPlatform\Metadata\Operation;
99
use ApiPlatform\Serializer\TagCollectorInterface;
1010
use App\Entity\HasId;
11+
use App\Entity\ScheduleEntry;
1112

1213
/**
1314
* Collects cache tags during normalization.
@@ -48,6 +49,12 @@ public function collect(array $context = []): void {
4849
}
4950

5051
$this->addCacheTagForResource($iri);
52+
53+
// add resource specific tags
54+
if ($object instanceof ScheduleEntry) {
55+
// various content of ScheduleEntry such as Start, DayNumber, ScheduleNumber is calculated from period properties
56+
$this->addCacheTagForResource($object->getPeriod()->getId());
57+
}
5158
}
5259

5360
private function addCacheTagForResource(string $iri): void {

api/src/State/PeriodPersistProcessor.php

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,13 @@
88
use App\Entity\Period;
99
use App\State\Util\AbstractPersistProcessor;
1010
use App\Util\DateTimeUtil;
11-
use FOS\HttpCacheBundle\CacheManager;
1211

1312
/**
1413
* @template-extends AbstractPersistProcessor<Period>
1514
*/
1615
class PeriodPersistProcessor extends AbstractPersistProcessor {
1716
public function __construct(
18-
ProcessorInterface $decorated,
19-
private readonly CacheManager $cacheManager
17+
ProcessorInterface $decorated
2018
) {
2119
parent::__construct($decorated);
2220
}
@@ -25,32 +23,29 @@ public function __construct(
2523
* @param Period $data
2624
*/
2725
public function onBefore($data, Operation $operation, array $uriVariables = [], array $context = []): Period {
28-
$this->moveDaysAndScheduleEntries($data, $context['previous_data'] ?? null);
26+
self::moveDaysAndScheduleEntries($data, $context['previous_data'] ?? null);
2927
self::removeExtraDays($data);
3028
self::addMissingDays($data);
3129

3230
return $data;
3331
}
3432

35-
public function moveDaysAndScheduleEntries(Period $period, ?Period $originalPeriod = null) {
33+
public static function moveDaysAndScheduleEntries(Period $period, ?Period $originalPeriod = null) {
3634
if (!$originalPeriod) {
3735
return;
3836
}
3937

40-
$deltaMinutes = DateTimeUtil::differenceInMinutes($originalPeriod->start, $period->start);
41-
if (0 === $deltaMinutes) {
42-
return;
43-
}
44-
45-
// start date shifts --> purge schedule_entries subresource to reflect changes in dates + numbers
46-
$this->cacheManager->invalidateTags(["/api/periods/{$period->getId()}/schedule_entries"]);
47-
4838
// moveScheduleEntries === true: scheduleEntries move relative to the start date (no change of offset needed -> return)
4939
// moveScheduleEntries === false: scheduleEntries stay absolutely on the scheduled calendar date (change of offset needed)
5040
if ($period->moveScheduleEntries) {
5141
return;
5242
}
5343

44+
$deltaMinutes = DateTimeUtil::differenceInMinutes($originalPeriod->start, $period->start);
45+
if (0 === $deltaMinutes) {
46+
return;
47+
}
48+
5449
// Move ScheduleEntries
5550
// --> existing scheduleEntries outside the new period boundary are not possible (validation should have already failed)
5651
foreach ($period->scheduleEntries as $scheduleEntry) {

api/tests/State/PeriodPersistProcessorTest.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
use App\Entity\Period;
1111
use App\Entity\ScheduleEntry;
1212
use App\State\PeriodPersistProcessor;
13-
use FOS\HttpCacheBundle\CacheManager;
1413
use PHPUnit\Framework\TestCase;
1514

1615
/**
@@ -50,11 +49,9 @@ protected function setUp(): void {
5049
$day2->addDayResponsible($this->dayResponsible);
5150

5251
$decoratedProcessor = $this->createMock(ProcessorInterface::class);
53-
$cacheManager = $this->createMock(CacheManager::class);
5452

5553
$this->processor = new PeriodPersistProcessor(
56-
$decoratedProcessor,
57-
$cacheManager
54+
$decoratedProcessor
5855
);
5956
}
6057

e2e/specs/httpCache/scheduleEntries.cy.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ const collectionXKeys =
1414
/* campCollaboration for bipiUser */
1515
'10d8f02ce5b4 ' +
1616
/* scheduleEntries + links */
17-
'e68f4e47517a e68f4e47517a#period e68f4e47517a#activity e68f4e47517a#day ' +
17+
/* the first scheduleEntry also includes the period id 7fa4564a5d5d */
18+
'e68f4e47517a 7fa4564a5d5d e68f4e47517a#period e68f4e47517a#activity e68f4e47517a#day ' +
1819
'f0883e931649 f0883e931649#period f0883e931649#activity f0883e931649#day ' +
1920
'29c9e9a07d82 29c9e9a07d82#period 29c9e9a07d82#activity 29c9e9a07d82#day ' +
2021
'ee85308a97d1 ee85308a97d1#period ee85308a97d1#activity ee85308a97d1#day ' +

0 commit comments

Comments
 (0)