Skip to content

Commit

Permalink
use a lock key constant
Browse files Browse the repository at this point in the history
  • Loading branch information
dpanta94 committed Jan 9, 2025
1 parent 2590790 commit eefa881
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions src/Tickets/Commerce/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class Order extends Abstract_Order {
*
* @var string
*/
protected const ORDER_LOCK_META = '_tec_tc_order_lock';
protected const ORDER_LOCK_KEY = 'post_content_filtered';

/**
* Keeping track of the lock id generated during a request.
Expand Down Expand Up @@ -380,7 +380,7 @@ public function modify_status( $order_id, $status_slug, array $extra_args = [] )

$updated = tec_tc_orders()
->where( 'id', $order_id )
->where( 'post_content_filtered', $this->get_lock_id() )
->where( self::ORDER_LOCK_KEY, $this->get_lock_id() )
->set_args( $args )->save();

$this->unlock_order( $order_id );
Expand Down Expand Up @@ -651,7 +651,7 @@ public function upsert( Gateway_Interface $gateway, array $args ) {

$updated = tec_tc_orders()
->where( 'id', $existing_order_id )
->where( 'post_content_filtered', $this->get_lock_id() )
->where( self::ORDER_LOCK_KEY, $this->get_lock_id() )
->set_args( $update_args )
->save();

Expand Down Expand Up @@ -938,9 +938,11 @@ public function lock_order( int $order_id ): bool {

DB::beginTransaction();

$lock_key = self::ORDER_LOCK_KEY;

DB::query(
DB::prepare(
"UPDATE %i set post_content_filtered = %s where ID = $order_id and post_content_filtered = ''",
"UPDATE %i set $lock_key = %s where ID = $order_id and $lock_key = ''",
DB::prefix( 'posts' ),
$this->get_lock_id()
)
Expand All @@ -959,9 +961,10 @@ public function lock_order( int $order_id ): bool {
* @return bool Whether the order was unlocked.
*/
public function unlock_order( int $order_id ): bool {
$lock_key = self::ORDER_LOCK_KEY;
return (bool) DB::query(
DB::prepare(
"UPDATE %i set post_content_filtered = '' where ID = $order_id and post_content_filtered = %s",
"UPDATE %i set $lock_key = '' where ID = $order_id and $lock_key = %s",
DB::prefix( 'posts' ),
$this->get_lock_id()
)
Expand Down Expand Up @@ -1002,9 +1005,11 @@ protected function generate_lock_id(): string {
* @return bool Whether the order is locked.
*/
public function is_order_locked( int $order_id ): bool {
$lock_key = self::ORDER_LOCK_KEY;

return (bool) DB::get_var(
DB::prepare(
"SELECT post_content_filtered FROM %i WHERE ID = $order_id",
"SELECT $lock_key FROM %i WHERE ID = $order_id",
DB::prefix( 'posts' )
)
);
Expand Down

0 comments on commit eefa881

Please sign in to comment.