diff --git a/src/Brownie/ESputnik/ESputnik.php b/src/Brownie/ESputnik/ESputnik.php index 58ff41a..52117b8 100644 --- a/src/Brownie/ESputnik/ESputnik.php +++ b/src/Brownie/ESputnik/ESputnik.php @@ -10,6 +10,7 @@ use Brownie\ESputnik\HTTPClient\HTTPClient; use Brownie\ESputnik\Model\Address; use Brownie\ESputnik\Model\OrdersInfo; +use Brownie\ESputnik\Model\RecipientList; use Brownie\ESputnik\Model\Version; use Brownie\ESputnik\Model\Subscribe; use Brownie\ESputnik\Model\Contact; @@ -391,6 +392,39 @@ public function updateContacts( return false; } + /** + * SmartSend message. + * + * @param int $message_id ID message for send + * @param RecipientList $recipientList List users and params for send + * + * @return bool + */ + public function sendSmartMessage( + int $message_id, + RecipientList $recipientList + ) { + $data = [ + 'recipients' => $recipientList->toArray(), + ]; + + $response = $this + ->getHttpClient() + ->request( + HTTPClient::HTTP_CODE_200, + "message/{$message_id}/smartsend", + $data, + HTTPClient::HTTP_METHOD_POST + + ); + + if (isset($response['response']['results']) && !empty($response['response']['results'])) { + return $response['response']['results']; + } + + return false; + } + /** * Creates a contact model for arguments. * diff --git a/src/Brownie/ESputnik/HTTPClient/HTTPClient.php b/src/Brownie/ESputnik/HTTPClient/HTTPClient.php index 9a271d5..b814787 100644 --- a/src/Brownie/ESputnik/HTTPClient/HTTPClient.php +++ b/src/Brownie/ESputnik/HTTPClient/HTTPClient.php @@ -147,7 +147,7 @@ public function request( * Checking HTTP Code. */ if ($checkHTTPCode != $httpCode) { - throw new InvalidCodeException($httpCode); + throw new InvalidCodeException($responseBody); } if ($ignoreEmptyResponse && empty($response)) { diff --git a/src/Brownie/ESputnik/Model/Contact.php b/src/Brownie/ESputnik/Model/Contact.php index 2e06029..3c4c021 100644 --- a/src/Brownie/ESputnik/Model/Contact.php +++ b/src/Brownie/ESputnik/Model/Contact.php @@ -75,6 +75,10 @@ public function toArray() { $data = []; + if ($this->getId()) { + $data['id'] = $this->getId(); + } + if ($this->getFirstName()) { $data['firstName'] = $this->getFirstName(); } @@ -83,11 +87,13 @@ public function toArray() $data['lastName'] = $this->getLastName(); } - $data[$this->getChannelList()->getKeyName()] = array_map( - function (Channel $channel) { - return $channel->toArray(); - }, $this->getChannelList()->toArray() - ); + if ($this->getChannelList()) { + $data[$this->getChannelList()->getKeyName()] = array_map( + function (Channel $channel) { + return $channel->toArray(); + }, $this->getChannelList()->toArray() + ); + } if ($this->getAddress()) { $data['address'] = $this->getAddress()->toArray(); diff --git a/src/Brownie/ESputnik/Model/ContactFieldsUpdate.php b/src/Brownie/ESputnik/Model/ContactFieldsUpdate.php index f63670b..d7456e2 100644 --- a/src/Brownie/ESputnik/Model/ContactFieldsUpdate.php +++ b/src/Brownie/ESputnik/Model/ContactFieldsUpdate.php @@ -41,6 +41,9 @@ class ContactFieldsUpdate extends ArrayList const POSTCODE = 'postcode'; + const MOBILEPUSH = 'mobilepush'; + + const WEBPUSH = 'webpush'; protected $fields = [ 'fieldNames' => [self::FIRST_NAME, self::LAST_NAME], 'customFieldIDS' => null, diff --git a/src/Brownie/ESputnik/Model/MobilePushChannel.php b/src/Brownie/ESputnik/Model/MobilePushChannel.php new file mode 100644 index 0000000..9d3ac7d --- /dev/null +++ b/src/Brownie/ESputnik/Model/MobilePushChannel.php @@ -0,0 +1,19 @@ + + * @license http://www.gnu.org/copyleft/lesser.html + */ + +namespace Brownie\ESputnik\Model; + +use Brownie\ESputnik\Model\Base\Channel; + +/** + * Media channel "mobilepush". + */ +class MobilePushChannel extends Channel +{ + + protected $type = 'mobilepush'; +} diff --git a/src/Brownie/ESputnik/Model/Recipient.php b/src/Brownie/ESputnik/Model/Recipient.php new file mode 100644 index 0000000..04a87c4 --- /dev/null +++ b/src/Brownie/ESputnik/Model/Recipient.php @@ -0,0 +1,36 @@ + + * @license http://www.gnu.org/copyleft/lesser.html + */ + +namespace Brownie\ESputnik\Model; + +use Brownie\ESputnik\Model\Base\ArrayList; + +/** + * Recipient. + * + * @method string setEmail($email) Set email client. + * @method int setContactId($id) Set id. + * @method string setLocator($email) Set email client. + * @method string setJsonParam($params) Set json params. + * @method bool setExternalRequestId($external_id) Set external id. + * @method string getEmail($email) Get email client. + * @method int getContactId() Get id. + * @method string getLocator($email) Get email client. + * @method string getJsonParam() Get json params. + * @method string getExternalRequestId() Get external id. + */ +class Recipient extends ArrayList +{ + + protected $fields = [ + 'email' => null, + 'contactId' => null, + 'locator' => null, + 'jsonParam' => null, + 'externalRequestId' => null, + ]; +} diff --git a/src/Brownie/ESputnik/Model/RecipientList.php b/src/Brownie/ESputnik/Model/RecipientList.php new file mode 100644 index 0000000..9a513bc --- /dev/null +++ b/src/Brownie/ESputnik/Model/RecipientList.php @@ -0,0 +1,47 @@ + + * @license http://www.gnu.org/copyleft/lesser.html + */ + +namespace Brownie\ESputnik\Model; + +use Brownie\ESputnik\Exception\ValidateException; +use Brownie\ESputnik\Model\Base\ArrayList; +use Brownie\ESputnik\Model\Base\Channel; +use Brownie\ESputnik\Model\Base\EntityList; + +/** + * @method Contact setId($id) Set id. + * @method Contact setRecipients($recepients) Set recepients. + * @method int getId() Get id. + * @method Recepients getRecipients() Get recepients. + */ +class RecipientList extends EntityList +{ + protected $keyName = 'recipients'; + + /** + * Add field to list. + * Returns the current object. + * + * @param Field $field Field contact. + * + * @return self + */ + public function add(Recipient $field) + { + parent::append($field); + return $this; + } + + public function toArray() + { + return array_map( + function (Recipient $field) { + return $field->toArray(); + }, parent::toArray() + ); + } +} diff --git a/src/Brownie/ESputnik/Model/WebPushChannel.php b/src/Brownie/ESputnik/Model/WebPushChannel.php new file mode 100644 index 0000000..63a799b --- /dev/null +++ b/src/Brownie/ESputnik/Model/WebPushChannel.php @@ -0,0 +1,19 @@ + + * @license http://www.gnu.org/copyleft/lesser.html + */ + +namespace Brownie\ESputnik\Model; + +use Brownie\ESputnik\Model\Base\Channel; + +/** + * Media channel "webpush". + */ +class WebPushChannel extends Channel +{ + + protected $type = 'webpush'; +}