Skip to content
This repository has been archived by the owner on Mar 6, 2024. It is now read-only.

Commit

Permalink
all events now use the queue, also added a 500ms delay so we don't hi…
Browse files Browse the repository at this point in the history
…t the robin api limit
  • Loading branch information
pepijnblom committed Dec 16, 2015
1 parent 106ff0e commit 7483d77
Show file tree
Hide file tree
Showing 11 changed files with 209 additions and 161 deletions.
26 changes: 11 additions & 15 deletions app/code/community/Robinhq/Hooks/Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,48 +108,44 @@ public function getCollector() {
}

public function formatPhoneNumber($phoneNumber, $countryCode) {
Mage::log(__METHOD__, null, 'pepijn.log');
$phoneNumberClean = preg_replace("/[^\d]/", "", $phoneNumber);
Mage::log('$countryCode = ' . $countryCode, null, 'pepijn.log');
Mage::log('$phoneNumber = ' . $phoneNumber, null, 'pepijn.log');
Mage::log('$phoneNumberClean = ' . $phoneNumberClean, null, 'pepijn.log');
$length = strlen($phoneNumber);
Mage::log('$length = ' . $length, null, 'pepijn.log');
$length = strlen($phoneNumberClean);
if($length == 10) {
$phoneNumberFormatted = $phoneNumber;
}
if($length == 11) {
Mage::log('$VAR = ' . substr($phoneNumberClean,0,2), null, 'pepijn.log');
elseif($length == 11) {
if($countryCode=='NL') {
if(substr($phoneNumberClean, 0, 2)=='31') {
$phoneNumberFormatted = substr_replace($phoneNumberClean,'0',0,2);
}
}else{
$phoneNumberFormatted = $phoneNumber;
}

}
Mage::log('$phoneNumberFormatted = ' . $phoneNumberFormatted, null, 'pepijn.log');
if(!isset($phoneNumberFormatted)) {
$phoneNumberFormatted = $phoneNumberClean;
}
return $phoneNumberFormatted;
}

/**
* Gets the amount of rewardpoints a customer has saved up
* Get reward points for a customer
*
* @param Mage_Customer_Model_Customer $_customer
* @return int
*/
public function getRewardPoints() {
public function getRewardPoints($_customer) {
$points = 0;
$allStores = Mage::app()->getStores();
if ($this->customer->getId()) {
if ($_customer->getId()) {
foreach ($allStores as $_eachStoreId => $val) {
$_storeId = Mage::app()->getStore($_eachStoreId)->getId();
if (Mage::getStoreConfig('rewardpoints/default/flatstats', $_storeId)) {
$reward_flat_model = Mage::getModel('rewardpoints/flatstats');
$points += $reward_flat_model->collectPointsCurrent($this->customer->getId(), $_storeId) + 0;
$points += $reward_flat_model->collectPointsCurrent($_customer->getId(), $_storeId) + 0;
} else {
$reward_model = Mage::getModel('rewardpoints/stats');
$points += $reward_model->getPointsCurrent($this->customer->getId(), $_storeId) + 0;
$points += $reward_model->getPointsCurrent($_customer->getId(), $_storeId) + 0;
}
}
}
Expand Down
94 changes: 39 additions & 55 deletions app/code/community/Robinhq/Hooks/Model/Api.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?php


/**
* A small wrapper to communicate with the Robin API.
* Class Robinhq_Hooks_Model_Api
*/
class Robinhq_Hooks_Model_Api
{
class Robinhq_Hooks_Model_Api {

/**
* @var Robinhq_Hooks_Model_Logger
Expand All @@ -16,19 +16,19 @@ class Robinhq_Hooks_Model_Api
* Gets and sets the dependency's
* @param Robinhq_Hooks_Model_Logger $logger
*/
public function __construct(Robinhq_Hooks_Model_Logger $logger)
{
public function __construct(Robinhq_Hooks_Model_Logger $logger) {

$this->logger = $logger;
}

public function customer($customer)
{
return $this->customers(array($customer));
public function customer($customer) {

return $this->customers([$customer]);
}

public function order($order)
{
return $this->orders(array($order));
public function order($order) {

return $this->orders([$order]);
}

/**
Expand All @@ -38,9 +38,9 @@ public function order($order)
* @param $customers
* @return mixed
*/
public function customers($customers)
{
$this->logger->log("Sending customers to ROBIN");
public function customers($customers) {

$this->logger->log('Sending customers to ROBIN');
return $this->post('customers', $customers);
}

Expand All @@ -51,10 +51,10 @@ public function customers($customers)
* @param $orders
* @return mixed
*/
public function orders($orders)
{
$this->logger->log("Sending orders to ROBIN");
return $this->post("orders", $orders);
public function orders($orders) {

$this->logger->log('Sending orders to ROBIN');
return $this->post('orders', $orders);
}

/**
Expand All @@ -65,8 +65,8 @@ public function orders($orders)
* @throws Exception
* @return resource
*/
private function setUpCurl($request)
{
private function setUpCurl($request) {

$config = Mage::getStoreConfig('settings/general');
if (!empty($config['api_key']) && !empty($config['api_secret'])) {
$url = $config['api_url'] . '/' . $request;
Expand All @@ -75,12 +75,9 @@ private function setUpCurl($request)
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERPWD, $config['api_key'] . ":" . $config['api_secret']);

return $ch;
}
throw new Exception(
'Missing API configuration, go to System -> Configuration -> ROBINHQ -> Settings and fill in your API credentials'
);
throw new Exception('Missing API configuration, go to System -> Configuration -> ROBINHQ -> Settings and fill in your API credentials');
}

/**
Expand All @@ -96,23 +93,17 @@ private function setUpCurl($request)
* @throws Robinhq_Hooks_Model_Exception_UnauthorizedException
* @throws Robinhq_Hooks_Model_Exception_UnknownStatusCodeException
*/
private function post($request, $values)
{
$errorCodes = $this->getErrorCodes();
private function post($request, $values) {

$errorCodes = $this->getErrorCodes();
$ch = $this->prepare($request, $values);

$responseInfo = $this->execute($ch);

$this->logger->log("Robin returned status: " . $responseInfo['http_code']);

if (in_array($responseInfo['http_code'], $errorCodes)) {
$error = Robinhq_Hooks_Model_Exception_RequestFailed::factory($responseInfo['http_code']);
$this->logger->log($error->getMessage());
throw $error;
}


return $responseInfo;
}

Expand All @@ -121,15 +112,12 @@ private function post($request, $values)
* @return mixed
* @throws Robinhq_Hooks_Model_Exception_RequestImpossibleException
*/
private function execute($ch)
{
private function execute($ch) {

if (curl_exec($ch) === false) {
curl_close($ch);
throw new Robinhq_Hooks_Model_Exception_RequestImpossibleException(
"Error: 'Unable to preform request to Robin, request was not executed.'"
);
throw new Robinhq_Hooks_Model_Exception_RequestImpossibleException("Error: 'Unable to preform request to Robin, request was not executed.'");
}

$responseInfo = curl_getinfo($ch);
curl_close($ch);
return $responseInfo;
Expand All @@ -141,38 +129,34 @@ private function execute($ch)
* @return resource
* @throws Exception
*/
private function prepare($request, $values)
{
$values = json_encode(array($request => $values));
private function prepare($request, $values) {

$values = json_encode([$request => $values]);
$ch = $this->setUpCurl($request);
$valuesLength = strlen($values);
$this->logger->log("Posting with: " . $values);
curl_setopt(
$ch,
CURLOPT_HTTPHEADER,
array(
'Content-Type: application/json',
'Content-Length: ' . $valuesLength
)
);
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json',
'Content-Length: ' . $valuesLength,
]);
curl_setopt($ch, CURLOPT_POSTFIELDS, $values);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_VERBOSE, true);
$fp = fopen(dirname(__FILE__) . '/robin_curl_output.txt', 'w');
curl_setopt($ch, CURLOPT_STDERR, $fp);
return $ch;
}

/**
* @return array
*/
private function getErrorCodes()
{
$codes = new Robinhq_Hooks_Model_Robin_StatusCode();
private function getErrorCodes() {

$errorCodes = array(
$codes::INTERNAL_SERVER_ERROR,
$codes = new Robinhq_Hooks_Model_Robin_StatusCode();
$errorCodes = [$codes::INTERNAL_SERVER_ERROR,
$codes::BAD_REQUEST,
$codes::UNAUTHORIZED,
$codes::RATE_LIMIT_EXCEEDED
);
$codes::RATE_LIMIT_EXCEEDED,
];
return $errorCodes;
}

Expand Down
64 changes: 27 additions & 37 deletions app/code/community/Robinhq/Hooks/Model/Collector.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php


class Robinhq_Hooks_Model_Collector
{
class Robinhq_Hooks_Model_Collector {

/**
* @var Robinhq_Hooks_Model_Queue
*/
Expand All @@ -23,36 +23,33 @@ class Robinhq_Hooks_Model_Collector
* @param Robinhq_Hooks_Model_Robin_Converter $converter
* @param $pagination
*/
public function __construct(
Robinhq_Hooks_Model_Queue $queue,
Robinhq_Hooks_Model_Robin_Converter $converter,
$pagination
) {
public function __construct(Robinhq_Hooks_Model_Queue $queue, Robinhq_Hooks_Model_Robin_Converter $converter, $pagination) {

$this->queue = $queue;
$this->converter = $converter;
$this->pagination = $pagination;
}

public function orders()
{
public function orders() {

$this->queue->setType(Robinhq_Hooks_Model_Queue::ORDER);
$collection = $this->getOrdersPaginated($this->pagination);
$this->walk($collection, "orderCallback");
}

public function customers()
{
public function customers() {

$this->queue->setType(Robinhq_Hooks_Model_Queue::CUSTOMER);
$collection = $this->getCustomersPaginated($this->pagination);
$this->walk($collection, "customerCallback");
$this->walk($collection, 'customerCallback');
}

/**
* @param $collection
* @param array $callback
*/
private function iterate($collection, array $callback)
{
private function iterate($collection, array $callback) {

$array = $collection->toArray();
$array = (array_key_exists('items', $array)) ? $array['items'] : $array;
array_map($callback, $array);
Expand All @@ -62,10 +59,10 @@ private function iterate($collection, array $callback)
*
* @param $customerData
*/
private function customerCallback($customerData)
{
/** @var Mage_Customer_Model_Customer $customer */
private function customerCallback($customerData) {


/** @var Mage_Customer_Model_Customer $customer */
$customer = Mage::getModel('customer/customer');
$customer->setData($customerData);
$customer = $this->converter->toRobinCustomer($customer);
Expand All @@ -77,8 +74,8 @@ private function customerCallback($customerData)
*
* @param $orderData
*/
private function orderCallback($orderData)
{
private function orderCallback($orderData) {

/** @var Mage_Sales_Model_Order $order */
$order = Mage::getModel('sales/order')->load($orderData['entity_id'], 'entity_id');
$order = $this->converter->toRobinOrder($order);
Expand All @@ -88,40 +85,33 @@ private function orderCallback($orderData)
/**
* @return Mage_Sales_Model_Order[]|Varien_Data_Collection
*/
private function getOrdersPaginated($size)
{
return Mage::getModel('sales/order')
->getCollection()
->addAttributeToSelect('entity_id')
->setPageSize($size);
private function getOrdersPaginated($size) {

return Mage::getModel('sales/order')->getCollection()->addAttributeToSelect('entity_id')->setPageSize($size);
}

/**
* @return Mage_Customer_Model_Customer[]|Varien_Data_Collection
*/
private function getCustomersPaginated($size)
{
return Mage::getModel('customer/customer')
->getCollection()
->addNameToSelect()
->addAttributeToSelect('email')
private function getCustomersPaginated($size) {

return Mage::getModel('customer/customer')->getCollection()->addNameToSelect()->addAttributeToSelect('email')
->joinAttribute('billing_telephone', 'customer_address/telephone', 'default_billing', null, 'left')
->addAttributeToSelect('created_at')
->addAttributeToSelect('twitter_handler')
->setPageSize($size);
->addAttributeToSelect('created_at')->addAttributeToSelect('twitter_handler')->setPageSize($size)
;
}

/**
* @param $collection
* @param $callback
*/
private function walk(Varien_Data_Collection $collection, $callback)
{
private function walk(Varien_Data_Collection $collection, $callback) {

$size = $collection->getLastPageNumber();
for ($i = 1; $i <= $size; $i++) {
$collection->setCurPage($i);
$collection->load();
$this->iterate($collection, array($this, $callback));
$this->iterate($collection, [$this, $callback]);
$collection->clear();
}
$this->queue->clear();
Expand Down
Loading

0 comments on commit 7483d77

Please sign in to comment.