Skip to content

Commit

Permalink
Merge pull request #48 from shopware/add-auto-storage
Browse files Browse the repository at this point in the history
feat: add auto storage option
  • Loading branch information
shyim authored Sep 23, 2024
2 parents f6f2a18 + 33df607 commit 56e8727
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 5 deletions.
5 changes: 4 additions & 1 deletion src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ public function getConfigTreeBuilder(): TreeBuilder

// @phpstan-ignore-next-line
$rootNode->children()
->scalarNode('storage')->defaultValue('in-memory')->end()
->enumNode('storage')
->values(['in-memory', 'doctrine', 'dynamodb', 'auto'])
->defaultValue('auto')
->end()
->arrayNode('doctrine')
->children()
->scalarNode('shop_class')
Expand Down
16 changes: 14 additions & 2 deletions src/DependencyInjection/ShopwareAppExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Shopware\AppBundle\DependencyInjection;

use AsyncAws\DynamoDb\DynamoDbClient;
use Doctrine\Bundle\DoctrineBundle\DoctrineBundle;
use Shopware\App\SDK\Adapter\DynamoDB\DynamoDBRepository;
use Shopware\App\SDK\Shop\ShopRepositoryInterface;
use Shopware\App\SDK\Test\MockShopRepository;
Expand All @@ -29,12 +30,23 @@ public function load(array $configs, ContainerBuilder $container): void

$loader->load('services.xml');

if ($config['storage'] === 'dynamodb') {
$storage = $config['storage'];

if ($storage === 'auto') {
// @infection-ignore-all
$storage = match (true) {
ContainerBuilder::willBeAvailable('async-aws/dynamo-db', DynamoDbClient::class, ['async-aws/async-aws-bundle']) => 'dynamodb',
ContainerBuilder::willBeAvailable('doctrine/orm', DoctrineBundle::class, ['doctrine/doctrine-bundle']) => 'doctrine',
default => 'in-memory',
};
}

if ($storage === 'dynamodb') {
$service = new Definition(DynamoDBRepository::class);
$service->setArgument(0, new Reference(DynamoDbClient::class));
$service->setArgument(1, $config['dynamodb']['table_name'] ?? 'shops');
$container->setDefinition(ShopRepositoryInterface::class, $service);
} elseif ($config['storage'] === 'doctrine') {
} elseif ($storage === 'doctrine') {
$container->getDefinition(ShopRepositoryInterface::class)
->replaceArgument(0, $config['doctrine']['shop_class'] ?? AbstractShop::class);
} else {
Expand Down
5 changes: 4 additions & 1 deletion tests/DependencyInjection/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ public static function getComparableTreeBuilder(): TreeBuilder
$rootNode = $treeBuilder->getRootNode();

$rootNode->children()
->scalarNode('storage')->defaultValue('in-memory')->end()
->enumNode('storage')
->values(['in-memory', 'doctrine', 'dynamodb', 'auto'])
->defaultValue('auto')
->end()
->arrayNode('doctrine')
->children()
->scalarNode('shop_class')
Expand Down
14 changes: 13 additions & 1 deletion tests/DependencyInjection/ShopwareAppExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use AsyncAws\DynamoDb\DynamoDbClient;
use PHPUnit\Framework\TestCase;
use Shopware\App\SDK\Adapter\DynamoDB\DynamoDBRepository;
use Shopware\App\SDK\Adapter\DynamoDB\DynamoDBShop;
use Shopware\App\SDK\Shop\ShopRepositoryInterface;
use Shopware\App\SDK\Test\MockShopRepository;
Expand All @@ -17,14 +18,25 @@

class ShopwareAppExtensionTest extends TestCase
{
public function testDefaultConfig(): void
public function testDefaultConfigAuto(): void
{
$extension = new ShopwareAppExtension();
$container = new ContainerBuilder();
$extension->load([], $container);

static::assertTrue($container->hasDefinition(ShopRepositoryInterface::class));

static::assertSame(DynamoDBRepository::class, $container->getDefinition(ShopRepositoryInterface::class)->getClass());
}

public function testDefaultInMemory(): void
{
$extension = new ShopwareAppExtension();
$container = new ContainerBuilder();
$extension->load(['my_bundle' => ['storage' => 'in-memory']], $container);

static::assertTrue($container->hasDefinition(ShopRepositoryInterface::class));

static::assertSame(MockShopRepository::class, $container->getDefinition(ShopRepositoryInterface::class)->getClass());
}

Expand Down

0 comments on commit 56e8727

Please sign in to comment.