diff options
author | Jason Rhodes <jason.matthew.rhodes@gmail.com> | 2016-05-03 22:10:04 -0400 |
---|---|---|
committer | Richard Leland <rich@richleland.com> | 2016-05-03 22:10:04 -0400 |
commit | d4c3a89c103e551a3cd71d20f36414095158365b (patch) | |
tree | 86ff4277800b41e1cf9def71b1753d53c0b25def /lib/SparkPost/SparkPost.php | |
parent | d96d825aa138af92628ec7311c05e9659917ba79 (diff) | |
download | php-sparkpost-d4c3a89c103e551a3cd71d20f36414095158365b.zip php-sparkpost-d4c3a89c103e551a3cd71d20f36414095158365b.tar.gz php-sparkpost-d4c3a89c103e551a3cd71d20f36414095158365b.tar.bz2 |
Add php-cs-fixer as composer script and make initial fix-style run
* Add php-cs-fixer as composer script
* Make initial fix-style run
* Fix indentation on all files
* Add editorconfig file
Diffstat (limited to 'lib/SparkPost/SparkPost.php')
-rw-r--r-- | lib/SparkPost/SparkPost.php | 258 |
1 files changed, 136 insertions, 122 deletions
diff --git a/lib/SparkPost/SparkPost.php b/lib/SparkPost/SparkPost.php index ffc84f0..92ad867 100644 --- a/lib/SparkPost/SparkPost.php +++ b/lib/SparkPost/SparkPost.php @@ -1,136 +1,150 @@ <?php + namespace SparkPost; + use Ivory\HttpAdapter\Configuration; use Ivory\HttpAdapter\HttpAdapterInterface; -class SparkPost { - - public $transmission; - public $messageEvents; - - /** - * Library version, used for setting User-Agent. - */ - private $version = '1.1.0'; - - /** - * Connection config for making requests. - */ - private $config; - - /** - * @var \Ivory\HttpAdapter\HttpAdapterInterface to make requests through. - */ - public $httpAdapter; - - /** - * 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' - ]; - - /** - * Sets up httpAdapter and config - * - * Sets up instances of sub libraries. - * - * @param \Ivory\HttpAdapter\HttpAdapterInterface $httpAdapter - An adapter for making http requests - * @param String | array $settingsConfig - Hashmap that contains config values - * for the SDK to connect to SparkPost. If its a string we assume that - * its just they API Key. - */ - public function __construct($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); - $this->messageEvents = new MessageEvents($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 - * @param string $endpoint - * @return APIResource - the unwrapped resource - */ - public function setupUnwrapped ($endpoint) { - $this->{$endpoint} = new APIResource($this); - $this->{$endpoint}->endpoint = $endpoint; - - return $this->{$endpoint}; - } - - /** - * Merges passed in headers with default headers for http requests - */ - public function getHttpHeaders() { - $defaultOptions = [ - 'Authorization' => $this->config['key'], - 'Content-Type' => 'application/json', +class SparkPost +{ + public $transmission; + public $messageEvents; + + /** + * Library version, used for setting User-Agent. + */ + private $version = '1.1.0'; + + /** + * Connection config for making requests. + */ + private $config; + + /** + * @var \Ivory\HttpAdapter\HttpAdapterInterface to make requests through. + */ + public $httpAdapter; + + /** + * 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', ]; - return $defaultOptions; - } - - /** - * Helper function for getting the configuration for http requests - * @param array $config - * @return Configuration - */ - private function getHttpConfig($config) { - // 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/' . $this->version); - return $httpConfig; - } - - - /** - * Validates and sets up the httpAdapter - * @param $httpAdapter \Ivory\HttpAdapter\HttpAdapterInterface to make requests through. - * @throws \Exception - */ - public function setHttpAdapter(HttpAdapterInterface $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 String | array $settingsConfig - Hashmap that contains config values - * for the SDK to connect to SparkPost. If its a string we assume that - * its just they API Key. - * @throws \Exception - */ - public function setConfig($settingsConfig) { - // if the config map is a string we should assume that its an api key - if (is_string($settingsConfig)) { - $settingsConfig = ['key'=>$settingsConfig]; + /** + * Sets up httpAdapter and config. + * + * Sets up instances of sub libraries. + * + * @param \Ivory\HttpAdapter\HttpAdapterInterface $httpAdapter - An adapter for making http requests + * @param string | array $settingsConfig - Hashmap that contains config values + * for the SDK to connect to SparkPost. If its a string we assume that + * its just they API Key. + */ + public function __construct($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); + $this->messageEvents = new MessageEvents($this); } - // Validate API key because its required - if (!isset($settingsConfig['key']) || !preg_match('/\S/', $settingsConfig['key'])){ - throw new \Exception('You must provide an API key'); + /** + * Creates an unwrapped api interface for endpoints that aren't yet supported. + * The new resource is attached to this object as well as returned. + * + * @param string $endpoint + * + * @return APIResource - the unwrapped resource + */ + public function setupUnwrapped($endpoint) + { + $this->{$endpoint} = new APIResource($this); + $this->{$endpoint}->endpoint = $endpoint; + + return $this->{$endpoint}; } - $this->config = self::$apiDefaults; + /** + * Merges passed in headers with default headers for http requests. + */ + public function getHttpHeaders() + { + $defaultOptions = [ + 'Authorization' => $this->config['key'], + 'Content-Type' => 'application/json', + ]; + + return $defaultOptions; + } - // set config, overriding defaults - foreach ($settingsConfig as $configOption => $configValue) { - if(key_exists($configOption, $this->config)) { - $this->config[$configOption] = $configValue; - } + /** + * Helper function for getting the configuration for http requests. + * + * @param array $config + * + * @return Configuration + */ + private function getHttpConfig($config) + { + // 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/'.$this->version); + + return $httpConfig; } - } -} -?> + /** + * Validates and sets up the httpAdapter. + * + * @param $httpAdapter \Ivory\HttpAdapter\HttpAdapterInterface to make requests through. + * + * @throws \Exception + */ + public function setHttpAdapter(HttpAdapterInterface $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 string | array $settingsConfig - Hashmap that contains config values + * for the SDK to connect to SparkPost. If its a string we assume that + * its just they API Key. + * + * @throws \Exception + */ + public function setConfig($settingsConfig) + { + // if the config map is a string we should assume that its an api key + if (is_string($settingsConfig)) { + $settingsConfig = ['key' => $settingsConfig]; + } + + // Validate API key because its required + if (!isset($settingsConfig['key']) || !preg_match('/\S/', $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; + } + } + } +} |