<?php
namespace Website\Widgets\MailChimp; use Moto; use Moto\Service\AbstractStaticService; use Zend; class Service extends AbstractStaticService { const ERROR_UNABLE_TO_SEND_MESSAGE = 'COMMON.ERROR.UNABLE_TO_SEND'; const ERROR_UNABLE_TO_SEND_CODE = 500; protected static $_resourceName = 'widget.mail_chimp'; protected static $_lists; public static function getOptions() { return Moto\Website\Integration::first('mailchimp')->toArray(); } public static function setOptions($request = null) { if (null === $request) { $request = static::getRequest()->getParams(); } $filter = new InputFilter\SetOptions(); $filter->setData($request); if (!$filter->isValid()) { throw new Moto\System\Exception(Moto\System\Exception::ERROR_BAD_REQUEST_MESSAGE, Moto\System\Exception::ERROR_BAD_REQUEST_CODE, $filter->getMessagesKeys()); } $api = new Api($filter->getValues()); static::$_lists = $api->get('lists', array('count' => '100')); $model = Moto\Website\Integration::first('mailchimp'); $model->fromArray($filter->getValues()); $model->save(); return $model->toArray(); } public static function getList() { if (!empty(static::$_lists)) { return static::$_lists; } $model = Moto\Website\Integration::first('mailchimp'); $api = new Api($model->toArray()); static::$_lists = $api->get('lists', array('count' => '100')); return static::$_lists; } public static function subscribe($request) { $filter = new InputFilter\Subscribe(); $filter->setData($request); if (!$filter->isValid()) { throw new Moto\System\Exception(Moto\System\Exception::ERROR_BAD_REQUEST_MESSAGE, Moto\System\Exception::ERROR_BAD_REQUEST_CODE, $filter->getMessagesKeys()); } $model = Moto\Website\Integration::first('mailchimp'); $api = new Api($model->toArray()); $result = $api->postListMembers($filter->getValue('list_id'), $filter->getValues()); return $result; } }