diff options
author | beardyman <nornholdj@gmail.com> | 2015-09-17 23:20:26 -0400 |
---|---|---|
committer | beardyman <nornholdj@gmail.com> | 2015-09-17 23:20:26 -0400 |
commit | efe48cc85e619768f69cd1d4843505bf9d146745 (patch) | |
tree | 9d0f5c7bae4a4d75745d30d969798a9c121155fd /lib/SparkPost/SparkPost.php | |
parent | b51ce2d0a8fad2577c164e1ab1c8382b1ae23fae (diff) | |
download | php-sparkpost-efe48cc85e619768f69cd1d4843505bf9d146745.zip php-sparkpost-efe48cc85e619768f69cd1d4843505bf9d146745.tar.gz php-sparkpost-efe48cc85e619768f69cd1d4843505bf9d146745.tar.bz2 |
Removed most static methods and adopted an instance based implementation. Updated all examples and compatibility later to reflect these changes.
Diffstat (limited to 'lib/SparkPost/SparkPost.php')
-rw-r--r-- | lib/SparkPost/SparkPost.php | 132 |
1 files changed, 7 insertions, 125 deletions
diff --git a/lib/SparkPost/SparkPost.php b/lib/SparkPost/SparkPost.php index 5bcb815..e64ff9f 100644 --- a/lib/SparkPost/SparkPost.php +++ b/lib/SparkPost/SparkPost.php @@ -1,138 +1,20 @@ <?php namespace SparkPost; -use Ivory\HttpAdapter; -use Ivory\HttpAdapter\HttpAdapterInterface; -use Ivory\HttpAdapter\Configuration; - class SparkPost { - private static $config; - private static $httpAdapter; - - private static $defaults = array( - 'host'=>'api.sparkpost.com', - 'protocol'=>'https', - 'port'=>443, - 'strictSSL'=>true, - 'key'=>'', - 'version'=>'v1' - ); - - /** - * Enforce that this object can't be instansiated - */ - private function __construct(){} - - /** - * @desc Helper function for getting the configuration for http requests - * @return \Ivory\HttpAdapter\Configuration - */ - // TODO: Need to figure out how to set strictSSL - private static function getHttpConfig($config) { - // get composer.json to extract version number - $composerFile = file_get_contents(dirname(__FILE__) . "/../../composer.json"); - $composer = json_decode($composerFile, true); - - // create Configuration for http adapter - $httpConfig = new Configuration(); - $baseUrl = $config['protocol'] . '://' . $config['host'] . ($config['port'] ? ':' . $config['port'] : '') . '/api/' . $config['version']; - $httpConfig->setBaseUri($baseUrl); - $httpConfig->setUserAgent('php-sparkpost/' . $composer['version']); - return $httpConfig; - } + public $transmission; /** - * @desc Convenience function for setting the httpAdapter and config in one step + * @desc sets up httpAdapter and config * - * @param Ivory\HttpAdapter $httpAdapter - an adapter for making http requests + * Sets up instances of sub libraries. + * + * @param Ivory\HttpAdapter $httpAdapter - An adapter for making http requests * @param Array $settingsConfig - Hashmap that contains config values for the SDK to connect to SparkPost - */ - public static function configure($httpAdapter, $settingsConfig) { - //need to set the config prior to setting up the adapter because of default settings for the adapter - self::setConfig($settingsConfig); - self::setHttpAdapter($httpAdapter); - } - - /** - * Allows the user to pass in values to override the defaults and set their API key - * @param Array $settingsConfig - Hashmap that contains config values for the SDK to connect to SparkPost - * @throws \Exception - */ - public static function setConfig(Array $settingsConfig) { - // Validate API key because its required - if (!isset($settingsConfig['key']) || empty(trim($settingsConfig['key']))){ - throw new \Exception('You must provide an API key'); - } - - self::$config = self::$defaults; - - // set config, overriding defaults - foreach ($settingsConfig as $configOption => $configValue) { - if(key_exists($configOption, self::$config)) { - self::$config[$configOption] = $configValue; - } - } - } - - /** - * @desc Merges passed in headers with default headers for http requests - * @return Array - headers to be set on http requests - */ - public static function getHttpHeaders(Array $headers = null) { - $defaultOptions = [ - 'Authorization' => self::getConfig()['key'], - 'Content-Type' => 'application/json', - ]; - - // Merge passed in headers with defaults - if (!is_null($headers)) { - foreach ($headers as $header => $value) { - $defaultOptions[$header] = $value; - } - } - return $defaultOptions; - } - - /** - * Retrieves the configuration that was previously setup by the user - * @throws \Exception - */ - public static function getConfig() { - if (self::$config === null) { - throw new \Exception('No configuration has been provided'); - } - return self::$config; - } - - /** - * TODO: Docs */ - public static function unsetConfig() { - self::$config = NULL; - } - - /** - * TODO: Docs - */ - public static function setHttpAdapter($httpAdapter) { - if (!$httpAdapter instanceOf HttpAdapterInterface) { - throw new \Exception('$httpAdapter paramter must be a valid Ivory\HttpAdapter'); - } - - self::$httpAdapter = $httpAdapter; - self::$httpAdapter->setConfiguration(self::getHttpConfig(self::getConfig())); + public function __construct($httpAdapter, $settingsConfig) { + $this->transmission = new Transmission($httpAdapter, $settingsConfig); } - - /** - * Retrieves the Http Adapter that was previously setup by the user - * @throws \Exception - */ - public static function getHttpAdapter() { - if (self::$httpAdapter === null) { - throw new \Exception('No Http Adapter has been provided'); - } - return self::$httpAdapter; - } } ?> |