Skip to content

Commit

Permalink
[EasyWebhook] Refactor statements provider to use dbal schema (#501)
Browse files Browse the repository at this point in the history
  • Loading branch information
natepage committed Mar 17, 2021
1 parent a209717 commit 81d7223
Show file tree
Hide file tree
Showing 7 changed files with 175 additions and 142 deletions.
152 changes: 152 additions & 0 deletions packages/EasyWebhook/src/Bridge/Doctrine/DbalStatementsProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
<?php

declare(strict_types=1);

namespace EonX\EasyWebhook\Bridge\Doctrine;

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Schema\Schema;
use EonX\EasyWebhook\Interfaces\Stores\ResultStoreInterface;
use EonX\EasyWebhook\Interfaces\Stores\StoreInterface;

final class DbalStatementsProvider
{
/**
* @var \Doctrine\DBAL\Connection
*/
private $conn;

/**
* @var callable
*/
private $extendWebhookResultsTable;

/**
* @var callable
*/
private $extendWebhooksTable;

/**
* @var string
*/
private $webhookResultsTable;

/**
* @var string
*/
private $webhooksTable;

public function __construct(Connection $conn, ?string $webhooksTable = null, ?string $webhookResultsTable = null)
{
$this->conn = $conn;
$this->webhooksTable = $webhooksTable ?? StoreInterface::DEFAULT_TABLE;
$this->webhookResultsTable = $webhookResultsTable ?? ResultStoreInterface::DEFAULT_TABLE;
}

public function extendWebhookResultsTable(callable $callable): self
{
$this->extendWebhookResultsTable = $callable;

return $this;
}

public function extendWebhooksTable(callable $callable): self
{
$this->extendWebhooksTable = $callable;

return $this;
}

/**
* @return string[]
*
* @throws \Doctrine\DBAL\Exception
*/
public function migrateStatements(): array
{
$schema = new Schema();

$webhooksTable = $schema->createTable($this->webhooksTable);
$webhooksTable->addColumn('id', 'guid');
$webhooksTable->addColumn('class', 'string', [
'length' => 191,
]);
$webhooksTable->addColumn('method', 'string', [
'length' => 10,
]);
$webhooksTable->addColumn('url', 'string', [
'length' => 191,
]);
$webhooksTable->addColumn('status', 'string', [
'length' => 50,
]);
$webhooksTable->addColumn('event', 'string', [
'length' => 191,
'notNull' => false,
]);
$webhooksTable->addColumn('http_options', 'text', [
'notNull' => false,
]);
$webhooksTable->addColumn('current_attempt', 'integer', [
'default' => 0,
'length' => 11,
]);
$webhooksTable->addColumn('max_attempt', 'integer', [
'default' => 1,
'length' => 11,
]);
$webhooksTable->addColumn('send_after', 'datetime', [
'notNull' => false,
]);
$webhooksTable->addColumn('created_at', 'datetime');
$webhooksTable->addColumn('updated_at', 'datetime');
$webhooksTable->setPrimaryKey(['id']);
$webhooksTable->addIndex(['status', 'send_after'], 'send_after_idx');

$webhookResultsTable = $schema->createTable($this->webhookResultsTable);
$webhookResultsTable->addColumn('id', 'guid');
$webhookResultsTable->addColumn('method', 'string', [
'length' => 10,
]);
$webhookResultsTable->addColumn('url', 'string');
$webhookResultsTable->addColumn('http_options', 'text', [
'notNull' => false,
]);
$webhookResultsTable->addColumn('response', 'text', [
'notNull' => false,
]);
$webhookResultsTable->addColumn('throwable', 'text', [
'notNull' => false,
]);
$webhookResultsTable->addColumn('webhook_class', 'string', [
'length' => 191,
]);
$webhookResultsTable->addColumn('webhook_id', 'guid');
$webhookResultsTable->addColumn('created_at', 'datetime');
$webhookResultsTable->addColumn('updated_at', 'datetime');
$webhookResultsTable->setPrimaryKey(['id']);

if ($this->extendWebhooksTable !== null) {
\call_user_func($this->extendWebhooksTable, $webhooksTable);
}

if ($this->extendWebhookResultsTable !== null) {
\call_user_func($this->extendWebhookResultsTable, $webhookResultsTable);
}

return $schema->toSql($this->conn->getDatabasePlatform());
}

/**
* @return string[]
*
* @throws \Doctrine\DBAL\Exception
*/
public function rollbackStatements(): array
{
return [
\sprintf('DROP TABLE %s;', $this->webhookResultsTable),
\sprintf('DROP TABLE %s;', $this->webhooksTable),
];
}
}

This file was deleted.

This file was deleted.

This file was deleted.

5 changes: 5 additions & 0 deletions packages/EasyWebhook/src/Interfaces/Stores/StoreInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ interface StoreInterface
*/
public const DATETIME_FORMAT = 'Y-m-d H:i:s';

/**
* @var string
*/
public const DEFAULT_TABLE = 'easy_webhooks';

public function find(string $id): ?WebhookInterface;

public function generateWebhookId(): string;
Expand Down
1 change: 1 addition & 0 deletions packages/EasyWebhook/src/Stores/DoctrineDbalStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ public function store(WebhookInterface $webhook): WebhookInterface
$now = Carbon::now('UTC');
$data = \array_merge($webhook->getExtra() ?? [], $webhook->toArray());
$data['class'] = \get_class($webhook);
$data['updated_at'] = $now;

// New result with no id
if ($webhook->getId() === null) {
Expand Down
20 changes: 17 additions & 3 deletions packages/EasyWebhook/tests/AbstractStoreTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\DriverManager;
use EonX\EasyWebhook\Bridge\Doctrine\StatementProviders\SqliteStatementProvider;
use EonX\EasyWebhook\Bridge\Doctrine\DbalStatementsProvider;
use EonX\EasyWebhook\Interfaces\WebhookInterface;
use EonX\EasyWebhook\Webhook;

Expand All @@ -17,6 +17,11 @@ abstract class AbstractStoreTestCase extends AbstractTestCase
*/
protected $doctrineDbal;

/**
* @var \EonX\EasyWebhook\Bridge\Doctrine\DbalStatementsProvider
*/
private $stmtsProvider;

protected function createWebhookForSendAfter(
?\DateTimeInterface $sendAfter = null,
?string $status = null
Expand Down Expand Up @@ -53,7 +58,7 @@ protected function setUp(): void
$conn = $this->getDoctrineDbalConnection();
$conn->connect();

foreach (SqliteStatementProvider::migrateStatements() as $statement) {
foreach ($this->getStmtsProvider()->migrateStatements() as $statement) {
$conn->executeStatement($statement);
}

Expand All @@ -64,12 +69,21 @@ protected function tearDown(): void
{
$conn = $this->getDoctrineDbalConnection();

foreach (SqliteStatementProvider::rollbackStatements() as $statement) {
foreach ($this->getStmtsProvider()->rollbackStatements() as $statement) {
$conn->executeStatement($statement);
}

$conn->close();

parent::tearDown();
}

private function getStmtsProvider(): DbalStatementsProvider
{
if ($this->stmtsProvider !== null) {
return $this->stmtsProvider;
}

return $this->stmtsProvider = new DbalStatementsProvider($this->getDoctrineDbalConnection());
}
}

0 comments on commit 81d7223

Please sign in to comment.