From ad14f9956f0b798723c78533aa5e4849eb0d9b8b Mon Sep 17 00:00:00 2001 From: lrobert Date: Tue, 14 Jan 2014 11:08:31 -0500 Subject: [PATCH 01/11] Added composer suppport --- autoload.php.dist | 4 +++- composer.json | 31 +++++++++++++++++++++++++++++++ tests/bootstrap.php | 4 +++- 3 files changed, 37 insertions(+), 2 deletions(-) create mode 100644 composer.json diff --git a/autoload.php.dist b/autoload.php.dist index 293f4e4..15bf2e7 100644 --- a/autoload.php.dist +++ b/autoload.php.dist @@ -1,5 +1,7 @@ =5.3.3", + "symfony/class-loader": "2.4.*", + "symfony/console" : "2.4.*" + }, + "require-dev" : { + "phpunit/phpunit": "3.7.*" + }, + "autoload" : { + "psr-0": { + "Bronto_" : "src/", + "Bronto_Tests_": "tests/" + } + }, + "target-dir" : "Symfony/Component/Console", + "minimum-stability": "dist" +} \ No newline at end of file diff --git a/tests/bootstrap.php b/tests/bootstrap.php index fd2535e..a732331 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -3,7 +3,9 @@ * @copyright 2011-2013 Bronto Software, Inc. * @license http://opensource.org/licenses/OSL-3.0 Open Software License v. 3.0 (OSL-3.0) */ -if (file_exists($file = __DIR__.'/../autoload.php')) { +if (file_exists($file = __DIR__.'/../vendor/autoload.php')) { + require_once $file; +} elseif (file_exists($file = __DIR__.'/../autoload.php')) { require_once $file; } elseif (file_exists($file = __DIR__.'/../autoload.php.dist')) { require_once $file; From 2a22c6af531b5a249c43d7d6bceb0e2d61fae36d Mon Sep 17 00:00:00 2001 From: lrobert Date: Fri, 31 Jan 2014 11:55:56 -0500 Subject: [PATCH 02/11] Updated composer.json to have a correct minimum-stability --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 317e022..3d46c7c 100644 --- a/composer.json +++ b/composer.json @@ -27,5 +27,5 @@ } }, "target-dir" : "Symfony/Component/Console", - "minimum-stability": "dist" + "minimum-stability": "stable" } \ No newline at end of file From 4827094cecdc0f2ccdbb34f91350ba2547c62c55 Mon Sep 17 00:00:00 2001 From: Philip Cali Date: Wed, 5 Feb 2014 15:22:14 +0000 Subject: [PATCH 03/11] Adding observer for custom error handling and reusing sessions. --- src/Bronto/Api.php | 67 ++++++++++++++++++++++++++++++++++++--- src/Bronto/Api/Object.php | 2 +- src/Bronto/Observer.php | 35 ++++++++++++++++++++ 3 files changed, 98 insertions(+), 6 deletions(-) create mode 100644 src/Bronto/Observer.php diff --git a/src/Bronto/Api.php b/src/Bronto/Api.php index a777475..813411e 100644 --- a/src/Bronto/Api.php +++ b/src/Bronto/Api.php @@ -37,6 +37,7 @@ class Bronto_Api 'type' => null, 'path' => null, ), + 'observer' => false, // SoapClient 'soap_version' => SOAP_1_1, 'compression' => true, @@ -76,6 +77,11 @@ class Bronto_Api */ protected $_uuid; + /** + * @var Bronto_Observer + */ + protected $_observer; + /** * @param string $token * @param array $options @@ -116,11 +122,20 @@ public function login() // Get a new SoapClient $this->reset(); $client = $this->getSoapClient(false); - $sessionId = $client->login(array('apiToken' => $token))->return; - $client->__setSoapHeaders(array( - new SoapHeader(self::BASE_URL, 'sessionHeader', array('sessionId' => $sessionId)) - )); - $this->_authenticated = true; + // Allow observer to inject a session before login + if ($this->getObserver()) { + $this->getObserver()->onBeforeLogin($this); + } + // Check for auth changes + if (!$this->isAuthenticated()) { + $sessionId = $client->login(array('apiToken' => $token))->return; + $this->setSessionId($sessionId); + + // Allow observer to store session + if ($this->getObserver()) { + $this->getObserver()->onAfterLogin($this, $sessionId); + } + } } catch (Exception $e) { $this->throwException($e); } @@ -128,6 +143,22 @@ public function login() return $this; } + /** + * Resuse an existing session, if possible + * + * @param string $sessionId + * @return Bronto_Api + */ + public function setSessionId($sessionId) + { + $client = $this->getSoapClient(false); + $client->__setSoapHeaders(array( + new SoapHeader(self::BASE_URL, 'sessionHeader', array('sessionId' => $sessionId)) + )); + $this->_authenticated = true; + return $this; + } + /** * We want all Exceptions to be Bronto_Api_Exception for request/response * @@ -162,6 +193,11 @@ public function throwException($exception, $message = null, $code = null) $exception->setResponse($this->getLastResponse()); } + // Allow observer to handle exception cases + if ($this->getObserver()) { + $this->getObserver()->onError($this, $exception); + } + throw $exception; } @@ -516,6 +552,27 @@ public function getRetryer(array $options = array()) return $this->_retryer; } + /** + * Gets the observer for the API client + * + * @return Bronto_Observer + */ + public function getObserver() + { + if (!$this->_observer) { + if (isset($this->_options['observer'])) { + $observer = $this->_options['observer']; + if (is_string($observer) && class_exists($observer)) { + $observer = new $observer(); + } + if ($observer instanceOf Bronto_Observer) { + $this->_observer = $observer; + } + } + } + return $this->_observer; + } + /** * @return Bronto_Util_Retryer_RetryerInterface */ diff --git a/src/Bronto/Api/Object.php b/src/Bronto/Api/Object.php index 7f505db..615beaa 100644 --- a/src/Bronto/Api/Object.php +++ b/src/Bronto/Api/Object.php @@ -350,7 +350,7 @@ public function doRequest($method, array $data, $canUseRetryer = false) return $this->getApi()->throwException($exception); } else { // Attempt to get a new session token - sleep(5); + // sleep(5); $this->getApi()->login(); // If using readDirection, we have to start over if (isset($data['filter']['readDirection'])) { diff --git a/src/Bronto/Observer.php b/src/Bronto/Observer.php new file mode 100644 index 0000000..6e6dcde --- /dev/null +++ b/src/Bronto/Observer.php @@ -0,0 +1,35 @@ + + * @copyright 2011-2014 Bronto Software, Inc. + * @license http://opensource.org/licenses/OSL-3.0 Open Software License v. 3.0 (OSL-3.0) + */ +interface Bronto_Observer +{ + /** + * Observe when the Bronto_Api is about to perform the login + * + * @param Bronto_Api $api + * @return void + */ + public function onBeforeLogin($api); + + /** + * Observe when the Bronto_Api client makes a login call + * + * @param Bronto_Api $api + * @param string $sessionId + * @return void + */ + public function onAfterLogin($api, $sessionId); + + /** + * Observe when the Bronto_Api client throws an exception + * + * @param Bronto_Api $api + * @param string $sessionId + * @return void + */ + public function onError($api, $exception); +} From 5ea2d1e8e8b4078734ed2457b9c2d8c246e23227 Mon Sep 17 00:00:00 2001 From: Philip Cali Date: Thu, 6 Feb 2014 18:01:46 +0000 Subject: [PATCH 04/11] Ability to add a custom Retryer. --- src/Bronto/Api.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/Bronto/Api.php b/src/Bronto/Api.php index 813411e..0ad4c7f 100644 --- a/src/Bronto/Api.php +++ b/src/Bronto/Api.php @@ -540,6 +540,13 @@ public function getRetryer(array $options = array()) if (!($this->_retryer instanceOf Bronto_Util_Retryer_RetryerInterface)) { $options = array_merge($this->_options['retryer'], $options); switch ($options['type']) { + case 'custom': + if ($options['object']) { + $this->_retryer = $options['object']; + } else { + $this->_retryer = new $options['path']; + } + break; case 'file': $this->_retryer = new Bronto_Util_Retryer_FileRetryer($options); break; From 3c5ac2d52f4854d6b47e94c990f5a52194a468a6 Mon Sep 17 00:00:00 2001 From: Philip Cali Date: Fri, 7 Feb 2014 19:58:00 +0000 Subject: [PATCH 05/11] Older versions of php were throwing warnings. --- src/Bronto/SoapClient.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Bronto/SoapClient.php b/src/Bronto/SoapClient.php index ac744de..419b0db 100644 --- a/src/Bronto/SoapClient.php +++ b/src/Bronto/SoapClient.php @@ -14,12 +14,12 @@ class Bronto_SoapClient extends SoapClient */ public function __doRequest($request, $location, $action, $version, $one_way = 0) { $result = parent::__doRequest($request, $location, $action, $version); - + // Only replace unicode characters if PCRE version is less than 8.30 - if (version_compare(strstr(constant('PCRE_VERSION'), ' ', true), '8.30', '<')) { + $version = reset(explode(' ', constant('PCRE_VERSION'))); + if (version_compare($version, '8.30', '<')) { $result = preg_replace('/[\x{0}-\x{8}\x{B}-\x{C}\x{E}-\x{1F}\x{D800}-\x{DFFF}]/u', '', $result); } - return $result; } } From adf1c5c5be26eb043d496a0135ddc8a9198f37f2 Mon Sep 17 00:00:00 2001 From: Philip Cali Date: Tue, 11 Feb 2014 20:06:01 +0000 Subject: [PATCH 06/11] PHP Strict mode was complaining about references. --- src/Bronto/SoapClient.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Bronto/SoapClient.php b/src/Bronto/SoapClient.php index 419b0db..2dc7449 100644 --- a/src/Bronto/SoapClient.php +++ b/src/Bronto/SoapClient.php @@ -16,7 +16,8 @@ public function __doRequest($request, $location, $action, $version, $one_way = 0 $result = parent::__doRequest($request, $location, $action, $version); // Only replace unicode characters if PCRE version is less than 8.30 - $version = reset(explode(' ', constant('PCRE_VERSION'))); + $parts = explode(' ', constant('PCRE_VERSION')); + $version = reset($parts); if (version_compare($version, '8.30', '<')) { $result = preg_replace('/[\x{0}-\x{8}\x{B}-\x{C}\x{E}-\x{1F}\x{D800}-\x{DFFF}]/u', '', $result); } From 8ab6a80b63f20bb84737fbdac94c32186058b314 Mon Sep 17 00:00:00 2001 From: Philip Cali Date: Tue, 18 Feb 2014 21:54:17 -0500 Subject: [PATCH 07/11] Update composer.json --- composer.json | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/composer.json b/composer.json index 3d46c7c..5e4435e 100644 --- a/composer.json +++ b/composer.json @@ -26,6 +26,5 @@ "Bronto_Tests_": "tests/" } }, - "target-dir" : "Symfony/Component/Console", "minimum-stability": "stable" -} \ No newline at end of file +} From 78336ccd485371c27bd2fed8d46316b05f17c6ad Mon Sep 17 00:00:00 2001 From: Philip Cali Date: Tue, 18 Feb 2014 22:44:06 -0500 Subject: [PATCH 08/11] Update composer.json Removing Symfony, as it's not required to run the API client. --- composer.json | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/composer.json b/composer.json index 5e4435e..a28f296 100644 --- a/composer.json +++ b/composer.json @@ -13,9 +13,7 @@ } ], "require" : { - "php" : ">=5.3.3", - "symfony/class-loader": "2.4.*", - "symfony/console" : "2.4.*" + "php" : ">=5.3.3" }, "require-dev" : { "phpunit/phpunit": "3.7.*" From 16a4e5f357f65adf2faf976b15087141bca7408e Mon Sep 17 00:00:00 2001 From: Gassan Idriss Date: Fri, 4 Apr 2014 13:38:21 -0700 Subject: [PATCH 09/11] Fix addProduct method Fix error cant modify overloaded property when adding a product with addProduct method --- src/Bronto/Api/Order/Row.php | 55 ++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/src/Bronto/Api/Order/Row.php b/src/Bronto/Api/Order/Row.php index 22c303b..11fcfba 100644 --- a/src/Bronto/Api/Order/Row.php +++ b/src/Bronto/Api/Order/Row.php @@ -22,6 +22,61 @@ */ class Bronto_Api_Order_Row extends Bronto_Api_Row { + /** + * @var string + */ + public $id; + + /** + * @var string + */ + public $contactId; + + /** + * @var string + */ + public $email; + + /** + * @var array + */ + public $products = array(); + + /** + * @var string + */ + public $orderDate; + + /** + * @var string + */ + public $deliveryId; + + /** + * @var string + */ + public $messageId; + + /** + * @var string + */ + public $automatorId; + + /** + * @var string + */ + public $listId; + + /** + * @var string + */ + public $segmentId; + + /** + * @var string + */ + public $deliveryType; + /** * @param bool $upsert Ignored * @param bool $refresh From 5e2c3a43015a80cfc557b2253f3e5f014a46c414 Mon Sep 17 00:00:00 2001 From: Gassan Idriss Date: Fri, 4 Apr 2014 14:14:35 -0700 Subject: [PATCH 10/11] Fix addProduct method --- src/Bronto/Api/Order/Row.php | 63 ++++-------------------------------- 1 file changed, 6 insertions(+), 57 deletions(-) diff --git a/src/Bronto/Api/Order/Row.php b/src/Bronto/Api/Order/Row.php index 11fcfba..dc3a291 100644 --- a/src/Bronto/Api/Order/Row.php +++ b/src/Bronto/Api/Order/Row.php @@ -22,60 +22,6 @@ */ class Bronto_Api_Order_Row extends Bronto_Api_Row { - /** - * @var string - */ - public $id; - - /** - * @var string - */ - public $contactId; - - /** - * @var string - */ - public $email; - - /** - * @var array - */ - public $products = array(); - - /** - * @var string - */ - public $orderDate; - - /** - * @var string - */ - public $deliveryId; - - /** - * @var string - */ - public $messageId; - - /** - * @var string - */ - public $automatorId; - - /** - * @var string - */ - public $listId; - - /** - * @var string - */ - public $segmentId; - - /** - * @var string - */ - public $deliveryType; /** * @param bool $upsert Ignored @@ -124,6 +70,8 @@ public function __set($columnName, $value) */ public function addProduct(array $data = array()) { + $products = $this->products; + $product = new Bronto_Api_Order_Product($data); $productId = $product->id; @@ -131,11 +79,12 @@ public function addProduct(array $data = array()) throw new Bronto_Api_Order_Exception('Product must have a value for ID.'); } - if (isset($this->products[$productId])) { + if (isset($products[$productId])) { throw new Bronto_Api_Order_Exception("Product already exists in Order with ID: {$productId}"); } - - $this->products[$productId] = $product; + + $products[$productId] = $product; + $this->products = $products; return $product; } } From c8d45784c4a77b626e2a9cd2b759f2b5644d9795 Mon Sep 17 00:00:00 2001 From: Gassan Idriss Date: Mon, 22 Dec 2014 14:26:17 -0800 Subject: [PATCH 11/11] per philcali's suggestion in pull request 8 comments --- src/Bronto/Api/Order/Row.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Bronto/Api/Order/Row.php b/src/Bronto/Api/Order/Row.php index dc3a291..8c05003 100644 --- a/src/Bronto/Api/Order/Row.php +++ b/src/Bronto/Api/Order/Row.php @@ -70,7 +70,7 @@ public function __set($columnName, $value) */ public function addProduct(array $data = array()) { - $products = $this->products; + $products = is_null($this->products) ? array() : $this->products; $product = new Bronto_Api_Order_Product($data); $productId = $product->id;