From e3d1d2f06a67ea4145d83de4b3e0edd8c10dde1b Mon Sep 17 00:00:00 2001 From: tinect Date: Sat, 5 Oct 2024 12:13:34 +0200 Subject: [PATCH] feat: add support for writeBatch --- .github/workflows/test.yml | 3 +- composer.json | 2 +- src/Adapter/BunnyCdnFactory.php | 12 +++- .../Shopware6BunnyCdnWriteBatchAdapter.php | 64 +++++++++++++++++++ tests/unit/Adapter/BunnyCdnFactoryTest.php | 15 +++++ 5 files changed, 93 insertions(+), 3 deletions(-) create mode 100644 src/Adapter/Shopware6BunnyCdnWriteBatchAdapter.php diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index eec5ba1..37c3bb6 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -25,6 +25,7 @@ jobs: "v6.5.8.2", "6.5.x", "v6.6.0.3", + "v6.6.8.1", "trunk" ] php: ["8.2"] @@ -70,4 +71,4 @@ jobs: with: files: ./clover.xml root_dir: ${{ github.workspace }}/custom/plugins/${{ env.PLUGIN_NAME }} - working-directory: ${{ github.workspace }}/custom/plugins/${{ env.PLUGIN_NAME }} \ No newline at end of file + working-directory: ${{ github.workspace }}/custom/plugins/${{ env.PLUGIN_NAME }} diff --git a/composer.json b/composer.json index a0acfa3..68e8e31 100644 --- a/composer.json +++ b/composer.json @@ -20,7 +20,7 @@ ], "require": { "shopware/core": "~6.5.3||~6.6.0", - "platformcommunity/flysystem-bunnycdn": "^3.3.4", + "platformcommunity/flysystem-bunnycdn": "^3.3.5", "league/flysystem-path-prefixing": "^3.10.3", "ajgl/flysystem-replicate": "^2.2", "tinect/flysystem-garbage": "^1.0" diff --git a/src/Adapter/BunnyCdnFactory.php b/src/Adapter/BunnyCdnFactory.php index 74605d0..ed3a199 100644 --- a/src/Adapter/BunnyCdnFactory.php +++ b/src/Adapter/BunnyCdnFactory.php @@ -7,6 +7,7 @@ use League\Flysystem\Local\LocalFilesystemAdapter; use League\Flysystem\PathPrefixing\PathPrefixedAdapter; use Shopware\Core\Framework\Adapter\Filesystem\Adapter\AdapterFactoryInterface; +use Shopware\Core\Framework\Adapter\Filesystem\Plugin\WriteBatchInterface; use Tinect\Flysystem\Garbage\GarbageFilesystemAdapter; class BunnyCdnFactory implements AdapterFactoryInterface @@ -19,7 +20,7 @@ public function create(array $config): FilesystemAdapter $adapterConfig = new AdapterConfig(); $adapterConfig->assign($config); - $adapter = new Shopware6BunnyCdnAdapter($adapterConfig); + $adapter = $this->getBasicAdapter($adapterConfig); if (!empty($adapterConfig->isUseGarbage())) { $adapter = new GarbageFilesystemAdapter($adapter); @@ -36,6 +37,15 @@ public function create(array $config): FilesystemAdapter return $adapter; } + private function getBasicAdapter(AdapterConfig $adapterConfig): FilesystemAdapter + { + if (\class_exists(WriteBatchInterface::class)) { + return new Shopware6BunnyCdnWriteBatchAdapter($adapterConfig); + } + + return new Shopware6BunnyCdnAdapter($adapterConfig); + } + public function getType(): string { return 'bunnycdn'; diff --git a/src/Adapter/Shopware6BunnyCdnWriteBatchAdapter.php b/src/Adapter/Shopware6BunnyCdnWriteBatchAdapter.php new file mode 100644 index 0000000..63d10c6 --- /dev/null +++ b/src/Adapter/Shopware6BunnyCdnWriteBatchAdapter.php @@ -0,0 +1,64 @@ +getTargetFiles() as $targetFile) { + if (\is_resource($data->getSourceFile())) { + $sourcePath = stream_get_meta_data($data->getSourceFile())['uri'] ?? null; + + if (!\is_string($sourcePath)) { + throw new \InvalidArgumentException('Cannot get source file path from stream.'); + } + + $files[] = new WriteBatchFile($sourcePath, $targetFile); + + continue; + } + + $files[] = new WriteBatchFile($data->getSourceFile(), $targetFile); + } + + continue; + } + + if ($data instanceof Config) { + $config = $data; + + continue; + } + + if (\is_array($data)) { + foreach ($data as $item) { + if ($item instanceof WriteBatchFile) { + $files[] = $item; + + continue; + } + + throw new \InvalidArgumentException('Each value of array must be a WriteBatchFile object.'); + } + } + } + + if (empty($files)) { + return; + } + + parent::writeBatch($files, $config); + } +} diff --git a/tests/unit/Adapter/BunnyCdnFactoryTest.php b/tests/unit/Adapter/BunnyCdnFactoryTest.php index 43d7086..6090687 100644 --- a/tests/unit/Adapter/BunnyCdnFactoryTest.php +++ b/tests/unit/Adapter/BunnyCdnFactoryTest.php @@ -5,8 +5,10 @@ use Ajgl\Flysystem\Replicate\ReplicateFilesystemAdapter; use Frosh\BunnycdnMediaStorage\Adapter\BunnyCdnFactory; use Frosh\BunnycdnMediaStorage\Adapter\Shopware6BunnyCdnAdapter; +use Frosh\BunnycdnMediaStorage\Adapter\Shopware6BunnyCdnWriteBatchAdapter; use League\Flysystem\PathPrefixing\PathPrefixedAdapter; use PHPUnit\Framework\TestCase; +use Shopware\Core\Framework\Adapter\Filesystem\Plugin\WriteBatchInterface; use Tinect\Flysystem\Garbage\GarbageFilesystemAdapter; class BunnyCdnFactoryTest extends TestCase @@ -47,6 +49,19 @@ public function testFactoryCreatesReplicateFilesystemAdapter(): void static::assertInstanceOf(ReplicateFilesystemAdapter::class, $adapter); } + public function testFactoryCreatesWriteBatchAdapter(): void + { + if (!\class_exists(WriteBatchInterface::class)) { + static::markTestSkipped('WriteBatchInterface not found'); + } + + $factory = new BunnyCdnFactory(); + $config = ['endpoint' => 'https://storage.bunnycdn.com', 'apiKey' => 'a', 'storageName' => 'a', 'useGarbage' => false]; + $adapter = $factory->create($config); + + static::assertInstanceOf(Shopware6BunnyCdnWriteBatchAdapter::class, $adapter); + } + public function testFactoryReturnsBunnycdnType(): void { $factory = new BunnyCdnFactory();