Skip to content

Commit

Permalink
fix + cron
Browse files Browse the repository at this point in the history
  • Loading branch information
MouseZver committed Mar 9, 2021
1 parent 85bbc2d commit eebb311
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 22 deletions.
39 changes: 39 additions & 0 deletions example/cron.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

/*
WARNING !!!
This is code example logic
*/

$leeloo = new \Nouvu\Leeloo\Api( $config );

$leeloo -> setSqlCallback( [
'delete' => fn( int $id ) => $db -> query( 'DELETE FROM `leeloo_queue_cron` WHERE `id` = ' . $id ),
'update' => fn( int $id, string $response ) => $db -> prepare( 'UPDATE `leeloo_queue_cron` SET `again` = `again` + 1, response = ? WHERE `id` = ?', [ $response, $id ] ),
] );

$response = $lrm -> query( 'SELECT `id`, `method`, `data` FROM `leeloo_queue_cron` ORDER BY `id` ASC, `again` ASC LIMIT 30' );

while ( $queue = $response -> fetch( FETCH_ASSOC ) )
{
try
{
$leeloo -> cron( $queue );
}
catch ( \Nouvu\Leeloo\LeelooOrderFailed $e )
{
$logger -> set( $e -> getMessage() ) -> set( json_encode ( $leeloo -> getResponse() ) ) -> set( 'id row: ' . $queue['id'] );
}
}

/* CREATE TABLE `leeloo_queue_cron` (
`id` INT NOT NULL AUTO_INCREMENT,
`method` VARCHAR(20) NOT NULL,
`data` JSON NOT NULL,
`response` JSON NOT NULL,
`again` INT NOT NULL DEFAULT 1,
`created` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`))
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
COLLATE = utf8_bin; */
29 changes: 29 additions & 0 deletions example/failed_order_pending.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

$data = [
'email' => 'mail@mail.ru',
'phone' => 'phone',
'accountId' => $id_account,

'id' => &$id_insert_order
];

try
{
$leeloo_order_id = $leeloo -> orderPending( $data, $offer );

$db -> query( 'INSERT... order pay, status = pending AND leeloo_order_id = {$leeloo_order_id}' );

$id_insert_order = $db -> last_insert_id();
}
catch ( \Nouvu\Leeloo\LeelooOrderFailed $e )
{
$logger -> set( $e -> getMessage() ) -> set( json_encode ( $leeloo -> getResponse() ) );

$db -> query( 'INSERT... order pay, status = pending AND leeloo_order_id = null' );

$id_insert_order = $db -> last_insert_id();

$leeloo -> saveFailure();
}

24 changes: 24 additions & 0 deletions example/failed_order_update.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

$data = [
'status' => 'RESOLVED', // REJECTED
'price' => 10,
'currency' => 'USD',
'userComments' => 'Hello world',
];

try
{
$leeloo -> orderUpdate( $leeloo_order_id, $data );
/*
$leeloo -> orderCompleted( $leeloo_order_id, 10, 'USD', 'Hello world' );
$leeloo -> orderFailed( $leeloo_order_id, 10, 'USD', 'Hello world' );
*/
}
catch ( \Nouvu\Leeloo\LeelooOrderFailed $e )
{
$logger -> set( $e -> getMessage() ) -> set( json_encode ( $leeloo -> getResponse() ) );

$leeloo -> saveFailure();
}

41 changes: 28 additions & 13 deletions src/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ final class Api extends Core
'order'
];

private bool $cron = false;

private int $id = 0;

public function __construct ( array $leeloo, bool $send = true )
{
$this -> config = new Config( [
Expand All @@ -48,28 +52,28 @@ public function setSqlCallback( array $sql_callback ): void
$this -> set -> sqlCallback( $sql_callback );
}

public function send( string $link, array $data, string $request = 'POST', bool $cron = false, int $id = 0 ): bool
public function send( string $link, array $data, string $request = 'POST' ): bool
{
$this -> response = ( $this -> getData( 'send' ) ? $this -> stream( $link, $data, $request ) : [ 'send' => 'setSqlCallback' ] );

if ( ! empty ( $this -> response['status'] ) )
{
if ( $cron )
if ( $this -> cron )
{
$this -> getData( 'sql_callback.delete' )( $id );
$this -> getData( 'sql_callback.delete' )( $this -> id );
}

return true;
}

if ( $cron )
if ( $this -> cron )
{
$this -> getData( 'sql_callback.update' )( $id );
$this -> getData( 'sql_callback.update' )( $this -> id, json_encode ( $this -> getResponse() ) );

return false;
}

$this -> save();
$this -> saveFailure();

return false;
}
Expand Down Expand Up @@ -121,7 +125,7 @@ public function sendMessage( string $account_id, string $message ): void
] );
}

public function get_response(): array
public function getResponse(): array
{
return $this -> response;
}
Expand Down Expand Up @@ -195,22 +199,33 @@ public function orderFailed( string $leeloo_order_id, /* int | float */ $price,
] );
}

public function save(): void
public function saveFailure(): void
{
[ 'method' => $method, 'args' => $args ] = $this -> getVars();

$this -> getData( 'sql_callback.insert' )( $method, $args );
$this -> getData( 'sql_callback.insert' )( $method, json_encode ( $args ), json_encode ( $this -> getResponse() ) );
}

public function cron( array $data ): void
public function cron( array $queue ): void
{
/* $data ->
$this -> cron = true;

$this -> verify( [ 'id', 'method', 'args' ] );
$this -> id = $queue['id'];

try
{
$this -> {$queue['method']}( ...$queue['data'] );

if ( in_array ( $queue['method'], [ 'orderPending', 'orderUpdate' ] ) )
{
$this -> getData( 'sql_callback.delete' )( $this -> id );
}
}
catch ( LeelooOrderFailed $e )
{
$this -> getData( 'sql_callback.update' )( $this -> id, json_encode ( $this -> getResponse() ) );

} */
throw $e;
}
}
}
11 changes: 2 additions & 9 deletions src/Core.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Core

protected Setting $set;

protected function stream( string $link, array $data, string $request ): ?array
protected function stream( string $link, array $data, string $request ): array
{
$curl = curl_init ();

Expand All @@ -34,14 +34,7 @@ protected function stream( string $link, array $data, string $request ): ?array

curl_close ( $curl );

$s = json_decode ( $response, true );

if ( is_null ( $s ) )
{
var_dump ( $response );exit;
}

return $s;
return json_decode ( $response, true ) ?? [ 'response' => $response ];
}

protected function setData( string $name, array $keys, array $data ): void
Expand Down

0 comments on commit eebb311

Please sign in to comment.