@@ -91,56 +91,82 @@ public static function testConnection()
91
91
private $ db_user ;
92
92
private $ db_pass ;
93
93
private $ db_connection ;
94
+ private $ callback_active ;
95
+ private $ callback_getLastUpdateDate ;
96
+ private $ callback_setLastUpdateDate ;
97
+ private $ callback_getOrderMinimum ;
98
+ private $ callback_setOrderMinimum ;
99
+ private $ callback_getOrderMaximum ;
100
+ private $ callback_setOrderMaximum ;
101
+ private $ callback_getCurrency ;
94
102
95
103
public function __construct ()
96
104
{
97
- //echo get_class( $this ) . "::__construct()\n";
98
-
99
105
$ this ->db_api = Config::get ('db.api ' );
100
- $ this ->db_host = Config::get ('db.host ' );
101
- $ this ->db_port = Config::get ('db.port ' );
102
- $ this ->db_database = Config::get ('db.database ' );
103
- $ this ->db_tablePrefix = Config::get ('db.tablePrefix ' );
104
- $ this ->db_user = Config::get ('db.user ' );
105
- $ this ->db_pass = Config::get ('db.pass ' );
106
-
107
- if ($ this ->db_api == 'mysqli ' ) {
108
- if (extension_loaded ('mysqli ' )) {
109
- /**
110
- * @see https://www.php.net/manual/en/mysqli.construct.php
111
- */
106
+ $ this ->callback_active = Config::get ('callback.active ' );
112
107
113
- if (empty ($ this ->db_host )) {
114
- $ this ->db_host = ini_get ('mysqli.default_host ' );
108
+ $ lifespan = Config::get ('persistence.lifespan ' );
109
+ if (!empty ($ lifespan )) {
110
+ foreach ($ this ->data as &$ merchantProperty ) {
111
+ if (isset ($ merchantProperty [ 'lifespan ' ])) {
112
+ $ merchantProperty [ 'lifespan ' ] = $ lifespan ;
115
113
}
114
+ }
115
+ }
116
116
117
- if (empty ($ this ->db_user )) {
118
- $ this ->db_user = ini_get ('mysqli.default_user ' );
119
- }
117
+ if (!empty ($ this ->db_api )) {
118
+ $ this ->db_host = Config::get ('db.host ' );
119
+ $ this ->db_port = Config::get ('db.port ' );
120
+ $ this ->db_database = Config::get ('db.database ' );
121
+ $ this ->db_tablePrefix = Config::get ('db.tablePrefix ' );
122
+ $ this ->db_user = Config::get ('db.user ' );
123
+ $ this ->db_pass = Config::get ('db.pass ' );
124
+ if ($ this ->db_api == 'mysqli ' ) {
125
+ if (extension_loaded ('mysqli ' )) {
126
+ /**
127
+ * @see https://www.php.net/manual/en/mysqli.construct.php
128
+ */
120
129
121
- if (empty ($ this ->db_pass )) {
122
- $ this ->db_pass = ini_get ('mysqli.default_pw ' );
123
- }
130
+ if (empty ($ this ->db_host )) {
131
+ $ this ->db_host = ini_get ('mysqli.default_host ' );
132
+ }
124
133
125
- if (empty ($ this ->db_port )) {
126
- $ this ->db_port = ini_get ('mysqli.default_port ' );
127
- }
134
+ if (empty ($ this ->db_user )) {
135
+ $ this ->db_user = ini_get ('mysqli.default_user ' );
136
+ }
137
+
138
+ if (empty ($ this ->db_pass )) {
139
+ $ this ->db_pass = ini_get ('mysqli.default_pw ' );
140
+ }
141
+
142
+ if (empty ($ this ->db_port )) {
143
+ $ this ->db_port = ini_get ('mysqli.default_port ' );
144
+ }
128
145
129
- # It's safe to suppress any errors/warnings/notices here because
130
- # if we can't connect we'll throw an Exception anyway.
131
- set_error_handler (function () {
132
- });
133
- $ this ->db_connection = new \mysqli ($ this ->db_host , $ this ->db_user , $ this ->db_pass , $ this ->db_database , $ this ->db_port );
134
- restore_error_handler ();
146
+ # It's safe to suppress any errors/warnings/notices here because
147
+ # if we can't connect we'll throw an Exception anyway.
148
+ set_error_handler (function () {
149
+ });
150
+ $ this ->db_connection = new \mysqli ($ this ->db_host , $ this ->db_user , $ this ->db_pass , $ this ->db_database , $ this ->db_port );
151
+ restore_error_handler ();
135
152
136
- if ($ this ->db_connection ->connect_errno ) {
137
- throw new Exception ($ this ->db_connection ->connect_error , $ this ->db_connection ->connect_errno );
153
+ if ($ this ->db_connection ->connect_errno ) {
154
+ throw new Exception ($ this ->db_connection ->connect_error , $ this ->db_connection ->connect_errno );
155
+ }
156
+ } else {
157
+ throw new Exception ("Required extension 'mysqli' not loaded " );
138
158
}
139
159
} else {
140
- throw new Exception ("Required extension 'mysqli' not loaded " );
160
+ throw new Exception ("No available database API " );
141
161
}
142
- } else {
143
- throw new Exception ("No available database API " );
162
+ } elseif (isset ($ this ->callback_active ) && $ this ->callback_active === true ) {
163
+ $ this ->callback_getLastUpdateDate = Config::get ('callback.getLastUpdateDate ' );
164
+ $ this ->callback_setLastUpdateDate = Config::get ('callback.setLastUpdateDate ' );
165
+ $ this ->callback_getOrderMinimum = Config::get ('callback.getOrderMinimum ' );
166
+ $ this ->callback_setOrderMinimum = Config::get ('callback.setOrderMinimum ' );
167
+ $ this ->callback_getOrderMaximum = Config::get ('callback.getOrderMaximum ' );
168
+ $ this ->callback_setOrderMaximum = Config::get ('callback.setOrderMaximum ' );
169
+ $ this ->callback_getCurrency = Config::get ('callback.getCurrency ' );
144
170
}
145
171
}
146
172
@@ -160,14 +186,6 @@ public function __destruct()
160
186
private function getProperty ($ property , $ merchant )
161
187
{
162
188
if (array_key_exists ($ property , $ this ->data )) {
163
- /**
164
- * E.g. Given the default value of $this->db_tablePrefix ("afterpay_"):
165
- * "Afterpay\SDK\Money" --> "\Money" --> "Money" --> "afterpay_Money"
166
- *
167
- * @todo Create a method for converting UpperCamelCase to snake_case
168
- * because the mysqli driver may be case insensitive.
169
- */
170
- $ table_name = preg_replace ('/[^a-z0-9_]+/i ' , '' , $ this ->db_tablePrefix . substr (strrchr ($ this ->data [ $ property ][ 'type ' ], '\\' ), 1 ));
171
189
$ need_to_get_fresh_data = false ;
172
190
$ properties_to_update = [];
173
191
$ return = null ;
@@ -180,6 +198,14 @@ private function getProperty($property, $merchant)
180
198
}
181
199
182
200
if ($ this ->db_api == 'mysqli ' ) {
201
+ /**
202
+ * E.g. Given the default value of $this->db_tablePrefix ("afterpay_"):
203
+ * "Afterpay\SDK\Money" --> "\Money" --> "Money" --> "afterpay_Money"
204
+ *
205
+ * @todo Create a method for converting UpperCamelCase to snake_case
206
+ * because the mysqli driver may be case insensitive.
207
+ */
208
+ $ table_name = preg_replace ('/[^a-z0-9_]+/i ' , '' , $ this ->db_tablePrefix . substr (strrchr ($ this ->data [ $ property ][ 'type ' ], '\\' ), 1 ));
183
209
$ escaped_table_name = $ this ->db_connection ->real_escape_string ($ table_name );
184
210
185
211
if (in_array ($ property , [ 'orderMinimum ' , 'orderMaximum ' ])) {
@@ -279,6 +305,33 @@ private function getProperty($property, $merchant)
279
305
}
280
306
}
281
307
}
308
+ } elseif ($ this ->callback_active == true ) {
309
+ $ callbackGetFnName = 'callback_get ' . ucfirst ($ property );
310
+ $ callbackSetFnName = 'callback_set ' . ucfirst ($ property );
311
+ $ updatedAt = date ('Y-m-d H:i:s ' , strtotime ('-1 year ' ));
312
+ if (is_callable ($ this ->callback_getLastUpdateDate )) {
313
+ $ updatedAt = call_user_func ($ this ->callback_getLastUpdateDate );
314
+ } else {
315
+ throw new Exception ("Function ' {$ this ->callback_getLastUpdateDate }' is not callable or doesn't exist " );
316
+ }
317
+ if (time () - strtotime ($ updatedAt ) > $ this ->data [ $ property ][ 'lifespan ' ]) {
318
+ // update minimum and maximum values
319
+ $ properties_to_update [] = $ property ;
320
+ $ need_to_get_fresh_data = true ;
321
+ } else {
322
+ if (is_callable ($ this ->{$ callbackGetFnName })) {
323
+ if (is_callable ($ this ->callback_getCurrency )) {
324
+ $ return = new Money ([
325
+ 'amount ' => call_user_func ($ this ->{$ callbackGetFnName }),
326
+ 'currency ' => call_user_func ($ this ->callback_getCurrency ),
327
+ ]);
328
+ } else {
329
+ throw new Exception ("Function ' {$ this ->callback_getCurrency }' is not callable or doesn't exist " );
330
+ }
331
+ } else {
332
+ throw new Exception ("Function ' {$ this ->{$ callbackGetFnName }}' is not callable or doesn't exist " );
333
+ }
334
+ }
282
335
} else {
283
336
$ need_to_get_fresh_data = true ;
284
337
}
@@ -334,6 +387,23 @@ private function getProperty($property, $merchant)
334
387
);
335
388
$ update_stmt ->execute ();
336
389
$ update_stmt ->close ();
390
+ } elseif ($ this ->callback_active == true ) {
391
+ if (is_callable ($ this ->callback_setOrderMinimum ) && isset ($ responseBody ->minimumAmount )) {
392
+ call_user_func ($ this ->callback_setOrderMinimum , $ responseBody ->minimumAmount ->amount );
393
+ } else {
394
+ throw new Exception ("Function ' {$ this ->callback_setOrderMinimum }' is not callable or doesn't exist " );
395
+ }
396
+ if (is_callable ($ this ->callback_setOrderMaximum ) && isset ($ responseBody ->maximumAmount )) {
397
+ call_user_func ($ this ->callback_setOrderMaximum , $ responseBody ->maximumAmount ->amount );
398
+ } else {
399
+ throw new Exception ("Function ' {$ this ->callback_setOrderMaximum }' is not callable or doesn't exist " );
400
+ }
401
+ if (is_callable ($ this ->callback_setLastUpdateDate )) {
402
+ $ now = date ('Y-m-d H:i:s ' );
403
+ call_user_func ($ this ->callback_setLastUpdateDate , $ now );
404
+ } else {
405
+ throw new Exception ("Function ' {$ this ->callback_setLastUpdateDate }' is not callable or doesn't exist " );
406
+ }
337
407
}
338
408
} else {
339
409
if ($ this ->db_api == 'mysqli ' ) {
0 commit comments