Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

new structure (in progress) #3

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
382 changes: 60 additions & 322 deletions bluem-db.php

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@
"yoast/phpunit-polyfills": "^1.0",
"vimeo/psalm": "^4.16"
},
"autoload": {
"psr-4": {
"Bluem\\WooCommerce\\": "src"
}
},
"config": {
"allow-plugins": {
"composer/package-versions-deprecated": false
Expand Down
165 changes: 165 additions & 0 deletions src/Application/RequestRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
<?php

namespace Bluem\WooCommerce\Application;

use Bluem\BluemPHP\Domain\Request;
use Bluem\BluemPHP\Infrastructure\DatabaseService;
use Bluem\BluemPHP\Infrastructure\RequestFactory;
use JsonException;

class RequestRepository
{
private DatabaseService $databaseService;
private RequestFactory $requestFactory;
private string $requestTableName = "bluem_requests";
private string $requestLinksTableName = "bluem_requests_links";
private string $logTableName = "bluem_requests_log";
private WP_User $currentUser;

public function __construct()
{

$this->databaseService = new DatabaseService();
$this->requestFactory = new RequestFactory();
$this->currentUser = wp_get_current_user();
}

public function add(array $request_array): ?Request
{
$request_object = $this->requestFactory->fromArray($request_array);

// validate request object
if ( ! $this->validatedRequestObject( $request_object ) ) {
return null;
}

$inserted = $this->databaseService->insert(
$this->requestTableName,
$request_object->toArray()
);

if($inserted !== -1) {
$result = $request_object->withId($this->databaseService->getInsertedId());

if ( isset( $result->orderId )
&& $result->orderId !== ""
) {
$this->addLinkToRequest($result->id, $result->orderId);
}

$this->addRequestLogItem(
$result->id,
"Created request"
);
return $result;
}

return null;
}

public function addLinkToRequest(int $requestId, int $itemId): void
{
$this->databaseService->insert($this->requestLinksTableName, [
'request_id' => $requestId,
'item_id' => $itemId,
'item_type' => "order"
]);
}

public function updateRequest($request_id, array $updates): bool
{

$update_result = $this->databaseService->update(
$this->requestTableName,
$updates,
[
'id' => $request_id
]
);

if ( $update_result ) {
try {
$this->addRequestLogItem(
$request_id,
"Updated request. New data: " . json_encode($updates, JSON_THROW_ON_ERROR)
);
} catch (JsonException $e) {}

return true;
}

return false;
}

public function getRequest(int $request_id): ?Request
{
$data = $this->databaseService->getById(
$this->requestTableName,
$request_id
);

if($data!==null) {
return $this->requestFactory->fromArray(
$data
);
}

return null;
}

public function addRequestLogItem($request_id, $description): int
{
return $this->databaseService->insert(
$this->logTableName,
[
'request_id' => $request_id,
'description' => $description,
'timestamp' => date( "Y-m-d H:i:s" ),
'user_id' => $this->currentUser->ID
]
);

}

public function deleteRequest(int $request_id): bool
{
$deleteRequest = $this->databaseService->delete( $this->requestTableName, [ 'id' => $request_id ] );
$deleteRequestLogs = $this->databaseService->delete( $this->logTableName, [ 'request_id' => $request_id ] );

return $deleteRequest && $deleteRequestLogs;
}

// Helper functions

private function validatedRequestObject( Request $request ): bool {
// check if required keys are present and well-formed
// if ( ! $this->requestWellFormed( $request ) ) {
// return false;
// }
return true;
}

public function getRequestLogs(int $requestId): ?array
{
return $this->databaseService->query("bluem_requests_log", "SELECT * FROM {TABLENAME} WHERE `request_id` = $requestId ORDER BY `timestamp` DESC" );
}

public function getRequestLinksByItemId(int $requestId): ?array
{
return $this->databaseService->query("bluem_requests_links", "SELECT * FROM {TABLENAME} WHERE `item_id` = {$requestId} and `item_type` = 'order'ORDER BY `timestamp` DESC" );
}

public function getRequestLinksByRequestId(int $requestId): ?array
{
return $this->databaseService->query("bluem_requests_links", "SELECT * FROM {TABLENAME} WHERE `request_id` = {$id} ORDER BY `timestamp` DESC" );
}


private function requestWellFormed(array $request_array): bool
{
$request = $this->requestFactory->fromArray($request_array);

// @todo: check all available fields on their format
return true;
}
}
12 changes: 12 additions & 0 deletions src/Domain/DatabaseMigration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Bluem\WooCommerce\Domain;

final class DatabaseMigration
{
public string $sql;

public function __construct(string $sql) {
$this->sql = $sql;
}
}
65 changes: 65 additions & 0 deletions src/Domain/Request.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

namespace Bluem\WooCommerce\Domain;

class Request
{
public int $id;

public string $entrance_code;
public string $transaction_id;
public string $transaction_url;
public string $description;
public string $debtor_reference;
private ?string $user_id;
private string $timestamp;
public ?string $orderId;
public array $payload;

public function __construct(
$entrance_code,
$transaction_id,
$transaction_url,
$description,
$debtor_reference,
$environment,
$user_id,
$orderId,
$payload = ['environment' => $environment]
) {
$this->debtor_reference = $debtor_reference;
$this->description = $description;
$this->transaction_url = $transaction_url;
$this->transaction_id = $transaction_id;
$this->entrance_code = $entrance_code;
$this->user_id = $user_id;
$this->timestamp = date("Y-m-d H:i:s") ?: '';
$this->type = "identity";
$this->payload =$payload;
$this->orderId = $orderId;
}

public function withId(int $getInsertedId): Request
{
$copy = clone $this;
$copy->id = $getInsertedId;

return $copy;
}

public function toArray(): array
{
return [
'debtor_reference' => $this->debtor_reference,
'description' => $this->description,
'transaction_url' => $this->transaction_url,
'transaction_id' => $this->transaction_id,
'entrance_code' => $this->entrance_code,
'user_id' => $this->user_id,
'timestamp' => $this->timestamp,
'type' => $this->type,
'payload' => $this->payload,
'orderId' => $this->orderId,
];
}
}
102 changes: 102 additions & 0 deletions src/Infrastructure/DatabaseMigrationRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?php

namespace Bluem\WooCommerce\Infrastructure;

use Bluem\WooCommerce\Domain\DatabaseMigration;

class DatabaseMigrationRepository
{
/**
* @return DatabaseMigration[]
*/
public function getMigrations(): array
{
global $wpdb, $bluem_db_version;

$installed_ver = (float) get_option( "bluem_db_version" );
if ( !empty( $installed_ver ) && $installed_ver >= $bluem_db_version ) {
// up to date
return [];
}

// Define table names
$table_name_storage = $wpdb->prefix . 'bluem_storage';
$table_name_requests = $wpdb->prefix . 'bluem_requests';
$table_name_links = $wpdb->prefix . 'bluem_requests_links';
$table_name_logs = $wpdb->prefix . 'bluem_requests_log';

$charset_collate = $wpdb->get_charset_collate();

$migrations = [];
/**
* Create tables.
*/
$migrations[] = new DatabaseMigration("CREATE TABLE IF NOT EXISTS `$table_name_requests` (
id mediumint(9) NOT NULL AUTO_INCREMENT,
user_id mediumint(9) NOT NULL,
transaction_id varchar(64) NOT NULL,
entrance_code varchar(64) NOT NULL,
transaction_url varchar(256) NOT NULL,
timestamp timestamp DEFAULT NOW() NOT NULL,
description tinytext NOT NULL,
debtor_reference varchar(64) NOT NULL,
type varchar(16) DEFAULT '' NOT NULL,
status varchar(16) DEFAULT 'created' NOT NULL,
order_id mediumint(9) DEFAULT NULL,
payload text NOT NULL,
PRIMARY KEY (id)
) $charset_collate;");

$migrations[] = new DatabaseMigration("CREATE TABLE IF NOT EXISTS `$table_name_logs` (
id mediumint(9) NOT NULL AUTO_INCREMENT,
request_id mediumint(9) NOT NULL,
timestamp timestamp DEFAULT NOW() NOT NULL,
description varchar(512) NOT NULL,
user_id mediumint(9) NULL,
PRIMARY KEY (id)
) $charset_collate;");

$migrations[] = new DatabaseMigration("CREATE TABLE IF NOT EXISTS `$table_name_links` (
id mediumint(9) NOT NULL AUTO_INCREMENT,
request_id mediumint(9) NOT NULL,
item_id mediumint(9) NOT NULL,
item_type varchar(32) NOT NULL DEFAULT 'order',
timestamp timestamp DEFAULT NOW() NOT NULL,
PRIMARY KEY (id)
) $charset_collate;");

$migrations[] = new DatabaseMigration("CREATE TABLE IF NOT EXISTS `$table_name_storage` (
id mediumint(9) NOT NULL AUTO_INCREMENT,
token varchar(191) NOT NULL,
secret varchar(191) NOT NULL,
data longtext NOT NULL,
timestamp timestamp DEFAULT NOW() NOT NULL,
PRIMARY KEY (id)
) $charset_collate;");

/**
* Check for previous installed versions
* Migrate old tables to new tables including wp-prefix.
* Old tables in release version <= 1.3.
*/
if (!empty($installed_ver) && $installed_ver <= '1.3') {
$bluem_requests_table_exists = $wpdb->get_var("SHOW TABLES LIKE 'bluem_requests'") === 'bluem_requests';
$bluem_requests_links_table_exists = $wpdb->get_var("SHOW TABLES LIKE 'bluem_requests_log'") === 'bluem_requests_log';
$bluem_requests_log_table_exists = $wpdb->get_var("SHOW TABLES LIKE 'bluem_requests_links'") === 'bluem_requests_links';

if ( $bluem_requests_table_exists ) {
$migrations[] = new DatabaseMigration("INSERT INTO `$table_name_requests` SELECT * FROM bluem_requests;");
}

if ( $bluem_requests_log_table_exists ) {
$migrations[] = new DatabaseMigration("INSERT INTO `$table_name_logs` SELECT * FROM bluem_requests_log;");
}

if ( $bluem_requests_links_table_exists ) {
$migrations[] = new DatabaseMigration("INSERT INTO `$table_name_links` SELECT * FROM bluem_requests_links;");
}
}

return $migrations;
}
}
Loading
Loading