Skip to content

Commit 1b103b2

Browse files
new structure (in progress)
1 parent 7b4a74d commit 1b103b2

File tree

4 files changed

+139
-55
lines changed

4 files changed

+139
-55
lines changed

bluem-db.php

Lines changed: 5 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
register_activation_hook( __FILE__, 'bluem_db_create_requests_table' );
44
// no need for a deactivation hook yet.
55

6+
use Bluem\BluemPHP\Application\RequestRepository;
7+
68
/**
79
* Initialize a database table for the requests.
810
* @return void
@@ -122,22 +124,12 @@ function bluem_db_check() {
122124

123125
// request specific functions
124126
function bluem_db_create_request( $request_object ) {
125-
global $wpdb;
126-
127-
// date_default_timezone_set('Europe/Amsterdam');
128-
// $wpdb->time_zone = 'Europe/Amsterdam';
129-
130-
if ( ! bluem_db_validated_request( $request_object ) ) {
131-
return - 1;
132-
}
127+
$requestRepository = new RequestRepository();
133128

134-
$insert_result = $wpdb->insert(
135-
$wpdb->prefix . "bluem_requests",
136-
$request_object
137-
);
129+
$insert_result = $requestRepository->add($request_object);
138130

139131
if ( $insert_result ) {
140-
$request_id = $wpdb->insert_id;
132+
$request_id = $insert_result->id;
141133

142134
$request_object = (object) $request_object;
143135

@@ -353,48 +345,6 @@ function bluem_db_update_request( $request_id, $request_object ) {
353345
}
354346
}
355347

356-
/**
357-
* check if all fields are well-formed
358-
*
359-
* @param [type] $request
360-
*
361-
* @return bool
362-
*/
363-
function bluem_db_validated_request_well_formed( $request ): bool {
364-
// @todo: check all available fields on their format
365-
return true;
366-
}
367-
368-
/**
369-
* check if all required fields are present and well-formed
370-
*
371-
* @param [type] $request
372-
*
373-
* @return void
374-
*/
375-
function bluem_db_validated_request( $request ) {
376-
// check if present
377-
// entrance_code
378-
// transaction_id
379-
// transaction_url
380-
// user_id
381-
// timestamp
382-
// description
383-
// type
384-
385-
// optional fields
386-
// debtor_reference
387-
// order_id
388-
// payload
389-
390-
// and well formed
391-
if ( ! bluem_db_validated_request_well_formed( $request ) ) {
392-
return false;
393-
}
394-
395-
return true;
396-
}
397-
398348
/**
399349
* Get fields within any request
400350
* @return string[]
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
namespace Bluem\BluemPHP\Application;
4+
5+
use Bluem\BluemPHP\Domain\Request;
6+
use Bluem\BluemPHP\Infrastructure\DatabaseService;
7+
use Bluem\BluemPHP\Infrastructure\RequestFactory;
8+
9+
class RequestRepository
10+
{
11+
private DatabaseService $databaseService;
12+
private string $requestTableName;
13+
14+
public function __construct()
15+
{
16+
$this->databaseService = new DatabaseService();
17+
18+
$this->requestTableName = "bluem_requests";
19+
}
20+
21+
public function add($request_object): int
22+
{
23+
// validate request object
24+
if ( ! $this->validatedRequestObject( $request_object ) ) {
25+
return -1;
26+
}
27+
28+
return $this->databaseService->insert(
29+
$this->requestTableName,
30+
$request_object
31+
);
32+
}
33+
34+
private function validatedRequestObject( Request $request ): bool {
35+
// check if present
36+
// entrance_code
37+
// transaction_id
38+
// transaction_url
39+
// user_id
40+
// timestamp
41+
// description
42+
// type
43+
44+
// optional fields
45+
// debtor_reference
46+
// order_id
47+
// payload
48+
49+
// and well formed
50+
if ( ! $this->requestWellFormed( $request ) ) {
51+
return false;
52+
}
53+
54+
return true;
55+
}
56+
57+
private function requestWellFormed(Request $request): bool
58+
{
59+
// @todo: check all available fields on their format
60+
return true;
61+
}
62+
63+
64+
}

src/Domain/Request.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace Bluem\BluemPHP\Domain;
4+
5+
class Request
6+
{
7+
public string $entrance_code;
8+
public string $transaction_id;
9+
public string $transaction_url;
10+
public string $description;
11+
public string $debtor_reference;
12+
private ?string $user_id;
13+
private string $timestamp;
14+
private ?string $order_id;
15+
private array $payload;
16+
17+
public function __construct(
18+
$entrance_code,
19+
$transaction_id,
20+
$transaction_url,
21+
$description,
22+
$debtor_reference,
23+
$environment,
24+
$user_id
25+
) {
26+
$this->debtor_reference = $debtor_reference;
27+
$this->description = $description;
28+
$this->transaction_url = $transaction_url;
29+
$this->transaction_id = $transaction_id;
30+
$this->entrance_code = $entrance_code;
31+
$this->user_id = $user_id;
32+
$this->timestamp = date("Y-m-d H:i:s") ?: '';
33+
$this->type = "identity";
34+
$this->payload = ['environment' => $environment];
35+
}
36+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace Bluem\BluemPHP\Infrastructure;
4+
5+
class DatabaseService
6+
{
7+
private $wpdb;
8+
9+
public function __construct()
10+
{
11+
global $wpdb;
12+
13+
$this->wpdb = $wpdb;
14+
}
15+
16+
private function prefixTable(string $tableName):string {
17+
return $this->wpdb->prefix.$tableName;
18+
}
19+
20+
public function insert($tableName, $data): int
21+
{
22+
$result = $this->wpdb->insert(
23+
$this->prefixTable($tableName),
24+
$data
25+
);
26+
27+
return $result ? $this->wpdb->insert_id : -1;
28+
}
29+
30+
public function getInsertedId(): int
31+
{
32+
return $this->wpdb->insert_id;
33+
}
34+
}

0 commit comments

Comments
 (0)