Skip to content

Commit

Permalink
ref: review suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
theus77 committed Oct 13, 2024
1 parent a59eff9 commit 6647322
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 54 deletions.
25 changes: 12 additions & 13 deletions EMS/client-helper-bundle/src/Command/Local/UploadAssetsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,17 +65,16 @@ protected function execute(InputInterface $input, OutputInterface $output): int
}

try {
switch ($this->archiveType) {
case self::ARCHIVE_ZIP:
$hash = $this->uploadZipArchive();
break;
case self::ARCHIVE_EMS:
$hash = $this->uploadEmsArchive();
break;
default:
$this->io->error(\sprintf('Archive format %s not supported. Supported formats are "%s" and "%s"', $this->archiveType, self::ARCHIVE_EMS, self::ARCHIVE_ZIP));

return self::EXECUTE_ERROR;
$hash = match ($this->archiveType) {
self::ARCHIVE_ZIP => $this->uploadZipArchive(),
self::ARCHIVE_EMS => $this->uploadEmsArchive(),
default => false
};

if (!$hash) {
$this->io->error(\sprintf('Archive format %s not supported. Supported formats are "%s" and "%s"', $this->archiveType, self::ARCHIVE_EMS, self::ARCHIVE_ZIP));

return self::EXECUTE_ERROR;
}

$this->io->newLine();
Expand Down Expand Up @@ -141,9 +140,9 @@ private function uploadEmsArchive(): string
$progressBar = $this->io->createProgressBar($archive->getCount());
foreach ($this->coreApi->file()->heads(...$archive->getHashes()) as $hash) {
$file = $archive->getFirstFileByHash($hash);
$uploadHash = $this->coreApi->file()->uploadFile($directory.DIRECTORY_SEPARATOR.$file->getFilename());
$uploadHash = $this->coreApi->file()->uploadFile($directory.DIRECTORY_SEPARATOR.$file->filename);
if ($uploadHash !== $hash) {
throw new \RuntimeException(\sprintf('Mismatched between the computed hash (%s) and the hash of the uploaded file (%s) for the file %s', $hash, $uploadHash, $file->getFilename()));
throw new \RuntimeException(\sprintf('Mismatched between the computed hash (%s) and the hash of the uploaded file (%s) for the file %s', $hash, $uploadHash, $file->filename));
}
$progressBar->advance();
}
Expand Down
6 changes: 3 additions & 3 deletions EMS/common-bundle/src/Storage/Archive.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public static function fromStructure(string $structure, string $hashAlgo): self
$files = Json::decode($structure);
foreach ($files as $file) {
$item = $archive->parseFile($file);
$archive->files[$item->getFilename()] = $item;
$archive->files[$item->filename] = $item;
}

return $archive;
Expand All @@ -56,7 +56,7 @@ public static function fromStructure(string $structure, string $hashAlgo): self
public function getHashes(): iterable
{
foreach ($this->files as $file) {
yield $file->getHash();
yield $file->hash;
}
}

Expand All @@ -71,7 +71,7 @@ private function addFile(SplFileInfo $file): void
public function getFirstFileByHash(mixed $hash): ArchiveItem
{
foreach ($this->files as $file) {
if ($hash === $file->getHash()) {
if ($hash === $file->hash) {
return $file;
}
}
Expand Down
36 changes: 8 additions & 28 deletions EMS/common-bundle/src/Storage/ArchiveItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,43 +12,23 @@ class ArchiveItem implements \JsonSerializable
public const SIZE = 'size';

public function __construct(
private readonly string $filename,
private readonly string $type,
private readonly int $size,
private readonly string $hash
public readonly string $filename,
public readonly string $type,
public readonly int $size,
public readonly string $hash
) {
}

public function getFilename(): string
{
return $this->filename;
}

public function getType(): string
{
return $this->type;
}

public function getHash(): string
{
return $this->hash;
}

public function getSize(): int
{
return $this->size;
}

/**
* @return array{filename: string, hash: string, type: string, size: int}
*/
public function jsonSerialize(): array
{
return [
self::FILENAME => $this->filename,
self::HASH => $this->hash,
self::TYPE => $this->type,
self::SIZE => $this->size,
ArchiveItem::FILENAME => $this->filename,
ArchiveItem::HASH => $this->hash,
ArchiveItem::TYPE => $this->type,
ArchiveItem::SIZE => $this->size,
];
}
}
19 changes: 9 additions & 10 deletions EMS/common-bundle/src/Storage/StorageManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -536,13 +536,12 @@ public function getStreamFromArchive(string $hash, string $path): StreamWrapper

$archiveFile = TempFile::create()->loadFromStream($this->getStream($hash));
$mimeType = MimeTypeHelper::getInstance()->guessMimeType($archiveFile->path);
switch ($mimeType) {
case MimeTypes::APPLICATION_ZIP->value:
return $this->getStreamFromZipArchive($hash, $path, $archiveFile);
case MimeTypes::APPLICATION_JSON->value:
return $this->getStreamFromJsonArchive($hash, $path, $archiveFile);
}
throw new \RuntimeException(\sprintf('Archive format %s not supported', $mimeType));

return match ($mimeType) {
MimeTypes::APPLICATION_ZIP->value => $this->getStreamFromZipArchive($hash, $path, $archiveFile),
MimeTypes::APPLICATION_JSON->value => $this->getStreamFromJsonArchive($hash, $path, $archiveFile),
default => throw new \RuntimeException(\sprintf('Archive format %s not supported', $mimeType)),
};
}

public function extractFromArchive(string $hash): TempDirectory
Expand All @@ -557,7 +556,7 @@ public function extractFromArchive(string $hash): TempDirectory
$archive = Archive::fromStructure($archiveFile->getContents(), $this->hashAlgo);
$tempDir = TempDirectory::create();
foreach ($archive->iterator() as $file) {
$tempDir->add($this->getStream($file->getHash()), $file->getFilename());
$tempDir->add($this->getStream($file->hash), $file->filename);
}
break;
default:
Expand Down Expand Up @@ -615,7 +614,7 @@ private function getStreamFromJsonArchive(string $hash, string $path, TempFile $
$counter = 0;
foreach ($archive->iterator() as $item) {
foreach ($this->adapters as $adapter) {
if ($adapter->copyFileInArchiveCache($hash, $item->getHash(), $item->getFilename(), $item->getType())) {
if ($adapter->copyFileInArchiveCache($hash, $item->hash, $item->filename, $item->type)) {
++$counter;
break;
}
Expand All @@ -629,6 +628,6 @@ private function getStreamFromJsonArchive(string $hash, string $path, TempFile $
$this->logger->warning(\sprintf('%d files, on a total of %d, have been successfully saved in cache', $counter, $archive->getCount()));
}

return new StreamWrapper($this->getStream($file->getHash()), $file->getType(), $file->getSize());
return new StreamWrapper($this->getStream($file->hash), $file->type, $file->size);
}
}

0 comments on commit 6647322

Please sign in to comment.