Skip to content

Commit

Permalink
Change client create_contact response type
Browse files Browse the repository at this point in the history
  • Loading branch information
nerijuszaniauskas committed Feb 12, 2024
1 parent a372e26 commit 0cbbe0f
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 9 deletions.
15 changes: 8 additions & 7 deletions omnisend/includes/Internal/V1/class-client.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace Omnisend\Internal\V1;

use Omnisend\Sdk\V1\Contact;
use Omnisend\Sdk\V1\CreateContactResponse;
use WP_Error;

defined( 'ABSPATH' ) || die( 'no direct access' );
Expand All @@ -30,7 +31,7 @@ public function __construct( string $api_key, string $plugin_name, string $plugi
}


public function create_contact( $contact ): mixed {
public function create_contact( $contact ): CreateContactResponse {
$error = new WP_Error();

if ( $contact instanceof Contact ) {
Expand All @@ -42,7 +43,7 @@ public function create_contact( $contact ): mixed {
$error->merge_from( $this->check_setup() );

if ( $error->has_errors() ) {
return $error;
return new CreateContactResponse( '', $error );
}

$response = wp_remote_post(
Expand All @@ -61,31 +62,31 @@ public function create_contact( $contact ): mixed {

if ( is_wp_error( $response ) ) {
error_log('wp_remote_post error: ' . $response->get_error_message()); // phpcs:ignore
return $response;
return new CreateContactResponse( '', $response );
}

$http_code = wp_remote_retrieve_response_code( $response );
if ( $http_code >= 400 ) {
$body = wp_remote_retrieve_body( $response );
$err_msg = "HTTP error: {$http_code} - " . wp_remote_retrieve_response_message( $response ) . " - {$body}";
$error->add( 'omnisend_api', $err_msg );
return $error;
return new CreateContactResponse( '', $error );
}

$body = wp_remote_retrieve_body( $response );
if ( ! $body ) {
$error->add( 'omnisend_api', 'empty response' );
return $error;
return new CreateContactResponse( '', $error );
}

$arr = json_decode( $body, true );

if ( empty( $arr['contactID'] ) ) {
$error->add( 'omnisend_api', 'contactID not found in response.' );
return $error;
return new CreateContactResponse( '', $error );
}

return (string) $arr['contactID'];
return new CreateContactResponse( (string) $arr['contactID'], $error );
}

/**
Expand Down
4 changes: 2 additions & 2 deletions omnisend/includes/Sdk/V1/class-client.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ interface Client {
*
* @param Contact $contact
*
* @return string|WP_Error Created/updated contact identifier (ID) or WP_Error
* @return CreateContactResponse
*/
public function create_contact( $contact ): mixed;
public function create_contact( $contact ): CreateContactResponse;
}
36 changes: 36 additions & 0 deletions omnisend/includes/Sdk/V1/class-createcontactresponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
/**
* Omnisend Client
*
* @package OmnisendClient
*/

namespace Omnisend\Sdk\V1;

use WP_Error;

defined( 'ABSPATH' ) || die( 'no direct access' );

class CreateContactResponse {

private string $contact_id;

private WP_Error $wp_error;

/**
* @param string $contact_id
* @param WP_Error $wp_error
*/
public function __construct( string $contact_id, WP_Error $wp_error ) {
$this->contact_id = $contact_id;
$this->wp_error = $wp_error;
}

public function get_contact_id(): string {
return $this->contact_id;
}

public function get_wp_error(): WP_Error {
return $this->wp_error;
}
}

0 comments on commit 0cbbe0f

Please sign in to comment.