/**
  * Initialize the logging framework.
  *
  * Will add all LogStreams registered under logging/enabled-log-streams. These
  * handlers must have class instantiation entries under logging/log-streams/<name>.
  *
  * @param string        $name      Root context name
  * @param int           $threshold Minimum log level to record into the context
  * @param Configuration $config    Configuration object to use
  * @param string        $prefix    Base prefix for logger
  */
 static function init($name, $threshold, Configuration $config, $prefix)
 {
     if (self::$context) {
         // FIXME: is this necessary?
         throw new SmashPigException("Attempting to reinitialize the logger is not allowed!");
     }
     // Init all the log streams
     $streamObjs = array();
     try {
         $streams = $config->val('logging/enabled-log-streams');
         foreach ($streams as $streamName) {
             $streamObjs[] = $config->object("logging/log-streams/{$streamName}", false);
         }
     } catch (\Exception $ex) {
         trigger_error("Exception while creating default log streams: {$ex->getMessage()} at {$ex->getTraceAsString()}", E_USER_ERROR);
         die;
     }
     self::$context = new LogContextHandler($name, $streamObjs);
     self::$context->enterContext($prefix);
     self::$threshold = $threshold;
 }
 public function execute()
 {
     $this->config = Configuration::getDefaultConfig();
     if ($this->is_reject()) {
         // Returning false would cause it to go to the damaged queue, we
         // just want to forget about these.
         return true;
     }
     // XXX Why does everything get made into objects?
     $request = (array) $this->payload;
     // Determine message type.
     if (isset($request['txn_type'])) {
         $txn_type = $request['txn_type'];
     } elseif (isset($request['payment_status']) && in_array($request['payment_status'], array('Reversed', 'Refunded'))) {
         // refund, chargeback, or reversal
         $txn_type = 'refund';
     } else {
         throw new Exception('Invalid PayPal message: ' . json_encode($request));
     }
     $msg_type = null;
     foreach ($this->config->val('messages') as $type => $conf) {
         if (in_array($txn_type, $conf['txn_types'])) {
             $msg_type = $type;
         }
     }
     if (!$msg_type) {
         throw new Exception('Invalid PayPal message type: ' . $txn_type);
     }
     // Transform into new message.
     // FIXME this could just be an array, but we need compat with
     // keyedopaque* until activemq goes away
     $new_msg = new Message();
     // FIXME hacks because the recurring consumer doesn't want
     // a normalized message
     if ($msg_type === 'recurring') {
         foreach ($request as $key => $val) {
             $new_msg->{$key} = $val;
         }
     } else {
         $map = $this->config->val('var_map');
         foreach ($map as $rx => $tx) {
             if (array_key_exists($rx, $request)) {
                 $new_msg->{$tx} = $request[$rx];
             }
         }
         // FIXME: var map can't put one thing in two places
         if (isset($new_msg->contribution_tracking_id)) {
             $new_msg->order_id = $new_msg->contribution_tracking_id;
         }
         // FIXME represent special case as var_map config override?
         if ($msg_type === 'refund') {
             $new_msg->gateway_refund_id = $request['txn_id'];
             $new_msg->gross_currency = $request['mc_currency'];
             if (isset($new_msg->type) && $new_msg->type === 'chargeback_settlement') {
                 $new_msg->type = 'chargeback';
             } else {
                 $new_msg->type = $msg_type;
             }
         }
         // FIXME once recurring uses normalized msg it needs this too
         $new_msg->date = strtotime($new_msg->date);
     }
     $new_msg->gateway = 'paypal';
     SourceFields::addToMessage($new_msg);
     // Save to appropriate queue.
     $this->config->object('data-store/' . $msg_type)->push($new_msg);
     // FIXME random document formats
     if (substr($txn_type, 0, 7) === 'subscr_') {
         $log_id = "subscr_id:{$request['subscr_id']}";
     } else {
         $log_id = "txn_id:{$request['txn_id']}";
     }
     Logger::info("Message {$log_id} pushed to {$msg_type} queue.");
     // TODO It would be nice if push() returned something useful so we
     // could return something here too
     return true;
 }