Skip to content

Commit

Permalink
Merge pull request #3 from MakairaIO/queue
Browse files Browse the repository at this point in the history
MKGO-15 Module and Database Installation
  • Loading branch information
hmennen90 authored Jan 23, 2024
2 parents 3ba385b + da50aaf commit 3109723
Show file tree
Hide file tree
Showing 7 changed files with 236 additions and 34 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
Build/
.DS_Store
16 changes: 5 additions & 11 deletions App/Actions/Export.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Gambio\Core\Application\Http\Request;
use Gambio\Core\Application\Http\Response;
use GXModules\Makaira\GambioConnect\App\GambioConnectService;
use GXModules\Makaira\GambioConnect\GambioConnectInstaller;

/**
* Class Export
Expand All @@ -17,17 +18,10 @@
*/
class Export extends AbstractAction
{
/**
* @var GambioConnectService
*/
private $service;



public function __construct(GambioConnectService $service)
{
$this->service = $service;
}
public function __construct(
protected GambioConnectService $service
)
{}


/**
Expand Down
85 changes: 85 additions & 0 deletions App/ChangesService.php
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]);
}
}
89 changes: 89 additions & 0 deletions App/Models/Change.php
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;
}
}
33 changes: 11 additions & 22 deletions GXModule.json
100755 → 100644
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"
}
]
}
}
35 changes: 35 additions & 0 deletions GambioConnectInstaller.php
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
);
}
}
11 changes: 10 additions & 1 deletion GambioConnectServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

use Gambio\Admin\Modules\Product\Submodules\Variant\Model\Events\UpdatedProductVariantsStock;
use Gambio\Core\Configuration\Services\ConfigurationFinder;
use GXModules\Makaira\GambioConnect\App\ChangesService;
use GXModules\Makaira\GambioConnect\App\Documents\MakairaProduct;
use GXModules\Makaira\GambioConnect\App\EventListeners\VariantUpdateEventListener;

Expand All @@ -33,6 +34,7 @@ class GambioConnectServiceProvider extends AbstractModuleServiceProvider
public function provides(): array
{
return [
GambioConnectInstaller::class,
GambioConnectOverview::class,
Export::class,
VariantUpdateEventListener::class,
Expand All @@ -57,13 +59,20 @@ public function register(): void
->addArgument(ProductVariantsReadService::class)
->addArgument(AdditionalOptionReadService::class)
->addArgument(Connection::class)
->addArgument(MakairaLogger::class);
->addArgument(MakairaLogger::class)
->addArgument(ChangesService::class);

$this->application->registerShared(ChangesService::class)
->addArgument(Connection::class);

$this->application->registerShared(VariantUpdateEventListener::class)
->addArgument(GambioConnectService::class);

$this->application->registerShared(MakairaProduct::class)
->addArgument(ProductVariantsReadService::class);

$this->application->registerShared(GambioConnectInstaller::class)
->addArgument(Connection::class);
}

public function boot(): void
Expand Down

0 comments on commit 3109723

Please sign in to comment.