Skip to content

Commit

Permalink
Merge pull request #24 from shopware/add-dynamodb-repository
Browse files Browse the repository at this point in the history
feat: add dynamodb storage
  • Loading branch information
shyim authored Sep 11, 2024
2 parents 3c1a0ca + 03b2516 commit 0fdf101
Show file tree
Hide file tree
Showing 5 changed files with 436 additions and 1 deletion.
7 changes: 6 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,12 @@
"nyholm/psr7-server": "^1.0",
"php-http/curl-client": "^2.2",
"phpstan/phpstan": "^1.10.14",
"phpunit/phpunit": "^10.1"
"phpunit/phpunit": "^10.1",
"async-aws/dynamo-db": "~3.2",
"symfony/polyfill-uuid": "^1.31"
},
"suggest": {
"async-aws/dynamo-db": "For using the DynamoDBRepository"
},
"autoload": {
"psr-4": {
Expand Down
97 changes: 97 additions & 0 deletions src/Adapter/DynamoDB/DynamoDBRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php

declare(strict_types=1);

namespace Shopware\App\SDK\Adapter\DynamoDB;

use AsyncAws\DynamoDb\DynamoDbClient;
use AsyncAws\DynamoDb\Input\DeleteItemInput;
use AsyncAws\DynamoDb\Input\GetItemInput;
use AsyncAws\DynamoDb\Input\PutItemInput;
use Shopware\App\SDK\Shop\ShopInterface;
use Shopware\App\SDK\Shop\ShopRepositoryInterface;

/**
* @implements ShopRepositoryInterface<DynamoDBShop>
*/
class DynamoDBRepository implements ShopRepositoryInterface
{
public function __construct(private readonly DynamoDbClient $client, private readonly string $tableName)
{
}

public function createShopStruct(string $shopId, string $shopUrl, string $shopSecret): ShopInterface
{
return new DynamoDBShop($shopId, $shopUrl, $shopSecret);
}

public function createShop(ShopInterface $shop): void
{
$this->client->putItem(new PutItemInput([
'TableName' => $this->tableName,
'Item' => [
'id' => ['S' => $shop->getShopId()],
'active' => ['BOOL' => $shop->isShopActive() ? '1' : '0'],
'url' => ['S' => $shop->getShopUrl()],
'secret' => ['S' => $shop->getShopSecret()],
'clientId' => ['S' => (string) $shop->getShopClientId()],
'clientSecret' => ['S' => (string) $shop->getShopClientSecret()],
],
]));
}

public function getShopFromId(string $shopId): ShopInterface|null
{
$item = $this->client->getItem(new GetItemInput([
'TableName' => $this->tableName,
'Key' => [
'id' => ['S' => $shopId],
],
]))->getItem();

if (!$item) {
return null;
}

$shopClientId = $item['clientId']->getS();
$shopClientSecret = $item['clientSecret']->getS();

if ($shopClientSecret === '') {
$shopClientSecret = null;
}

if ($shopClientId === '') {
$shopClientId = null;
}

$active = $item['active']->getBool();

if ($active === null) {
$active = false;
}

return new DynamoDBShop(
$item['id']->getS() ?? '',
$item['url']->getS() ?? '',
$item['secret']->getS() ?? '',
$shopClientId,
$shopClientSecret,
$active,
);
}

public function updateShop(ShopInterface $shop): void
{
$this->createShop($shop);
}

public function deleteShop(string $shopId): void
{
$this->client->deleteItem(new DeleteItemInput([
'TableName' => $this->tableName,
'Key' => [
'id' => ['S' => $shopId],
],
]));
}
}
66 changes: 66 additions & 0 deletions src/Adapter/DynamoDB/DynamoDBShop.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

declare(strict_types=1);

namespace Shopware\App\SDK\Adapter\DynamoDB;

use Shopware\App\SDK\Shop\ShopInterface;

class DynamoDBShop implements ShopInterface
{
public function __construct(public string $shopId, public string $shopUrl, public string $shopSecret, public ?string $shopClientId = null, public ?string $shopClientSecret = null, public bool $active = false)
{
}

public function isShopActive(): bool
{
return $this->active;
}

public function getShopId(): string
{
return $this->shopId;
}

public function getShopUrl(): string
{
return $this->shopUrl;
}

public function getShopSecret(): string
{
return $this->shopSecret;
}

public function getShopClientId(): ?string
{
return $this->shopClientId;
}

public function getShopClientSecret(): ?string
{
return $this->shopClientSecret;
}

public function setShopApiCredentials(string $clientId, string $clientSecret): ShopInterface
{
$this->shopClientId = $clientId;
$this->shopClientSecret = $clientSecret;

return $this;
}

public function setShopUrl(string $url): ShopInterface
{
$this->shopUrl = $url;

return $this;
}

public function setShopActive(bool $active): ShopInterface
{
$this->active = $active;

return $this;
}
}
Loading

0 comments on commit 0fdf101

Please sign in to comment.