* @license PHP License * @package WB * @subpackage base */ WBClass::load('WBEvent_Handler'); /** * WBEvent_Handler_Newsletter_Subscriber * * * @version 0.2.0 * @package WB * @subpackage base */ class WBEvent_Handler_Newsletter_Subscriber extends WBEvent_Handler { /** * Handler config * * Parameters: * - namespace: string or empty * * @var array */ protected $config = array( 'namespace' => '', 'optin ' => 'newsletter' ); /** * @var WBDatasource_Newsletter_Subscriber */ private $sub; /** * Constructor * * Initialize subscriber object */ final public function __construct() { $this->sub = WBClass::create('WBDatasource_Newsletter_Subscriber'); } /** * Add Newsletter Subscriber * * @param WBEvent $e * @return bool true to continue, false to stop processing * @throws Exception in case of error */ final public function process(WBEvent $e) { $config = array( 'namespace' => '', 'optin' => 'newsletter' ); $this->config = array_merge($config, $this->config); if (!$this->isSubscriber($e)) { return true; } $data = $this->mapEvent2Data($e); if (empty($data)) { return true; } $this->sub->add($data); $ns = $this->config['namespace']; $xid = $this->getSource($e, $ns); if (0 > $xid) { return true; } $this->sub->addSource($ns, $xid); return true; } /** * Trigger Subscription * * Check whether to add subscriber * * @param WBEvent * @return bool */ protected function isSubscriber(WBEvent $e) { if (empty($this->config['optin'])) { return true; } $subscribe = intval($e->get($this->config['optin'])); if (1 > $subscribe) { return false; } return true; } /** * Map Event Data To Subscriber Data * * @param WBEvent * @return array */ protected function mapEvent2Data(WBEvent $e) { return $e->get(); } /** * Get Source If Any * * Return xid of source and set namespace, if any * * @param WBEvent * @param $namespace * @return int */ protected function getSource(WBEvent $e, &$namespace = '') { if (empty($namespace)) { return -1; } return 1; } }