diff --git a/src/PureClarity/Api/Feed/Feed.php b/src/PureClarity/Api/Feed/Feed.php index a06c1bc..6377f0e 100755 --- a/src/PureClarity/Api/Feed/Feed.php +++ b/src/PureClarity/Api/Feed/Feed.php @@ -48,8 +48,11 @@ abstract class Feed /** @var string[] $nonEmptyFields - Fields that must contain data */ protected $nonEmptyFields = []; - /** @var string $pageData - Feed Data */ - protected $pageData = ''; + /** @var string[] $pageData - Feed Data buffer */ + protected $pageData = []; + + /** @var string $itemSeparator - Separator between items in a page */ + protected $itemSeparator = ','; /** @var integer $pageSize - Feed Handler class */ protected $pageSize = 50; @@ -100,16 +103,17 @@ public function start() public function append($data) { $errors = $this->validate($data); - if (empty($errors) === false) { + if (!empty($errors)) { throw new Exception(implode('|', $errors)); } $this->dataIndex++; - $this->pageData .= $this->processData($data); + $this->pageData[] = $this->processData($data); if (($this->dataIndex % $this->pageSize) === 0) { - $this->transfer->append($this->pageData); - $this->pageData = ''; + $separator = ($this->dataIndex > $this->pageSize) ? $this->itemSeparator : ''; + $this->transfer->append($separator . implode($this->itemSeparator, $this->pageData)); + $this->pageData = []; } } @@ -122,11 +126,6 @@ public function append($data) protected function processData($data) { $data['_index'] = $this->dataIndex; - - if ($this->dataIndex >= 2) { - $this->pageData .= ','; - } - return json_encode($data); } @@ -161,9 +160,10 @@ protected function validate($data) */ public function end() { - if ($this->pageData !== '') { - $this->transfer->append($this->pageData); - $this->pageData = ''; + if (!empty($this->pageData)) { + $separator = ($this->dataIndex > $this->pageSize) ? $this->itemSeparator : ''; + $this->transfer->append($separator . implode($this->itemSeparator, $this->pageData)); + $this->pageData = []; } $this->transfer->close($this->feedEnd); diff --git a/src/PureClarity/Api/Feed/Transfer.php b/src/PureClarity/Api/Feed/Transfer.php index a583c45..b9db396 100755 --- a/src/PureClarity/Api/Feed/Transfer.php +++ b/src/PureClarity/Api/Feed/Transfer.php @@ -32,6 +32,9 @@ class Transfer /** @var string $feedId */ private $feedId; + /** @var string $endpointUrl - Cached SFTP endpoint URL */ + private $endpointUrl; + /** * @param string $feedType - Feed Type, used in naming of the feed file * @param string $accessKey - Application Access Key @@ -133,7 +136,13 @@ private function buildRequest($data) private function send($endPoint, $data) { $request = $this->buildRequest($data); - $url = $this->getSftpEndpoint($this->region) . $endPoint; + + // Cache the endpoint URL on first use + if (!$this->endpointUrl) { + $this->endpointUrl = $this->getSftpEndpoint($this->region); + } + + $url = $this->endpointUrl . $endPoint; $request = http_build_query($request); $curl = new Curl(); @@ -152,7 +161,7 @@ private function send($endPoint, $data) ); } - if (empty($error) === false) { + if (!empty($error)) { throw new Exception($error); } diff --git a/src/PureClarity/Api/Feed/Type/Order.php b/src/PureClarity/Api/Feed/Type/Order.php index eed9e7b..750d70f 100755 --- a/src/PureClarity/Api/Feed/Type/Order.php +++ b/src/PureClarity/Api/Feed/Type/Order.php @@ -18,6 +18,9 @@ class Order extends Feed /** @var string $feedType */ protected $feedType = self::FEED_TYPE_ORDER; + /** @var string $itemSeparator - No separator for CSV format */ + protected $itemSeparator = ''; + /** @var string[] $requiredFields - Fields that must be present in the data (regardless of content) */ protected $requiredFields = [ 'OrderID', @@ -53,19 +56,19 @@ class Order extends Feed */ public function processData($orderData) { - $data = ''; + $lines = []; foreach ($orderData as $orderLine) { - $data .= PHP_EOL . $orderLine['OrderID'] . ',' . - $orderLine['UserId'] . ',' . - $orderLine['Email'] . ',' . - $orderLine['DateTime'] . ',' . - $orderLine['ProdCode'] . ',' . - $orderLine['Quantity'] . ',' . - $orderLine['UnitPrice'] . ',' . - $orderLine['LinePrice']; + $lines[] = PHP_EOL . $orderLine['OrderID'] . ',' . + $orderLine['UserId'] . ',' . + $orderLine['Email'] . ',' . + $orderLine['DateTime'] . ',' . + $orderLine['ProdCode'] . ',' . + $orderLine['Quantity'] . ',' . + $orderLine['UnitPrice'] . ',' . + $orderLine['LinePrice']; } - return $data; + return implode('', $lines); } /** @@ -78,10 +81,9 @@ protected function validate($orderData) { $errors = []; foreach ($orderData as $orderLine) { - $errors = array_merge( - $errors, - parent::validate($orderLine) - ); + foreach (parent::validate($orderLine) as $error) { + $errors[] = $error; + } } return $errors;