-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from MakairaIO/queue
MKGO-15 Module and Database Installation
- Loading branch information
Showing
7 changed files
with
236 additions
and
34 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1 +1,2 @@ | ||
Build/ | ||
.DS_Store |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace GXModules\Makaira\GambioConnect\App; | ||
|
||
use Doctrine\DBAL\Connection; | ||
use GXModules\Makaira\GambioConnect\App\Models\Change; | ||
|
||
class ChangesService | ||
{ | ||
public const TABLE_NAME = 'makaira_connect_changes'; | ||
|
||
|
||
function __construct( | ||
private Connection $connection, | ||
) { | ||
} | ||
|
||
|
||
public function dispatch(string $gambioId, string $type, string $comment = ""): bool | ||
{ | ||
$sql = ' | ||
INSERT INTO ' . self::TABLE_NAME . ' (gabio_id, type, comment) | ||
VALUES (:gambioId, :type, :comment) | ||
ON DUPLICATE KEY UPDATE | ||
gambio_id = VALUES(gambioId), | ||
type = VALUES(type) | ||
'; | ||
|
||
$stmt = $this->connection->prepare($sql); | ||
|
||
$stmt->bindValue('gambioId', $gambioId, \PDO::PARAM_STR); | ||
$stmt->bindValue('type', $type, \PDO::PARAM_STR); | ||
$stmt->bindValue('comment', $comment, \PDO::PARAM_STR); | ||
|
||
$stmt->executeStatement(); | ||
|
||
return true; | ||
} | ||
|
||
|
||
public function consume() | ||
{ | ||
|
||
|
||
$this->connection->beginTransaction(); | ||
|
||
try { | ||
$sqlSelect = 'SELECT * FROM ' . self::TABLE_NAME . ' WHERE consumed_at IS NULL ORDER BY id ASC LIMIT 1'; | ||
$record = $this->connection->fetchAssociative($sqlSelect); | ||
|
||
if ($record !== false) { | ||
|
||
$change = new Change( | ||
(int) $record['id'], | ||
$record['gambioid'], | ||
$record['type'], | ||
$record['comment'], | ||
$record['created_at'], | ||
$record['consumed_at'] | ||
); | ||
|
||
$sqlUpdate = 'UPDATE ' . self::TABLE_NAME . ' SET consumed_at = now() WHERE id = :id'; | ||
$stmt = $this->connection->prepare($sqlUpdate); | ||
$stmt->bindValue('id', $change->getId(), \PDO::PARAM_INT); | ||
$stmt->executeStatement(); | ||
|
||
$this->connection->commit(); | ||
return $change; | ||
} | ||
} catch (\Exception $e) { | ||
$this->connection->rollBack(); | ||
|
||
echo 'Error: ' . $e->getMessage(); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
public function delete(int $id): void | ||
{ | ||
$this->connection->delete(self::TABLE_NAME, ['id' => $id]); | ||
} | ||
} |
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,89 @@ | ||
<?php | ||
|
||
namespace GXModules\Makaira\GambioConnect\App\Models; | ||
|
||
|
||
class Change | ||
{ | ||
public function __construct( | ||
private int $id, | ||
private string $gambioid, | ||
private string $type, | ||
private string $created_at, | ||
private string $comment = "", | ||
private ?string $consumed_at = null | ||
) | ||
{} | ||
|
||
public function toArray(): array | ||
{ | ||
return [ | ||
'id' => $this->getId(), | ||
'gambio_id' => $this->getGambioId(), | ||
'type' => $this->getType(), | ||
'comment' => $this->getComment(), | ||
'created_at' => $this->getCreatedAt(), | ||
'consumed_at' => $this->getConsumedAt(), | ||
]; | ||
} | ||
|
||
public function getId(): int | ||
{ | ||
return $this->id; | ||
} | ||
|
||
public function setId(int $id): void | ||
{ | ||
$this->id = $id; | ||
} | ||
|
||
public function getGambioId(): string | ||
{ | ||
return $this->gambioid; | ||
} | ||
|
||
public function setGambioId(string $gambioId): void | ||
{ | ||
$this->gambioId = $gambioId; | ||
} | ||
|
||
public function getType(): string | ||
{ | ||
return $this->type; | ||
} | ||
|
||
public function setType(string $type): void | ||
{ | ||
$this->type = $type; | ||
} | ||
|
||
public function getComment(): string | ||
{ | ||
return $this->comment; | ||
} | ||
|
||
public function setComment(string $comment): void | ||
{ | ||
$this->comment = $comment; | ||
} | ||
|
||
public function getCreatedAt(): string | ||
{ | ||
return $this->createdAt; | ||
} | ||
|
||
public function setCreatedAt(string $createdAt): void | ||
{ | ||
$this->createdAt = $createdAt; | ||
} | ||
|
||
public function getConsumedAt(): string | ||
{ | ||
return $this->consumedAt; | ||
} | ||
|
||
public function setConsumedAt(string $consumedAt): void | ||
{ | ||
$this->consumedAt = $consumedAt; | ||
} | ||
} |
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 |
---|---|---|
@@ -1,24 +1,13 @@ | ||
{ | ||
"title": "gambioconnect.module_title", | ||
"description": "gambioconnect.module_description", | ||
"forceIncludingFiles": false, | ||
"configuration": [ | ||
{ | ||
"title": "gambioconnect.settings", | ||
"fields": { | ||
"makairaUrl": { | ||
"type": "text", | ||
"label": "gambioconnect.makaira_url" | ||
}, | ||
"makairaInstance": { | ||
"type": "text", | ||
"label": "gambioconnect.makaira_instance" | ||
}, | ||
"makairaSecret": { | ||
"type": "password", | ||
"label": "gambioconnect.makaira_secret" | ||
} | ||
} | ||
"title": "gambioconnect.module_title", | ||
"description": "gambioconnect.module_description", | ||
"forceIncludingFiles": true, | ||
"install": { | ||
"controller": "GXModules\\Makaira\\GambioConnect\\GambioConnectInstaller", | ||
"method": "onInstallation" | ||
}, | ||
"uninstall": { | ||
"controller": "GXModules\\Makaira\\GambioConnect\\GambioConnectInstaller", | ||
"method": "onUninstallation" | ||
} | ||
] | ||
} | ||
} |
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,35 @@ | ||
<?php | ||
|
||
namespace GXModules\Makaira\GambioConnect; | ||
|
||
use Doctrine\DBAL\Connection; | ||
use GXModules\Makaira\GambioConnect\App\ChangesService; | ||
|
||
class GambioConnectInstaller | ||
{ | ||
public function __construct( | ||
protected Connection $connection | ||
) {} | ||
|
||
|
||
public function onInstallation() { | ||
$this->connection->executeStatement( | ||
"CREATE TABLE IF NOT EXISTS `" . ChangesService::TABLE_NAME . "` ( | ||
`id` int(11) NOT NULL AUTO_INCREMENT, | ||
`gambio_id` varchar(255) NOT NULL, | ||
`type` varchar(255) NOT NULL, | ||
`comment` varchar(255) NOT NULL, | ||
`created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
`consumed_at` datetime DEFAULT NULL, | ||
PRIMARY KEY (`id`), | ||
UNIQUE INDEX idx_unique_gambio_id_type (gambio_id, type) | ||
)" | ||
); | ||
} | ||
|
||
public function onUninstallation() { | ||
$this->connection->executeStatement( | ||
"DROP TABLE IF EXISTS " . ChangesService::TABLE_NAME | ||
); | ||
} | ||
} |
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