Example #1
0
 /**
  * Send broadcast message
  *
  * @throws Mage_Core_Exception
  * @param Mage_XmlConnect_Model_Queue $queue
  */
 public function sendBroadcastMessage(Mage_XmlConnect_Model_Queue $queue)
 {
     if ($queue->getStatus() != Mage_XmlConnect_Model_Queue::STATUS_IN_QUEUE) {
         return;
     }
     try {
         $applicationId = Mage::getModel('xmlconnect/template')->load($queue->getTemplateId())->getApplicationId();
         /** @var $app Mage_XmlConnect_Model_Application */
         $app = Mage::getModel('xmlconnect/application')->load($applicationId);
         if (!$app->getId()) {
             Mage::throwException(Mage::helper('xmlconnect')->__('Can\'t load application with id "%s"', $applicationId));
         }
         if (!$app->isNotificationsActive()) {
             $queue->setStatus(Mage_XmlConnect_Model_Queue::STATUS_CANCELED);
             return;
         }
         $sendType = $queue->getData('type');
         switch ($sendType) {
             case Mage_XmlConnect_Model_Queue::MESSAGE_TYPE_AIRMAIL:
                 $configPath = 'xmlconnect/' . Mage_XmlConnect_Model_Queue::MESSAGE_TYPE_AIRMAIL . '/broadcast_url';
                 $params = $queue->getAirmailBroadcastParams();
                 break;
             case Mage_XmlConnect_Model_Queue::MESSAGE_TYPE_PUSH:
             default:
                 $configPath = 'xmlconnect/' . Mage_XmlConnect_Model_Queue::MESSAGE_TYPE_PUSH . '/broadcast_url';
                 $params = $queue->getPushBroadcastParams();
                 break;
         }
         $curl = new Varien_Http_Adapter_Curl();
         $curl->setConfig($this->_getCurlConfig($app->getUserpwd()));
         $urbanUrl = Mage::getStoreConfig($configPath);
         $curl->write(Zend_Http_Client::POST, $urbanUrl, HTTP_REQUEST_HTTP_VER_1_1, $this->getHttpHeaders(), $params);
         if ($curl->read() && $curl->getInfo(CURLINFO_HTTP_CODE) == 200) {
             $queue->setStatus(Mage_XmlConnect_Model_Queue::STATUS_COMPLETED);
         }
         $curl->close();
         $queue->setIsSent(true);
         $queue->save();
         return;
     } catch (Exception $e) {
         Mage::logException($e);
         throw $e;
     }
 }
Example #2
0
 /**
  * Send broadcast message
  *
  * @throws Mage_Core_Exception
  * @param Mage_XmlConnect_Model_Queue $queue
  */
 public function sendBroadcastMessage(Mage_XmlConnect_Model_Queue $queue)
 {
     if ($queue->getStatus() != Mage_XmlConnect_Model_Queue::STATUS_IN_QUEUE) {
         return;
     }
     try {
         /** @var $app Mage_XmlConnect_Model_Application */
         $templateModel = Mage::getModel('xmlconnect/template')->load($queue->getTemplateId());
         if (!$templateModel->getId()) {
             Mage::throwException(Mage::helper('xmlconnect')->__('Can\'t load template with id "%s"', $templateModel->getId()));
         }
         $app = Mage::getModel('xmlconnect/application')->load($templateModel->getApplicationId());
         if (!$app->getId()) {
             Mage::throwException(Mage::helper('xmlconnect')->__('Can\'t load application with id "%s"', $templateModel->getApplicationId()));
         }
         if (!$app->isNotificationsActive()) {
             $queue->setStatus(Mage_XmlConnect_Model_Queue::STATUS_CANCELED);
             return;
         }
         $sendType = $queue->getData('type');
         switch ($sendType) {
             case Mage_XmlConnect_Model_Queue::MESSAGE_TYPE_AIRMAIL:
                 $configPath = 'xmlconnect/' . Mage_XmlConnect_Model_Queue::MESSAGE_TYPE_AIRMAIL . '/broadcast_url';
                 $params = $queue->getAirmailBroadcastParams();
                 break;
             case Mage_XmlConnect_Model_Queue::MESSAGE_TYPE_PUSH:
             default:
                 $configPath = 'xmlconnect/' . Mage_XmlConnect_Model_Queue::MESSAGE_TYPE_PUSH . '/broadcast_url';
                 $params = $queue->getPushBroadcastParams();
                 break;
         }
         $curlHandler = curl_init(Mage::getStoreConfig($configPath));
         $httpHeaders = $this->getHttpHeaders();
         curl_setopt($curlHandler, CURLOPT_POST, 1);
         curl_setopt($curlHandler, CURLOPT_HTTPHEADER, $httpHeaders);
         curl_setopt($curlHandler, CURLOPT_POSTFIELDS, $params);
         curl_setopt($curlHandler, CURLOPT_RETURNTRANSFER, 1);
         curl_setopt($curlHandler, CURLOPT_USERPWD, $app->getUserpwd());
         curl_setopt($curlHandler, CURLOPT_TIMEOUT, 60);
         // Execute the request.
         $result = curl_exec($curlHandler);
         $succeeded = curl_errno($curlHandler) == 0;
         $responseCode = curl_getinfo($curlHandler, CURLINFO_HTTP_CODE);
         // close cURL resource, and free up system resources
         curl_close($curlHandler);
         if ($succeeded && $responseCode == 200) {
             $queue->setStatus(Mage_XmlConnect_Model_Queue::STATUS_COMPLETED);
         } else {
             Mage::log($result);
             $queue->setStatus(Mage_XmlConnect_Model_Queue::STATUS_CANCELED);
         }
         $queue->setIsSent(true);
         $queue->save();
         return;
     } catch (Exception $e) {
         Mage::logException($e);
         throw $e;
     }
 }