From 7483d77621407af81ce30786636df46ff2010c67 Mon Sep 17 00:00:00 2001 From: Pepijn Blom Date: Wed, 16 Dec 2015 16:53:50 +0100 Subject: [PATCH] all events now use the queue, also added a 500ms delay so we don't hit the robin api limit --- .../community/Robinhq/Hooks/Helper/Data.php | 26 ++--- .../community/Robinhq/Hooks/Model/Api.php | 94 +++++++--------- .../Robinhq/Hooks/Model/Collector.php | 64 +++++------ .../community/Robinhq/Hooks/Model/Logger.php | 20 ++-- .../Robinhq/Hooks/Model/Observer.php | 101 +++++++++++++++--- .../community/Robinhq/Hooks/Model/Queue.php | 7 +- .../Robinhq/Hooks/Model/Queue/Customers.php | 12 +-- .../Robinhq/Hooks/Model/Queue/Mass.php | 29 +++-- .../Robinhq/Hooks/Model/Queue/Orders.php | 13 +-- .../Robinhq/Hooks/Model/Robin/Customer.php | 2 +- .../Adminhtml/HooksbackendController.php | 2 +- 11 files changed, 209 insertions(+), 161 deletions(-) diff --git a/app/code/community/Robinhq/Hooks/Helper/Data.php b/app/code/community/Robinhq/Hooks/Helper/Data.php index 304cdb6..60033db 100755 --- a/app/code/community/Robinhq/Hooks/Helper/Data.php +++ b/app/code/community/Robinhq/Hooks/Helper/Data.php @@ -108,18 +108,12 @@ 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); @@ -127,29 +121,31 @@ public function formatPhoneNumber($phoneNumber, $countryCode) { }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; } } } diff --git a/app/code/community/Robinhq/Hooks/Model/Api.php b/app/code/community/Robinhq/Hooks/Model/Api.php index 7f501d5..f3ee917 100755 --- a/app/code/community/Robinhq/Hooks/Model/Api.php +++ b/app/code/community/Robinhq/Hooks/Model/Api.php @@ -1,11 +1,11 @@ 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]); } /** @@ -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); } @@ -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); } /** @@ -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; @@ -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'); } /** @@ -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; } @@ -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; @@ -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; } diff --git a/app/code/community/Robinhq/Hooks/Model/Collector.php b/app/code/community/Robinhq/Hooks/Model/Collector.php index 984f480..99db47a 100755 --- a/app/code/community/Robinhq/Hooks/Model/Collector.php +++ b/app/code/community/Robinhq/Hooks/Model/Collector.php @@ -1,8 +1,8 @@ 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); @@ -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); @@ -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); @@ -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(); diff --git a/app/code/community/Robinhq/Hooks/Model/Logger.php b/app/code/community/Robinhq/Hooks/Model/Logger.php index 97b7581..2aff75f 100755 --- a/app/code/community/Robinhq/Hooks/Model/Logger.php +++ b/app/code/community/Robinhq/Hooks/Model/Logger.php @@ -1,21 +1,21 @@ helper = Mage::helper("hooks"); + $this->helper = Mage::helper('hooks'); $this->push = $this->helper->getQueue(); $this->api = $this->helper->getApi(); $this->enabled = $this->isEnabled(); @@ -48,7 +48,45 @@ public function __construct() { * * @param Varien_Event_Observer|Mage_Customer_Model_Customer $customer */ - public function customerHook($customer) { + public function customerHook($_customer) { + + if ($this->enabled) { + if ($_customer instanceof Varien_Event_Observer) { + $_customer = $_customer->getEvent()->getCustomer(); + } + if ($_customer) { + try { + $this->helper->log('User with id: ' . $_customer->getId() . ' changed, sending it to ROBIN'); + $config = Mage::getStoreConfig('settings/general'); + $bulkLimit = (int)$config['bulk_limit']; + $selectLimit = (int)$config['select_limit']; + $logger = new Robinhq_Hooks_Model_Logger(); + $api = new Robinhq_Hooks_Model_Api($logger); + $queue = new Robinhq_Hooks_Model_Queue($logger, $api, $bulkLimit); + $queue->setType(Robinhq_Hooks_Model_Queue::CUSTOMER); + $converter = new Robinhq_Hooks_Model_Robin_Converter(); + $collector = new Robinhq_Hooks_Model_Collector($queue, $converter, $selectLimit); + $aCustomer = $converter->toRobinCustomer($_customer); + $queue->pushImmediately($aCustomer); + } + catch (Exception $e) { + $this->helper->log('Exception: ' . $e->getMessage()); + $this->helper->warnAdmin($e->getMessage()); + } + } + } + } + + + + /** + * Fires when the customer_save_after_handler events is dispatched. + * This happens when a customer is created and when an customer places an order. + * Also fires when a customer is created/edited through the backend. + * + * @param Varien_Event_Observer|Mage_Customer_Model_Customer $customer + */ + public function customerHookOld($customer) { if ($this->enabled) { if ($customer instanceof Varien_Event_Observer) { @@ -68,6 +106,8 @@ public function customerHook($customer) { } } + + /** * Fires when the sales_order_place_after_handler event is dispatched * and sends the processed order to Robin @@ -102,6 +142,52 @@ public function orderPlacedHook(Varien_Event_Observer $observer) { */ public function orderStatusChangeHook(Varien_Event_Observer $observer) { + + if ($this->enabled) { + $_order = $observer->getEvent()->getOrder(); + $_status = $_order->getStatus(); + //only fire when we actually have an status + if (is_string($_status)) { + $string = 'The status of order #' . $_order->getIncrementId() . ' changed to: ' . $_order->getStatus(); + $this->helper->log($string); + try { + $this->helper->log('Order has changed, sending it to Robin'); + + if ($_order->getCustomerId() !== null) { + $_customer = Mage::getModel('customer/customer')->load($_order->getCustomerId()); + $this->customerHook($_customer); + } + $config = Mage::getStoreConfig('settings/general'); + $bulkLimit = (int)$config['bulk_limit']; + $selectLimit = (int)$config['select_limit']; + $logger = new Robinhq_Hooks_Model_Logger(); + $api = new Robinhq_Hooks_Model_Api($logger); + $queue = new Robinhq_Hooks_Model_Queue($logger, $api, $bulkLimit); + $queue->setType(Robinhq_Hooks_Model_Queue::ORDER); + $converter = new Robinhq_Hooks_Model_Robin_Converter(); + $collector = new Robinhq_Hooks_Model_Collector($queue, $converter, $selectLimit); + $aOrder = $converter->toRobinOrder($_order); + $queue->pushImmediately($aOrder); + + + } + catch (Exception $e) { + $this->helper->log('Exception: ' . $e->getMessage()); + $this->helper->warnAdmin($e->getMessage()); + } + } + } + } + + /** + * Fires when the sales_order_save_after_handler event is dispatched + * and sends the changed order to ROBIN. This event captures all changes made + * to an order. It only sends the order to Robin when it has a status. + * + * @param Varien_Event_Observer $observer + */ + public function orderStatusChangeHookOld(Varien_Event_Observer $observer) { + if ($this->enabled) { $order = $observer->getEvent()->getOrder(); $status = $order->getStatus(); @@ -127,6 +213,7 @@ public function orderStatusChangeHook(Varien_Event_Observer $observer) { } } + private function isEnabled() { $config = Mage::getStoreConfig('settings/general'); @@ -140,16 +227,6 @@ private function isEnabled() { */ public function controllerFrontInitBefore(Varien_Event_Observer $event) { - // Mage::log(__METHOD__, null, 'pepijn.log'); - // Mage::log('----------------------------', null, 'pepijn.log'); - // Mage::helper('hooks')->formatPhoneNumber('0624289059', 'NL'); - // Mage::log('----------------------------', null, 'pepijn.log'); - // Mage::helper('hooks')->formatPhoneNumber('31624289059', 'NL'); - // Mage::log('----------------------------', null, 'pepijn.log'); - // Mage::helper('hooks')->formatPhoneNumber('32624289059', 'BE'); - // Mage::log('----------------------------', null, 'pepijn.log'); - // $address = Mage::getModel('customer/address')->load(1000); - // Mage::log('$address = ' . $address->getCountryId(), null, 'pepijn.log'); } diff --git a/app/code/community/Robinhq/Hooks/Model/Queue.php b/app/code/community/Robinhq/Hooks/Model/Queue.php index 07729dc..746b1f8 100755 --- a/app/code/community/Robinhq/Hooks/Model/Queue.php +++ b/app/code/community/Robinhq/Hooks/Model/Queue.php @@ -42,11 +42,16 @@ public function setType($type) { } public function push($model) { - $this->models[] = $model; $this->enqueueWhenBatchLimitIsReached(); } + public function pushImmediately($model) { + $this->models[] = $model; + $this->enqueue(); + $this->reset(); + } + /** * Enqueue's the remaining items in $this->models, * afterwards it resets the $this->models to an empty array. diff --git a/app/code/community/Robinhq/Hooks/Model/Queue/Customers.php b/app/code/community/Robinhq/Hooks/Model/Queue/Customers.php index 2a6cd6d..e45c437 100755 --- a/app/code/community/Robinhq/Hooks/Model/Queue/Customers.php +++ b/app/code/community/Robinhq/Hooks/Model/Queue/Customers.php @@ -1,8 +1,8 @@ customers = $customers; $this->api = $api; } - public function perform() - { + public function perform() { + // Add a 1/2 second sleep so we don't hit the robin api limits too quickly + usleep(500000); $this->api->customers($this->customers); } } \ No newline at end of file diff --git a/app/code/community/Robinhq/Hooks/Model/Queue/Mass.php b/app/code/community/Robinhq/Hooks/Model/Queue/Mass.php index 2e2cf8b..d027b2b 100755 --- a/app/code/community/Robinhq/Hooks/Model/Queue/Mass.php +++ b/app/code/community/Robinhq/Hooks/Model/Queue/Mass.php @@ -1,8 +1,8 @@ collector = $helper->getCollector(); $this->logger = $helper->getLogger(); $this->helper = $helper; } - public function perform() - { - $this->logger->log("Robin Mass Sender started queueing up customers and orders"); + public function perform() { + + $this->logger->log('Robin Mass Sender started queueing up customers and orders'); try { $this->collector->customers(); $this->collector->orders(); - $this->logger->log( - "Robin Mass Sender finished building the queue. Wait unitll the queue kicks in and - handles the jobs" - ); - $this->helper->noticeAdmin( - "All customers and orders are send to the queue. Depending on your cron - settings, they'll soon be send to ROBIN" - ); - } catch (Exception $e) { + $this->logger->log('Robin Mass Sender finished building the queue. Wait unitll the queue kicks in and handles the jobs'); + $this->helper->noticeAdmin('All customers and orders are send to the queue. Depending on your cron settings, they will soon be sent to ROBIN'); + } + catch (Exception $e) { $this->helper->warnAdmin($e->getMessage()); - $this->logger->log("Mass send failed with message: " . $e->getMessage()); + $this->logger->log('Mass send failed with message: ' . $e->getMessage()); } } } \ No newline at end of file diff --git a/app/code/community/Robinhq/Hooks/Model/Queue/Orders.php b/app/code/community/Robinhq/Hooks/Model/Queue/Orders.php index bb36c38..0c0c5c1 100755 --- a/app/code/community/Robinhq/Hooks/Model/Queue/Orders.php +++ b/app/code/community/Robinhq/Hooks/Model/Queue/Orders.php @@ -1,8 +1,8 @@ api = $api; $this->orders = $orders; } - public function perform() - { + public function perform() { + + // Add a 1/2 second sleep so we don't hit the robin api limits too quickly + usleep(500000); $this->api->orders($this->orders); } } \ No newline at end of file diff --git a/app/code/community/Robinhq/Hooks/Model/Robin/Customer.php b/app/code/community/Robinhq/Hooks/Model/Robin/Customer.php index e5a96cc..f35f8f2 100755 --- a/app/code/community/Robinhq/Hooks/Model/Robin/Customer.php +++ b/app/code/community/Robinhq/Hooks/Model/Robin/Customer.php @@ -41,7 +41,7 @@ private function make() { $formattedTotalSpent = Mage::helper('core')->currency($lifetime->getLifetime(), true, false); $phoneNumber = $this->getCustomerPhoneNumber(); $orderCount = $lifetime->getNumOrders(); - $rewardPoints = Mage::helper('hooks')->getRewardPoints(); + $rewardPoints = Mage::helper('hooks')->getRewardPoints($this->customer); $this->data = [ 'email_address' => $this->customer->getEmail(), diff --git a/app/code/community/Robinhq/Hooks/controllers/Adminhtml/HooksbackendController.php b/app/code/community/Robinhq/Hooks/controllers/Adminhtml/HooksbackendController.php index 5aa5266..c3ef54a 100755 --- a/app/code/community/Robinhq/Hooks/controllers/Adminhtml/HooksbackendController.php +++ b/app/code/community/Robinhq/Hooks/controllers/Adminhtml/HooksbackendController.php @@ -60,7 +60,7 @@ public function runAction() { $massQueue = new Robinhq_Hooks_Model_Queue_Mass($this->helper); $massQueue->setName('ROBIN Mass Send'); $massQueue->enqueue(); - $this->helper->log('Done. Wait until the queue kicks in and handles this jobs'); + $this->helper->log('Done. Wait until the queue kicks in and handles these jobs'); $this->helper->noticeAdmin('The Mass Send process is pushed to the queue.'); } else { $message = 'Module is disabled. Please enable it first.';