Skip to content
Merged
Show file tree
Hide file tree
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
22 changes: 9 additions & 13 deletions src/WpOrg/Api/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,12 @@ public function read(string $slug): ?bool
return null;
}

$content = $this->cache->read($key);
$content = (string) $this->cache->read($key);

// Should never happen because of the age check.
if ($content === false) {
return null;
}

$lines = explode("\n", $content);

return match ($lines[0]) {
'closed' => true,
'open' => false,
default => null, // Unexpected content. Treat as a cache miss.
return match (true) {
str_starts_with($content, 'closed'.PHP_EOL) => true,
str_starts_with($content, 'open'.PHP_EOL) => false,
default => null, // Unexpected content. Treat as a miss.
};
}

Expand All @@ -48,10 +41,13 @@ public function write(string $slug, bool $isClosed): void
}

$content = sprintf(
"%s\n%s\n%s\n",
"%s%s%s%s%s%s",
$isClosed ? 'closed' : 'open',
PHP_EOL,
$slug,
PHP_EOL,
date(DateTimeInterface::RFC3339),
PHP_EOL
);

$this->cache->write(
Expand Down
4 changes: 2 additions & 2 deletions tests/Unit/WpOrg/Api/CacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ function (string $slug, bool $expected, string $firstLine): void {
$composerCache->expects()
->read()
->with("{$slug}.txt")
->andReturn("{$firstLine}\n{$slug}\n2006-01-02T15:04:05+07:00\n");
->andReturn($firstLine.PHP_EOL.$slug.PHP_EOL.'2006-01-02T15:04:05+07:00'.PHP_EOL);

$cache = new Cache($composerCache);

Expand Down Expand Up @@ -89,7 +89,7 @@ function (string $slug, bool $isClosed, string $expected): void {

$composerCache->shouldHaveReceived('write', [
"{$slug}.txt",
Mockery::on(static fn (string $actual) => str_starts_with($actual, $expected."\n")),
Mockery::on(static fn (string $actual) => str_starts_with($actual, $expected.PHP_EOL.$slug.PHP_EOL)),
]);
})->with('slugs');

Expand Down
Loading