diff options
author | beardyman <nornholdj@gmail.com> | 2015-09-28 22:51:29 -0400 |
---|---|---|
committer | beardyman <nornholdj@gmail.com> | 2015-09-28 22:51:29 -0400 |
commit | c753d55e2025c3948fe8a59a660a881a429b8df2 (patch) | |
tree | 86a658105eb00db0593c8543cb25c51d9b2d0cfb /lib/SparkPost/SparkPost.php | |
parent | a436c497229ada070588f9b0babdd615b1cacc1b (diff) | |
download | php-sparkpost-c753d55e2025c3948fe8a59a660a881a429b8df2.zip php-sparkpost-c753d55e2025c3948fe8a59a660a881a429b8df2.tar.gz php-sparkpost-c753d55e2025c3948fe8a59a660a881a429b8df2.tar.bz2 |
moved config setup and management back into the sparkpost object
Diffstat (limited to 'lib/SparkPost/SparkPost.php')
-rw-r--r-- | lib/SparkPost/SparkPost.php | 119 |
1 files changed, 118 insertions, 1 deletions
diff --git a/lib/SparkPost/SparkPost.php b/lib/SparkPost/SparkPost.php index e64ff9f..aac352b 100644 --- a/lib/SparkPost/SparkPost.php +++ b/lib/SparkPost/SparkPost.php @@ -1,10 +1,35 @@ <?php namespace SparkPost; +use Ivory\HttpAdapter\Configuration; +use Ivory\HttpAdapter\HttpAdapterInterface; + class SparkPost { public $transmission; /** + * @dec connection config for making requests. + */ + private $config; + + /** + * @desc Ivory\HttpAdapter\HttpAdapterInterface to make requests through. + */ + public $httpAdapter; + + /** + * @desc Default config values. Passed in values will override these. + */ + private static $apiDefaults = [ + 'host'=>'api.sparkpost.com', + 'protocol'=>'https', + 'port'=>443, + 'strictSSL'=>true, + 'key'=>'', + 'version'=>'v1' + ]; + + /** * @desc sets up httpAdapter and config * * Sets up instances of sub libraries. @@ -13,7 +38,99 @@ class SparkPost { * @param Array $settingsConfig - Hashmap that contains config values for the SDK to connect to SparkPost */ public function __construct($httpAdapter, $settingsConfig) { - $this->transmission = new Transmission($httpAdapter, $settingsConfig); + //config needs to be setup before adapter because of default adapter settings + $this->setConfig($settingsConfig); + $this->setHttpAdapter($httpAdapter); + + $this->transmission = new Transmission($this); + } + + + + /** + * Creates an unwrapped api interface for endpoints that aren't yet supported. + * The new resource is attached to this object as well as returned + * @return SparkPost\APIResource - the unwrapped resource + */ + public function setupUnwrapped (string $endpoint) { + $this->{$endpoint} = new APIResource($this); + $this->{$endpoint}->endpoint = $endpoint; + + return $this->{$endpoint}; + } + + /** + * @desc Merges passed in headers with default headers for http requests + * @return Array - headers to be set on http requests + */ + public function getHttpHeaders(Array $headers = null) { + $defaultOptions = [ + 'Authorization' => $this->config['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; + } + + + /** + * @desc Helper function for getting the configuration for http requests + * @return \Ivory\HttpAdapter\Configuration + */ + private 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; + } + + + /** + * @desc Validates and sets up the httpAdapter + * @param $httpAdapter Ivory\HttpAdapter\HttpAdapterInterface to make requests through. + * @throws \Exception + */ + public function setHttpAdapter($httpAdapter) { + if (!$httpAdapter instanceOf HttpAdapterInterface) { + throw new \Exception('$httpAdapter paramter must be a valid Ivory\HttpAdapter'); + } + + $this->httpAdapter = $httpAdapter; + $this->httpAdapter->setConfiguration($this->getHttpConfig($this->config)); + } + + + /** + * 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 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'); + } + + $this->config = self::$apiDefaults; + + // set config, overriding defaults + foreach ($settingsConfig as $configOption => $configValue) { + if(key_exists($configOption, $this->config)) { + $this->config[$configOption] = $configValue; + } + } } } |