-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
New features #143
Closed
Closed
New features #143
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,207 @@ | ||
<?php | ||
/** | ||
* Omnisend Client | ||
* | ||
* @package OmnisendClient | ||
*/ | ||
|
||
namespace Omnisend\SDK\V1; | ||
|
||
use Omnisend\Internal\Utils; | ||
use WP_Error; | ||
|
||
defined( 'ABSPATH' ) || die( 'no direct access' ); | ||
|
||
/** | ||
* Omnisend Batch class. It should be used with Omnisend Client. | ||
* | ||
*/ | ||
class Batch { | ||
public const PRODUCTS_ENDPOINT = 'products'; | ||
public const CATEGORIES_ENDPOINT = 'categories'; | ||
public const EVENTS_ENDPOINT = 'events'; | ||
public const CONTACTS_ENDPOINT = 'contacts'; | ||
public const POST_METHOD = 'POST'; | ||
public const PUT_METHOD = 'PUT'; | ||
|
||
private const REQUIRED_PROPERTIES = array( | ||
'endpoint', | ||
'items', | ||
'method', | ||
); | ||
private const STRING_PROPERTIES = array( | ||
'endpoint', | ||
'method', | ||
'origin', | ||
); | ||
private const ARRAY_PROPERTIES = array( | ||
'items', | ||
); | ||
private const AVAILABLE_ENDPOINTS = array( | ||
self::PRODUCTS_ENDPOINT, | ||
self::CATEGORIES_ENDPOINT, | ||
self::EVENTS_ENDPOINT, | ||
self::CONTACTS_ENDPOINT, | ||
); | ||
private const AVAILABLE_METHODS = array( | ||
self::POST_METHOD, | ||
self::PUT_METHOD, | ||
); | ||
|
||
/** | ||
* @var string $endpoint | ||
*/ | ||
private $endpoint = null; | ||
|
||
/** | ||
* @var array $items | ||
*/ | ||
private $items = null; | ||
|
||
/** | ||
* @var string $method | ||
*/ | ||
private $method = null; | ||
|
||
/** | ||
* @var string $origin | ||
*/ | ||
private $origin = null; | ||
|
||
/** | ||
* Validate batch properties. | ||
* | ||
* It ensures that required properties are set and that they are valid. | ||
* | ||
* @return WP_Error | ||
*/ | ||
public function validate(): WP_Error { | ||
$error = new WP_Error(); | ||
$error = $this->validate_properties( $error ); | ||
|
||
if ( $error->has_errors() ) { | ||
return $error; | ||
} | ||
|
||
$error = $this->validate_values( $error ); | ||
|
||
return $error; | ||
} | ||
|
||
/** | ||
* Convert batch to array | ||
* | ||
* If batch is valid it will be transformed to array that can be sent to Omnisend. | ||
* | ||
* @return array | ||
*/ | ||
public function to_array(): array { | ||
if ( $this->validate()->has_errors() ) { | ||
return array(); | ||
} | ||
|
||
$arr = array( | ||
'endpoint' => $this->endpoint, | ||
'items' => $this->items, | ||
'method' => $this->method, | ||
); | ||
|
||
if ( ! empty( $this->origin ) ) { | ||
$arr['origin'] = $this->origin; | ||
} | ||
|
||
return $arr; | ||
} | ||
|
||
/** | ||
* Sets endpoint | ||
* | ||
* @param string $endpoint | ||
* | ||
* @return void | ||
*/ | ||
public function set_endpoint( $endpoint ): void { | ||
$this->endpoint = $endpoint; | ||
} | ||
|
||
/** | ||
* Sets batch items | ||
* | ||
* @param array $items | ||
* | ||
* @return void | ||
*/ | ||
public function set_items( $items ): void { | ||
$this->items = $items; | ||
} | ||
|
||
/** | ||
* Sets method, it can be "PUT" or "POST" | ||
* | ||
* @param string $method | ||
* | ||
* @return void | ||
*/ | ||
public function set_method( $method ): void { | ||
$this->method = $method; | ||
} | ||
|
||
/** | ||
* Sets origin of request | ||
* | ||
* @param string $origin | ||
* | ||
* @return void | ||
*/ | ||
public function set_origin( $origin ): void { | ||
$this->origin = $origin; | ||
} | ||
|
||
/** | ||
* Validates property type | ||
* | ||
* @param WP_Error $error | ||
* | ||
* @return WP_Error $error | ||
*/ | ||
private function validate_properties( WP_Error $error ): WP_Error { | ||
foreach ( $this as $property_key => $property_value ) { | ||
if ( in_array( $property_key, self::REQUIRED_PROPERTIES ) && empty( $property_value ) ) { | ||
$error->add( $property, $property_key . ' is a required property.' ); | ||
} | ||
|
||
if ( $property_value !== null && in_array( $property_key, self::STRING_PROPERTIES ) && ! is_string( $property_value ) ) { | ||
$error->add( $property, $property_key . ' must be a string.' ); | ||
} | ||
|
||
if ( $property_value !== null && in_array( $property_key, self::ARRAY_PROPERTIES ) && ! is_array( $property_value ) ) { | ||
$error->add( $property, $property_key . ' must be an array.' ); | ||
} | ||
} | ||
|
||
return $error; | ||
} | ||
|
||
/** | ||
* Validates property value | ||
* | ||
* @param WP_Error $error | ||
* | ||
* @return WP_Error $error | ||
*/ | ||
private function validate_values( WP_Error $error ): WP_Error { | ||
if ( ! in_array( $this->endpoint, self::AVAILABLE_ENDPOINTS ) ) { | ||
$error->add( 'endpoint', sprintf( 'Endpoint must be one of the following: %s', implode( ', ', self::AVAILABLE_ENDPOINTS ) ) ); | ||
} | ||
|
||
if ( ! in_array( $this->method, self::AVAILABLE_METHODS ) ) { | ||
$error->add( 'method', sprintf( 'Method must be on of the following: %s', implode( ', ', self::AVAILABLE_METHODS ) ) ); | ||
} | ||
|
||
if ( empty( $this->items ) || count( $this->items ) > 1000 ) { | ||
$error->add( 'items', sprintf( 'Items are empty or batch size limit: %s was exceeded', 1000 ) ); | ||
} | ||
|
||
return $error; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How are you planing to validate items? Because they can be product, category etc and as I understand batch methods will be used to create all of these items.
How would you create a batch of products and send them via this method? Would to_array generate correct request?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Batch should be used with Product/Event/Category class - they return single item and user only needs to save this item into array and set it with set_items e.g.
$data[] = $product->to_array();
$data[] = $category->to_array();
$batch->set_items($data);
or I could implement previously suggested "save/reset", and handle "$data[] = $product->to_array()" for end user. I only need to know when user starts assigning another product values within loop.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How new developer who first time see our SDK will know following things:
Here is my suggestion:
With this changes usage is very simple ("to_array" and "validate" used automatically + types defined):
Also please: