Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 14 additions & 14 deletions src/PureClarity/Api/Feed/Feed.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 = [];
}
}

Expand All @@ -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);
}

Expand Down Expand Up @@ -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);
Expand Down
13 changes: 11 additions & 2 deletions src/PureClarity/Api/Feed/Transfer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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();
Expand All @@ -152,7 +161,7 @@ private function send($endPoint, $data)
);
}

if (empty($error) === false) {
if (!empty($error)) {
throw new Exception($error);
}

Expand Down
30 changes: 16 additions & 14 deletions src/PureClarity/Api/Feed/Type/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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);
}

/**
Expand All @@ -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;
Expand Down