From cb2b2beda06e3de4c5448a112dcfd31b7107d580 Mon Sep 17 00:00:00 2001 From: "daan.rijpkema" Date: Fri, 20 Oct 2023 17:10:37 +0200 Subject: [PATCH] Moving more calls to get, update, add and delete requests to the repository and developing the database service to deal with that. --- bluem-db.php | 189 +++++-------------------- src/Application/RequestRepository.php | 143 +++++++++++++++---- src/Domain/Request.php | 37 ++++- src/Infrastructure/DatabaseService.php | 33 ++++- src/Infrastructure/RequestFactory.php | 23 +++ 5 files changed, 236 insertions(+), 189 deletions(-) create mode 100644 src/Infrastructure/RequestFactory.php diff --git a/bluem-db.php b/bluem-db.php index 6b2f53c..0376a53 100644 --- a/bluem-db.php +++ b/bluem-db.php @@ -4,6 +4,7 @@ // no need for a deactivation hook yet. use Bluem\BluemPHP\Application\RequestRepository; +use Bluem\BluemPHP\Domain\Request; /** * Initialize a database table for the requests. @@ -123,52 +124,16 @@ function bluem_db_check() { add_action( 'plugins_loaded', 'bluem_db_check' ); // request specific functions -function bluem_db_create_request( $request_object ) { +function bluem_db_create_request( $request_array ) { $requestRepository = new RequestRepository(); - $insert_result = $requestRepository->add($request_object); + $request = $requestRepository->add($request_array); - if ( $insert_result ) { - $request_id = $insert_result->id; - - $request_object = (object) $request_object; - - if ( isset( $request_object->order_id ) - && ! is_null( $request_object->order_id ) - && $request_object->order_id != "" - ) { - bluem_db_create_link( - $request_id, - $request_object->order_id, - "order" - ); - } - bluem_db_request_log( - $request_id, - "Created request" - ); - - return $wpdb->insert_id; - } else { - return - 1; - } + return $request !== null ? $request->id : -1; } -function bluem_db_request_log( $request_id, $description, $log_data = [] ) { - global $wpdb, $current_user; - - // date_default_timezone_set('Europe/Amsterdam'); - // $wpdb->time_zone = 'Europe/Amsterdam'; - - return $wpdb->insert( - $wpdb->prefix . "bluem_requests_log", - [ - 'request_id' => $request_id, - 'description' => $description, - 'timestamp' => date( "Y-m-d H:i:s" ), - 'user_id' => $current_user->ID - ] - ); +function bluem_db_request_log( $request_id, $description ) { + (new RequestRepository())->addRequestLogItem($request_id, $description); } /** @@ -316,85 +281,24 @@ function bluem_db_update_storage( $id, $object ) { * * @return bool */ -function bluem_db_update_request( $request_id, $request_object ) { - global $wpdb; - - // date_default_timezone_set('Europe/Amsterdam'); - // $wpdb->time_zone = 'Europe/Amsterdam'; - - if ( ! bluem_db_validated_request_well_formed( $request_object ) ) { - return false; - } - $update_result = $wpdb->update( - $wpdb->prefix . "bluem_requests", - $request_object, - [ - 'id' => $request_id - ] - ); - - if ( $update_result ) { - bluem_db_request_log( - $request_id, - "Updated request. New data: " . json_encode( $request_object ) - ); - - return true; - } else { - return false; - } -} - -/** - * Get fields within any request - * @return string[] - */ -function bluem_db_get_request_fields() { - return [ - 'id', - 'entrance_code', - 'transaction_id', - 'transaction_url', - 'user_id', - 'timestamp', - 'description', - 'type', - 'debtor_reference', - 'order_id', - 'payload' - ]; +function bluem_db_update_request( $request_id, $request_object ): bool +{ + return (new RequestRepository())->updateRequest($request_id, $request_object); } /** * Get the request for a given ID, or false if not found - * - * @param $request_id - * - * @return bool|object */ -function bluem_db_get_request_by_id( string $request_id ) { +function bluem_db_get_request_by_id( string $request_id ): ?Request +{ // @todo change to only accept int for $request_id - $res = bluem_db_get_requests_by_keyvalue( - 'id', - $request_id - ); - - return $res[0] ?? false; + return (new RequestRepository())->getRequest($request_id); } -function bluem_db_delete_request_by_id( $request_id ) { - global $wpdb; - - // date_default_timezone_set('Europe/Amsterdam'); - // $wpdb->time_zone = 'Europe/Amsterdam'; - - $wpdb->show_errors(); - - $query = $wpdb->delete( $wpdb->prefix . 'bluem_requests', [ 'id' => $request_id ] ); - $query2 = $wpdb->delete( $wpdb->prefix . 'bluem_requests_log', [ 'request_id' => $request_id ] ); - - return $query && $query2; +function bluem_db_delete_request_by_id( $request_id ): bool +{ + return (new RequestRepository())->deleteRequest($request_id); } function bluem_db_get_request_by_debtor_reference( $debtor_reference ) { @@ -555,11 +459,6 @@ function bluem_db_get_most_recent_request( $user_id = null, $type = "mandates" ) global $wpdb; - // date_default_timezone_set('Europe/Amsterdam'); - // $wpdb->time_zone = 'Europe/Amsterdam'; - - $wpdb->show_errors(); //setting the Show or Display errors option to true - $query = "SELECT * FROM `" . $wpdb->prefix . "bluem_requests` WHERE `user_id` = '{$user_id}' @@ -585,51 +484,46 @@ function bluem_db_get_most_recent_request( $user_id = null, $type = "mandates" ) function bluem_db_put_request_payload( $request_id, $data ) { $request = bluem_db_get_request_by_id( $request_id ); - if ( $request->payload !== "" ) { - try { - $newPayload = json_decode( $request->payload ); - } catch ( Throwable $th ) { - $newPayload = new Stdclass; - } + if(!$request) { + return; + } + + if ( $request->payload !== [] ) { + $newPayload = $request->payload ; } else { - $newPayload = new Stdclass; + $newPayload = []; } foreach ( $data as $k => $v ) { - $newPayload->$k = $v; + $newPayload[$k] = $v; } - bluem_db_update_request( - $request_id, - [ - 'payload' => json_encode( $newPayload ) - ] - ); + try { + bluem_db_update_request( + $request_id, + [ + 'payload' => json_encode($newPayload, JSON_THROW_ON_ERROR) + ] + ); + } catch (JsonException $e) { + + } } function bluem_db_get_logs_for_request( $id ) { global $wpdb; - // date_default_timezone_set('Europe/Amsterdam'); - // $wpdb->time_zone = 'Europe/Amsterdam'; - return $wpdb->get_results( "SELECT * FROM `" . $wpdb->prefix . "bluem_requests_log` WHERE `request_id` = $id ORDER BY `timestamp` DESC" ); } function bluem_db_get_links_for_order( $id ) { global $wpdb; - // date_default_timezone_set('Europe/Amsterdam'); - // $wpdb->time_zone = 'Europe/Amsterdam'; - return $wpdb->get_results( "SELECT * FROM `" . $wpdb->prefix . "bluem_requests_links` WHERE `item_id` = {$id} and `item_type` = 'order'ORDER BY `timestamp` DESC" ); } function bluem_db_get_links_for_request( $id ) { global $wpdb; - // date_default_timezone_set('Europe/Amsterdam'); - // $wpdb->time_zone = 'Europe/Amsterdam'; - return $wpdb->get_results( "SELECT * FROM `" . $wpdb->prefix . "bluem_requests_links` WHERE `request_id` = {$id} ORDER BY `timestamp` DESC" ); } @@ -644,20 +538,5 @@ function bluem_db_create_link( $request_id, $item_id, $item_type = "order" ) { return; } - $insert_result = $wpdb->insert( - $wpdb->prefix . "bluem_requests_links", - [ - 'request_id' => $request_id, - 'item_id' => $item_id, - 'item_type' => $item_type - ] - ); - - if ( $insert_result ) { - $link_id = $wpdb->insert_id; - - return $link_id; - } else { - return - 1; - } + (new RequestRepository())->addLinkToRequest($request_id, $item_id); } diff --git a/src/Application/RequestRepository.php b/src/Application/RequestRepository.php index 1140d44..0d5caea 100644 --- a/src/Application/RequestRepository.php +++ b/src/Application/RequestRepository.php @@ -5,60 +5,147 @@ use Bluem\BluemPHP\Domain\Request; use Bluem\BluemPHP\Infrastructure\DatabaseService; use Bluem\BluemPHP\Infrastructure\RequestFactory; +use JsonException; class RequestRepository { private DatabaseService $databaseService; - private string $requestTableName; + 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->requestTableName = "bluem_requests"; + $this->databaseService = new DatabaseService(); + $this->requestFactory = new RequestFactory(); + $this->currentUser = wp_get_current_user(); } - public function add($request_object): int + public function add(array $request_array): ?Request { + $request_object = $this->requestFactory->fromArray($request_array); + // validate request object if ( ! $this->validatedRequestObject( $request_object ) ) { - return -1; + return null; } - return $this->databaseService->insert( + $inserted = $this->databaseService->insert( $this->requestTableName, - $request_object + $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; } - private function validatedRequestObject( Request $request ): bool { - // check if present - // entrance_code - // transaction_id - // transaction_url - // user_id - // timestamp - // description - // type - - // optional fields - // debtor_reference - // order_id - // payload - - // and well formed - if ( ! $this->requestWellFormed( $request ) ) { - return false; + 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 true; + return false; } - private function requestWellFormed(Request $request): bool + public function getRequest(int $request_id): ?Request { - // @todo: check all available fields on their format + $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($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; } + + private function requestWellFormed(array $request_array): bool + { + $request = $this->requestFactory->fromArray($request_array); + + // @todo: check all available fields on their format + return true; + } } diff --git a/src/Domain/Request.php b/src/Domain/Request.php index b458f43..458bff9 100644 --- a/src/Domain/Request.php +++ b/src/Domain/Request.php @@ -4,6 +4,8 @@ class Request { + public int $id; + public string $entrance_code; public string $transaction_id; public string $transaction_url; @@ -11,8 +13,8 @@ class Request public string $debtor_reference; private ?string $user_id; private string $timestamp; - private ?string $order_id; - private array $payload; + public ?string $orderId; + public array $payload; public function __construct( $entrance_code, @@ -21,7 +23,9 @@ public function __construct( $description, $debtor_reference, $environment, - $user_id + $user_id, + $orderId, + $payload = ['environment' => $environment] ) { $this->debtor_reference = $debtor_reference; $this->description = $description; @@ -31,6 +35,31 @@ public function __construct( $this->user_id = $user_id; $this->timestamp = date("Y-m-d H:i:s") ?: ''; $this->type = "identity"; - $this->payload = ['environment' => $environment]; + $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, + ]; } } diff --git a/src/Infrastructure/DatabaseService.php b/src/Infrastructure/DatabaseService.php index 560161b..7ddc3e0 100644 --- a/src/Infrastructure/DatabaseService.php +++ b/src/Infrastructure/DatabaseService.php @@ -10,14 +10,20 @@ public function __construct() { global $wpdb; + // @todo: set timezone +// date_default_timezone_set('Europe/Amsterdam'); +// $wpdb->time_zone = 'Europe/Amsterdam'; + // for testing envs: +// $wpdb->show_errors(); + $this->wpdb = $wpdb; } private function prefixTable(string $tableName):string { - return $this->wpdb->prefix.$tableName; + return $this->wpdb->prefix . $tableName; } - public function insert($tableName, $data): int + public function insert(string $tableName, array $data): int { $result = $this->wpdb->insert( $this->prefixTable($tableName), @@ -31,4 +37,27 @@ public function getInsertedId(): int { return $this->wpdb->insert_id; } + + public function update(string $tableName, $request_object, array $conditions) + { + return $this->wpdb->update( + $this->prefixTable($tableName), + $request_object, + $conditions + ); + } + + public function getById(string $tableName, $idValue, string $idField= 'id'): ?array + { + $data = $this->wpdb->get_results( + sprintf("SELECT * FROM %s WHERE %s='%s' LIMIT 1", $tableName, $idField, $idValue) + ); + + return $data !== null ? $data[0] : null; + } + + public function delete(string $tableName, array $conditions): bool + { + return $this->wpdb->delete($tableName, $conditions); + } } diff --git a/src/Infrastructure/RequestFactory.php b/src/Infrastructure/RequestFactory.php new file mode 100644 index 0000000..aa9dc7e --- /dev/null +++ b/src/Infrastructure/RequestFactory.php @@ -0,0 +1,23 @@ +