From 0cbbe0f4d530672ce9989be7c334f05ca848cdcb Mon Sep 17 00:00:00 2001 From: Nerijus Zaniauskas Date: Mon, 12 Feb 2024 17:47:11 +0200 Subject: [PATCH] Change client create_contact response type --- .../includes/Internal/V1/class-client.php | 15 ++++---- omnisend/includes/Sdk/V1/class-client.php | 4 +-- .../Sdk/V1/class-createcontactresponse.php | 36 +++++++++++++++++++ 3 files changed, 46 insertions(+), 9 deletions(-) create mode 100644 omnisend/includes/Sdk/V1/class-createcontactresponse.php diff --git a/omnisend/includes/Internal/V1/class-client.php b/omnisend/includes/Internal/V1/class-client.php index 74c7591..e5350a6 100644 --- a/omnisend/includes/Internal/V1/class-client.php +++ b/omnisend/includes/Internal/V1/class-client.php @@ -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' ); @@ -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 ) { @@ -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( @@ -61,7 +62,7 @@ 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 ); @@ -69,23 +70,23 @@ public function create_contact( $contact ): mixed { $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 ); } /** diff --git a/omnisend/includes/Sdk/V1/class-client.php b/omnisend/includes/Sdk/V1/class-client.php index c13f7bb..28faf00 100644 --- a/omnisend/includes/Sdk/V1/class-client.php +++ b/omnisend/includes/Sdk/V1/class-client.php @@ -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; } diff --git a/omnisend/includes/Sdk/V1/class-createcontactresponse.php b/omnisend/includes/Sdk/V1/class-createcontactresponse.php new file mode 100644 index 0000000..026e3ba --- /dev/null +++ b/omnisend/includes/Sdk/V1/class-createcontactresponse.php @@ -0,0 +1,36 @@ +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; + } +}