Skip to content

Commit

Permalink
feat: add support for writeBatch
Browse files Browse the repository at this point in the history
  • Loading branch information
tinect committed Nov 7, 2024
1 parent c4a8bf2 commit e3d1d2f
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 3 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ jobs:
"v6.5.8.2",
"6.5.x",
"v6.6.0.3",
"v6.6.8.1",
"trunk"
]
php: ["8.2"]
Expand Down Expand Up @@ -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 }}
working-directory: ${{ github.workspace }}/custom/plugins/${{ env.PLUGIN_NAME }}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
12 changes: 11 additions & 1 deletion src/Adapter/BunnyCdnFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
Expand All @@ -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';
Expand Down
64 changes: 64 additions & 0 deletions src/Adapter/Shopware6BunnyCdnWriteBatchAdapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php declare(strict_types=1);

namespace Frosh\BunnycdnMediaStorage\Adapter;

use League\Flysystem\Config;
use PlatformCommunity\Flysystem\BunnyCDN\WriteBatchFile;
use Shopware\Core\Framework\Adapter\Filesystem\Plugin\CopyBatchInput;
use Shopware\Core\Framework\Adapter\Filesystem\Plugin\WriteBatchInterface;

class Shopware6BunnyCdnWriteBatchAdapter extends Shopware6BunnyCdnAdapter implements WriteBatchInterface
{
public function writeBatch(CopyBatchInput|array|Config ...$input): void
{
$files = [];
$config = new Config();

foreach ($input as $data) {
if ($data instanceof CopyBatchInput) {
// migrate CopyBatchInput of Shopware to WriteBatchFile
foreach ($data->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);
}
}
15 changes: 15 additions & 0 deletions tests/unit/Adapter/BunnyCdnFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit e3d1d2f

Please sign in to comment.