<?php

/**
 * mbc-user-digest_campaigns.php
 *
 * A consumer to process entries in the digestCampaignRequeuestsQueue via
 * the directUserDigest exchange. The mbp-user-digest_campaigns application
 * processes the entries in the digestCampaignRequeuestsQueue to generate
 * message entries in the digestCampaignQueue of campaign markup items to
 * use for digest messages sent via Mandrill.
 */
date_default_timezone_set('America/New_York');
define('CONFIG_PATH', __DIR__ . '/messagebroker-config');
// Load up the Composer autoload magic
require_once __DIR__ . '/vendor/autoload.php';
use DoSomething\MBC_DigestEmail\MBC_DigestEmailCampaignsConsumer;
use DoSomething\StatHat\Client as StatHat;
use DoSomething\MB_Toolbox\MB_Toolbox;
// Load configuration settings specific to this application
require_once __DIR__ . '/mbc-digest-email_campaigns.config.inc';
// Create objects for injection into MBC_ImageProcessor
$mb = new MessageBroker($credentials['rabbit'], $config);
$sh = new StatHat(['ez_key' => $credentials['stathat']['stathat_ez_key'], 'debug' => $credentials['stathat']['stathat_disable_tracking']]);
$tb = new MB_Toolbox($settings);
echo '------- mbc-user-digest START: ' . date('j D M Y G:i:s T') . ' -------', PHP_EOL;
$mb->consumeMessage(array(new MBC_DigestEmailCampaignsConsumer($mb, $sh, $tb, $settings), 'consumeUserDigestQueue'));
echo '------- mbc-user-digest END: ' . date('j D M Y G:i:s T') . ' -------', PHP_EOL;
    public function __construct($settings)
    {
        $this->settings = $settings;
    }
    /**
     * Submit user campaign activity to the UserAPI
     *
     * @param array $payload
     *   The contents of the queue entry
     */
    public function consumerQueue($message)
    {
        echo '------- mbc-resubscribe-mailchimp MBI_MailchimpResubscribe consumerQueue START' . date('D M j G:i:s T Y') . ' -------', PHP_EOL;
        $resubscribe = unserialize($message->body);
        // Submit subscription to Mailchimp
        $mc = new \Drewm\MailChimp($this->settings['mailchimp_apikey']);
        // Debugging
        //$results1 = $mc->call("lists/list", array());
        //$results2 = $mc->call("lists/interest-groupings", array('id' => 'f2fab1dfd4'));  // DoSomething Members f2fab1dfd4, Old People a27895fe0c
        $results = $mc->call("lists/subscribe", array('id' => 'a27895fe0c', 'email' => array('email' => $resubscribe['email']), 'merge_vars' => array('groupings' => array(0 => array('id' => $this->settings['mailchimp_grouping_id'])), 'FNAME' => $resubscribe['FNAME'], 'MMERGE10' => isset($resubscribe['birthdate']) ? $resubscribe['birthdate'] : ''), 'double_optin' => FALSE, 'update_existing' => TRUE, 'replace_interests' => FALSE, 'send_welcome' => FALSE));
        echo '-------  mbc-resubscribe-mailchimp MBI_MailchimpResubscribe consumerQueue - END' . date('D M j G:i:s T Y') . ' -------', PHP_EOL;
    }
}
// Settings
$credentials = array('host' => getenv("RABBITMQ_HOST"), 'port' => getenv("RABBITMQ_PORT"), 'username' => getenv("RABBITMQ_USERNAME"), 'password' => getenv("RABBITMQ_PASSWORD"), 'vhost' => getenv("RABBITMQ_VHOST"));
$config = array('exchange' => array('name' => getenv("MB_SCRIPT_EXCHANGE"), 'type' => getenv("MB_SCRIPT_EXCHANGE_TYPE"), 'passive' => getenv("MB_SCRIPT_EXCHANGE_PASSIVE"), 'durable' => getenv("MB_SCRIPT_EXCHANGE_DURABLE"), 'auto_delete' => getenv("MB_SCRIPT_EXCHANGE_AUTO_DELETE")), 'queue' => array('mailchimpResubscribeQueue' => array('name' => getenv("MB_MAILCHIMP_RESUBSCRIBE_QUEUE"), 'passive' => getenv("MB_MAILCHIMP_RESUBSCRIBE_QUEUE_PASSIVE"), 'durable' => getenv("MB_MAILCHIMP_RESUBSCRIBE_QUEUE_DURABLE"), 'exclusive' => getenv("MB_MAILCHIMP_RESUBSCRIBE_QUEUE_EXCLUSIVE"), 'auto_delete' => getenv("MB_MAILCHIMP_RESUBSCRIBE_QUEUE_AUTO_DELETE"), 'bindingKey' => getenv("MB_MAILCHIMP_RESUBSCRIBE_QUEUE_BINDING_KEY"))), 'routingKey' => 'old-people.mailchimp.resubscribe');
$settings = array('mailchimp_apikey' => getenv("MAILCHIMP_APIKEY"), 'mailchimp_list_id' => getenv("MAILCHIMP_LIST_ID"), 'mailchimp_grouping_id' => 333, 'stathat_ez_key' => getenv("STATHAT_EZKEY"));
// Kick off
$mb = new MessageBroker($credentials, $config);
$mb->consumeMessage(array(new MBI_MailchimpResubscribe($settings), 'consumerQueue'));