Source of file MailChimpAPI.php
Size: 6,170 Bytes - Last Modified: 2021-12-23T10:36:56+00:00
/var/www/docs.ssmods.com/process/src/src/API/MailChimpAPI.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277 | <?php /** * This file is part of SilverWare. * * PHP version >=5.6.0 * * For full copyright and license information, please view the * LICENSE.md file that was distributed with this source code. * * @package SilverWare\MailChimp\API * @author Colin Tucker <colin@praxis.net.au> * @copyright 2017 Praxis Interactive * @license https://opensource.org/licenses/BSD-3-Clause BSD-3-Clause * @link https://github.com/praxisnetau/silverware-mailchimp */ namespace SilverWare\MailChimp\API; use DrewM\MailChimp\MailChimp; use SilverStripe\Core\Config\Configurable; use SilverStripe\Core\Injector\Injectable; use SilverStripe\SiteConfig\SiteConfig; use Exception; /** * A singleton wrapper providing access to the MailChimp API. * * @package SilverWare\MailChimp\API * @author Colin Tucker <colin@praxis.net.au> * @copyright 2017 Praxis Interactive * @license https://opensource.org/licenses/BSD-3-Clause BSD-3-Clause * @link https://github.com/praxisnetau/silverware-mailchimp */ class MailChimpAPI { use Injectable; use Configurable; /** * Defines the default API timeout period in seconds. * * @var integer * @config */ private static $default_timeout = 10; /** * Holds the MailChimp API instance. * * @var DrewM\MailChimp\MailChimp */ protected $api; /** * Defines the API timeout period in seconds. * * @var integer */ protected $timeout; /** * Constructs the object upon instantiation. */ public function __construct() { // Define Timeout: $this->timeout = self::config()->default_timeout; // Attempt API Instantiation: try { $this->api = new MailChimp($this->getAPIKey()); } catch (Exception $e) { // NOOP } } /** * Defines the value of the timeout attribute. * * @param integer $timeout * * @return $this */ public function setTimeout($timeout) { $this->timeout = (integer) $timeout; return $this; } /** * Answers the value of the timeout attribute. * * @return integer */ public function getTimeout() { return $this->timeout; } /** * Answers the API key from site or YAML configuration. * * @return string */ public function getAPIKey() { $key = SiteConfig::current_site_config()->MailChimpAPIKey; if (!$key) { $key = self::config()->api_key; } return $key; } /** * Answers true if the receiver has an API key. * * @return boolean */ public function hasAPIKey() { return (boolean) $this->getAPIKey(); } /** * Answers true if the receiver has an API key and the key is valid. * * @return boolean */ public function isAPIKeyValid() { return ($this->hasAPIKey() && is_object($this->api)); } /** * Issues an HTTP DELETE request on the API and answers the response. * * @params string $method * @params array $params * * @return array */ public function delete($method, $params = []) { return $this->request('delete', $method, $params); } /** * Issues an HTTP GET request on the API and answers the response. * * @params string $method * @params array $params * * @return array */ public function get($method, $params = []) { return $this->request('get', $method, $params); } /** * Issues an HTTP PATCH request on the API and answers the response. * * @params string $method * @params array $params * * @return array */ public function patch($method, $params = []) { return $this->request('patch', $method, $params); } /** * Issues an HTTP POST request on the API and answers the response. * * @params string $method * @params array $params * * @return array */ public function post($method, $params = []) { return $this->request('post', $method, $params); } /** * Issues an HTTP PUT request on the API and answers the response. * * @params string $method * @params array $params * * @return array */ public function put($method, $params = []) { return $this->request('put', $method, $params); } /** * Answers an MD5 hash for the given email address (used for API actions). * * @param string $email * * @return string */ public function hash($email) { return md5(strtolower(trim($email))); } /** * Answers true if the last request was successful. * * @return boolean */ public function success() { return $this->api()->success(); } /** * Answers the last error message returned by the API. * * @return string */ public function error() { return $this->api()->getLastError(); } /** * Answers the last response returned by the API. * * @return array */ public function response() { return $this->api()->getLastResponse(); } /** * Issues an HTTP request of the specified type and passes the method and parameters. * * @params string $type * @params string $method * @params array $params * * @return array */ protected function request($type, $method, $params) { return $this->api()->{$type}($method, $params, $this->timeout); } /** * Answers the API instance or throws an exception if the API is unavailable. * * @throws Exception * * @return DrewM\MailChimp\MailChimp */ protected function api() { if (!$this->api) { throw new Exception('MailChimp API unavailable'); } return $this->api; } } |