-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[EasyWebhook] Refactor statements provider to use dbal schema (#501)
- Loading branch information
Showing
7 changed files
with
175 additions
and
142 deletions.
There are no files selected for viewing
152 changes: 152 additions & 0 deletions
152
packages/EasyWebhook/src/Bridge/Doctrine/DbalStatementsProvider.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
]; | ||
} | ||
} |
18 changes: 0 additions & 18 deletions
18
packages/EasyWebhook/src/Bridge/Doctrine/Interfaces/StatementsProviderInterface.php
This file was deleted.
Oops, something went wrong.
60 changes: 0 additions & 60 deletions
60
packages/EasyWebhook/src/Bridge/Doctrine/StatementProviders/SqlStatementProvider.php
This file was deleted.
Oops, something went wrong.
61 changes: 0 additions & 61 deletions
61
packages/EasyWebhook/src/Bridge/Doctrine/StatementProviders/SqliteStatementProvider.php
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters