| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
namespace JFB_Modules\Gateways\Db_Models; |
| 5 |
|
| 6 |
use Jet_Form_Builder\Db_Queries\Base_Db_Model; |
| 7 |
use Jet_Form_Builder\Db_Queries\Exceptions\Sql_Exception; |
| 8 |
use Jet_Form_Builder\Exceptions\Query_Builder_Exception; |
| 9 |
use JFB_Modules\Gateways\Query_Views\Payer_View; |
| 10 |
|
| 11 |
// If this file is called directly, abort. |
| 12 |
if ( ! defined( 'WPINC' ) ) { |
| 13 |
die; |
| 14 |
} |
| 15 |
|
| 16 |
class Payer_Model extends Base_Db_Model { |
| 17 |
|
| 18 |
/** |
| 19 |
* @inheritDoc |
| 20 |
*/ |
| 21 |
public static function table_name(): string { |
| 22 |
return 'payers'; |
| 23 |
} |
| 24 |
|
| 25 |
/** |
| 26 |
* @inheritDoc |
| 27 |
*/ |
| 28 |
public static function schema(): array { |
| 29 |
return array( |
| 30 |
'id' => 'bigint(20) NOT NULL AUTO_INCREMENT', |
| 31 |
'user_id' => 'bigint(20)', |
| 32 |
'payer_id' => 'varchar(100)', |
| 33 |
'first_name' => 'varchar(100)', |
| 34 |
'last_name' => 'varchar(100)', |
| 35 |
'email' => 'varchar(255)', |
| 36 |
'created_at' => 'TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP', |
| 37 |
'updated_at' => 'TIMESTAMP NOT NULL', |
| 38 |
); |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* @inheritDoc |
| 43 |
*/ |
| 44 |
public static function schema_keys(): array { |
| 45 |
return array( |
| 46 |
'id' => 'primary key', |
| 47 |
'payer_id' => 'index', |
| 48 |
'email' => 'index', |
| 49 |
); |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* @param $payer |
| 54 |
* |
| 55 |
* @return int |
| 56 |
* @throws Sql_Exception |
| 57 |
*/ |
| 58 |
public static function insert_or_update( $payer ): int { |
| 59 |
$payer_id = $payer['payer_id'] ?? ''; |
| 60 |
|
| 61 |
try { |
| 62 |
$find_payer = Payer_View::find( |
| 63 |
array( |
| 64 |
'payer_id' => $payer_id, |
| 65 |
) |
| 66 |
)->query()->query_one(); |
| 67 |
|
| 68 |
} catch ( Query_Builder_Exception $exception ) { |
| 69 |
return ( new self() )->insert( $payer ); |
| 70 |
} |
| 71 |
|
| 72 |
self::update_payer( $payer ); |
| 73 |
|
| 74 |
return (int) ( $find_payer['id'] ?? 0 ); |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* @param $payer |
| 79 |
* |
| 80 |
* @return int |
| 81 |
*/ |
| 82 |
public static function update_payer( $payer ): int { |
| 83 |
$where = array(); |
| 84 |
|
| 85 |
if ( isset( $payer['id'] ) ) { |
| 86 |
$where['id'] = $payer['id']; |
| 87 |
} elseif ( isset( $payer['payer_id'] ) ) { |
| 88 |
$where['payer_id'] = $payer['payer_id']; |
| 89 |
} |
| 90 |
unset( $payer['id'], $payer['payer_id'] ); |
| 91 |
|
| 92 |
return ( new static() )->update_soft( $payer, $where ); |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
} |
| 97 |
|