Skip to content
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

Product Wrapper: response consistency / create & update methods #13

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
35 changes: 3 additions & 32 deletions src/FetchApp/API/FetchApp.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,23 +222,7 @@ public function getProducts($itemsPerPage = -1, $pageNumber = -1)
$results = APIWrapper::makeRequest($requestURL, "GET");
if (is_a($results, "SimpleXMLElement")) {
foreach ($results->product as $product) {
$tempProduct = new Product();
$tempProduct->setProductID($product->id);
$tempProduct->setSKU($product->sku);
$tempProduct->setName($product->name);
$tempProduct->setDescription($product->description);
$tempProduct->setPrice($product->price);
$tempProduct->setCurrency(Currency::getValue($product->currency));
$tempProduct->setOrderCount($product->order_count);
$tempProduct->setDownloadCount($product->download_count);
$tempProduct->setPaypalAddToCartLink($product->paypal_add_to_cart_link['href']);
$tempProduct->setPaypalBuyNowLink($product->paypal_buy_now_link['href']);
$tempProduct->setPaypalViewCartLink($product->paypal_view_cart_link['href']);
$tempProduct->setCreationDate(new \DateTime($product->created_at));
$tempProduct->setFilesUri($product->files_uri);
$tempProduct->setDownloadsUri($product->downloads_uri);

$products[] = $tempProduct;
$products[] = Product::fromXML($product);
}
}
return $products;
Expand All @@ -254,22 +238,9 @@ public function getProduct($productID)
$requestURL = "https://app.fetchapp.com/api/v2/products/" . $productID;
$product = APIWrapper::makeRequest($requestURL, "GET");
if (is_a($product, "SimpleXMLElement")) {
$tempProduct = new Product();
$tempProduct->setProductID($product->id);
$tempProduct->setSKU($product->sku);
$tempProduct->setName($product->name);
$tempProduct->setPrice($product->price);
$tempProduct->setCurrency(Currency::getValue($product->currency));
$tempProduct->setOrderCount($product->order_count);
$tempProduct->setDownloadCount($product->download_count);
$tempProduct->setPaypalAddToCartLink($product->paypal_add_to_cart_link['href']);
$tempProduct->setPaypalBuyNowLink($product->paypal_buy_now_link['href']);
$tempProduct->setPaypalViewCartLink($product->paypal_view_cart_link['href']);
$tempProduct->setCreationDate(new \DateTime($product->created_at));
$tempProduct->setFilesUri($product->files_uri);
$tempProduct->setDownloadsUri($product->downloads_uri);
return Product::fromXML($product);
}
return $tempProduct;
return false;
}

/**
Expand Down
62 changes: 36 additions & 26 deletions src/FetchApp/API/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -253,29 +253,19 @@ public function getCurrency()
* @param array $files
* @return mixed
*/
public function create(array $files)
public function create(array $files=[], array $item_urls=[])
{
APIWrapper::verifyReadiness();
$this->files = $files;
$this->item_urls = $item_urls;

$url = "https://app.fetchapp.com/api/v2/products/create";
$data = $this->toXML();

$response = APIWrapper::makeRequest($url, "POST", $data);

if (isset($response->id)) {
$this->setProductID($response->id);
$this->setSKU($response->sku);
$this->setName($response->name);
$this->setPrice($response->price);
$this->setOrderCount($response->order_count);
$this->setDownloadCount($response->download_count);
$this->setPaypalAddToCartLink($response->paypal_add_to_cart_link);
$this->setPaypalBuyNowLink($response->paypal_buy_now_link);
$this->setPaypalViewCartLink($response->paypal_view_cart_link);
$this->setCreationDate(new \DateTime($response->created_at));
$this->setFilesUri($response->files_uri);
$this->setDownloadsUri($response->downloads_uri);
self::fromXML($response, $this);
return true;
} else {
// It failed, let's return the error
Expand All @@ -287,28 +277,18 @@ public function create(array $files)
* @param array $files
* @return mixed
*/
public function update(array $item_urls)
public function update(array $files=[], array $item_urls=[])
{
APIWrapper::verifyReadiness();
$this->files = $files;
$this->item_urls = $item_urls;

$url = "https://app.fetchapp.com/api/v2/products/" . $this->ProductID . "/update";
$data = $this->toXML();

$response = APIWrapper::makeRequest($url, "PUT", $data);
if (isset($response->id)) {
$this->setProductID($response->id);
$this->setSKU($response->sku);
$this->setName($response->name);
$this->setPrice($response->price);
$this->setOrderCount($response->order_count);
$this->setDownloadCount($response->download_count);
$this->setPaypalAddToCartLink($response->paypal_add_to_cart_link);
$this->setPaypalBuyNowLink($response->paypal_buy_now_link);
$this->setPaypalViewCartLink($response->paypal_view_cart_link);
$this->setCreationDate(new \DateTime($response->created_at));
$this->setFilesUri($response->files_uri);
$this->setDownloadsUri($response->downloads_uri);
self::fromXML($response, $this);
return true;
} else {
// It failed, let's return the error
Expand Down Expand Up @@ -431,4 +411,34 @@ public function toXML($sendEmailFlag = true)

return $productXML->asXML();
}

public static function fromXML($productXML, Product $productToUpdate = null)
{
if(!$productXML instanceof \SimpleXMLElement){
return false;
}

if($productToUpdate){
$product = $productToUpdate;
} else {
$product = new Product();
}

$product->setProductID((string)$productXML->id);
$product->setSKU((string)$productXML->sku);
$product->setName((string)$productXML->name);
$product->setDescription((string)$productXML->description);
$product->setPrice((float)$productXML->price);
$product->setCurrency(Currency::getValue($productXML->currency));
$product->setOrderCount((int)$productXML->order_count);
$product->setDownloadCount((int)$productXML->download_count);
$product->setPaypalAddToCartLink((string)$productXML->paypal_add_to_cart_link['href']);
$product->setPaypalBuyNowLink((string)$productXML->paypal_buy_now_link['href']);
$product->setPaypalViewCartLink((string)$productXML->paypal_view_cart_link['href']);
$product->setCreationDate(new \DateTime($productXML->created_at));
$product->setFilesUri((string)$productXML->files_uri);
$product->setDownloadsUri((string)$productXML->downloads_uri);

return $product;
}
}