|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Blokctl\Command; |
| 6 | + |
| 7 | +use Blokctl\Action\Asset\AssetsListAction; |
| 8 | +use Blokctl\Render; |
| 9 | +use Storyblok\ManagementApi\Data\Asset; |
| 10 | +use Symfony\Component\Console\Attribute\AsCommand; |
| 11 | +use Symfony\Component\Console\Input\InputInterface; |
| 12 | +use Symfony\Component\Console\Input\InputOption; |
| 13 | +use Symfony\Component\Console\Output\OutputInterface; |
| 14 | + |
| 15 | +#[AsCommand( |
| 16 | + name: 'assets:list', |
| 17 | + description: 'List assets with optional search filter', |
| 18 | +)] |
| 19 | +class AssetsListCommand extends AbstractCommand |
| 20 | +{ |
| 21 | + #[\Override] |
| 22 | + protected function configure(): void |
| 23 | + { |
| 24 | + parent::configure(); |
| 25 | + $this |
| 26 | + ->addOption( |
| 27 | + 'search', |
| 28 | + null, |
| 29 | + InputOption::VALUE_REQUIRED, |
| 30 | + 'Search assets by filename', |
| 31 | + ) |
| 32 | + ->addOption( |
| 33 | + 'page', |
| 34 | + 'p', |
| 35 | + InputOption::VALUE_REQUIRED, |
| 36 | + 'Page number', |
| 37 | + '1', |
| 38 | + ) |
| 39 | + ->addOption( |
| 40 | + 'per-page', |
| 41 | + null, |
| 42 | + InputOption::VALUE_REQUIRED, |
| 43 | + 'Results per page (max 1000)', |
| 44 | + '25', |
| 45 | + ); |
| 46 | + } |
| 47 | + |
| 48 | + protected function execute( |
| 49 | + InputInterface $input, |
| 50 | + OutputInterface $output, |
| 51 | + ): int { |
| 52 | + /** @var string|null $search */ |
| 53 | + $search = $input->getOption('search'); |
| 54 | + /** @var string $pageOption */ |
| 55 | + $pageOption = $input->getOption('page'); |
| 56 | + /** @var string $perPageOption */ |
| 57 | + $perPageOption = $input->getOption('per-page'); |
| 58 | + |
| 59 | + try { |
| 60 | + $result = (new AssetsListAction($this->client))->execute( |
| 61 | + spaceId: $this->spaceId, |
| 62 | + search: $search, |
| 63 | + page: (int) $pageOption, |
| 64 | + perPage: (int) $perPageOption, |
| 65 | + ); |
| 66 | + } catch (\RuntimeException $runtimeException) { |
| 67 | + Render::error($runtimeException->getMessage()); |
| 68 | + return self::FAILURE; |
| 69 | + } |
| 70 | + |
| 71 | + if ($result->count() === 0) { |
| 72 | + Render::log('No assets found'); |
| 73 | + return self::SUCCESS; |
| 74 | + } |
| 75 | + |
| 76 | + Render::titleSection( |
| 77 | + 'Assets (page ' . $pageOption . |
| 78 | + ', showing ' . $result->count() . ')', |
| 79 | + ); |
| 80 | + |
| 81 | + /** @var Asset $asset */ |
| 82 | + foreach ($result->assets as $asset) { |
| 83 | + $details = []; |
| 84 | + $details[] = 'id: ' . $asset->id(); |
| 85 | + $contentType = $asset->contentType(); |
| 86 | + if ($contentType !== '') { |
| 87 | + $details[] = $contentType; |
| 88 | + } |
| 89 | + |
| 90 | + $size = $asset->contentLength(); |
| 91 | + if ($size !== null && $size > 0) { |
| 92 | + $details[] = $this->formatBytes($size); |
| 93 | + } |
| 94 | + |
| 95 | + $createdAt = $asset->createdAt(); |
| 96 | + if ($createdAt !== null && $createdAt !== '') { |
| 97 | + $details[] = $createdAt; |
| 98 | + } |
| 99 | + |
| 100 | + Render::labelValue( |
| 101 | + $asset->filename(), |
| 102 | + implode(' | ', $details), |
| 103 | + ); |
| 104 | + } |
| 105 | + |
| 106 | + return self::SUCCESS; |
| 107 | + } |
| 108 | + |
| 109 | + private function formatBytes(int $bytes): string |
| 110 | + { |
| 111 | + if ($bytes < 1024) { |
| 112 | + return $bytes . ' B'; |
| 113 | + } |
| 114 | + |
| 115 | + if ($bytes < 1048576) { |
| 116 | + return round($bytes / 1024, 1) . ' KB'; |
| 117 | + } |
| 118 | + |
| 119 | + return round($bytes / 1048576, 1) . ' MB'; |
| 120 | + } |
| 121 | +} |
0 commit comments