summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAvi Goldman <avrahamymgoldman@gmail.com>2016-06-09 07:56:50 -0400
committerAvi Goldman <avrahamymgoldman@gmail.com>2016-06-09 07:56:50 -0400
commit1e6984319e51bf700ee9c1c9ad3435a013d3ac7f (patch)
treeaea0e009abf5778d938acde8078474d7d23c5374
parentaac8412f0b6a12dd26b21e891a6f216e0a1c88e2 (diff)
downloadphp-sparkpost-1e6984319e51bf700ee9c1c9ad3435a013d3ac7f.zip
php-sparkpost-1e6984319e51bf700ee9c1c9ad3435a013d3ac7f.tar.gz
php-sparkpost-1e6984319e51bf700ee9c1c9ad3435a013d3ac7f.tar.bz2
FAD-3148 basic non-functioning base class
-rw-r--r--lib/SendGridCompatibility/Email.php259
-rw-r--r--lib/SendGridCompatibility/SendGrid.php26
-rw-r--r--lib/SparkPost/SparkPost.php145
3 files changed, 43 insertions, 387 deletions
diff --git a/lib/SendGridCompatibility/Email.php b/lib/SendGridCompatibility/Email.php
deleted file mode 100644
index 3ca2466..0000000
--- a/lib/SendGridCompatibility/Email.php
+++ /dev/null
@@ -1,259 +0,0 @@
-<?php
-
-namespace SparkPost\SendGridCompatibility;
-
-class Email
-{
- public $model;
-
- /**
- * Sets up the model for saving the configuration.
- */
- public function __construct()
- {
- $this->model = array();
- }
-
- /**
- * adds addresses as recipients.
- *
- * @param string $address
- * @param string $name optional
- *
- * @return $this
- */
- public function addTo($address, $name = null)
- {
- if (!isset($this->model['recipients'])) {
- $this->model['recipients'] = array();
- }
-
- if (isset($name)) {
- $address = array('address' => array('email' => $address, 'name' => $name));
- } else {
- $address = array('address' => array('email' => $address));
- }
-
- array_push($this->model['recipients'], $address);
-
- return $this;
- }
-
- /**
- * explicitly sets a list of addresses.
- *
- * @param array $addresses
- *
- * @return $this
- */
- public function setTos(array $addresses)
- {
- $this->model['recipients'] = $addresses;
-
- return $this;
- }
-
- /**
- * sets the from address.
- *
- * @param string $address
- *
- * @return $this
- */
- public function setFrom($address)
- {
- $this->model['from'] = array('email' => $address);
-
- return $this;
- }
-
- /**
- * Sets the name for the from address.
- *
- * @param string $name
- *
- * @return $this
- *
- * @throws \Exception
- */
- public function setFromName($name)
- {
- if (!isset($this->model['from'])) {
- throw new \Exception('Must set \'From\' prior to setting \'From Name\'.');
- }
- $this->model['from']['name'] = $name;
-
- return $this;
- }
-
- /**
- * sets the reply to field.
- *
- * @param string $address
- *
- * @return $this
- */
- public function setReplyTo($address)
- {
- $this->model['replyTo'] = $address;
-
- return $this;
- }
-
- /**
- * throws an error because bcc fields are not yet implemented.
- *
- * @throws \Exception
- *
- * @param string $address
- *
- * @return $this
- */
- public function addBcc($address)
- {
- throw new \Exception('Adding bcc recipients is not yet supported, try adding them as a \'to\' address');
- }
-
- /**
- * sets the subject header.
- *
- * @param string $subject
- *
- * @return $this
- */
- public function setSubject($subject)
- {
- $this->model['subject'] = $subject;
-
- return $this;
- }
-
- /**
- * sets the text body.
- *
- * @param string $text
- *
- * @return $this
- */
- public function setText($text)
- {
- $this->model['text'] = $text;
-
- return $this;
- }
-
- /**
- * sets the html body.
- *
- * @param string $html
- *
- * @return $this
- */
- public function setHtml($html)
- {
- $this->model['html'] = $html;
-
- return $this;
- }
-
- /**
- * Throws an exception since adding categories is not yet supported.
- *
- * @param string $category
- *
- * @throws \Exception
- */
- public function addCategory($category)
- {
- throw new \Exception('Adding categories is not yet supported');
- }
-
- /**
- * Throws an exception since adding attachments is not yet supported.
- *
- * @throws \Exception
- *
- * @param mixed $attachment
- */
- public function addAttachment($attachment)
- {
- throw new \Exception('Adding attachments is not yet supported');
- }
-
- /**
- * Adds transmission level substitution data.
- *
- * @param string $name
- * @param mixed $values
- *
- * @return $this
- */
- public function addSubstitution($name, $values)
- {
- if (!isset($this->model['substitutionData'])) {
- $this->model['substitutionData'] = array();
- }
- $this->model['substitutionData'][$name] = $values;
-
- return $this;
- }
-
- /**
- * Adds transmission level substitution data.
- *
- * @param string $name
- * @param mixed $values
- */
- public function addSection($name, $values)
- {
- $this->addSubstitution($name, $values);
- }
-
- /**
- * Throws an exception because arguments for third party systems is not supported.
- *
- * @throws \Exception
- *
- * @param mixed $value
- */
- public function addUniqueArg($key, $value)
- {
- throw new \Exception('Adding Unique Arguments is not yet supported');
- }
-
- /**
- * Throws an exception because arguments for third party systems is not supported.
- *
- * @throws \Exception
- *
- * @param mixed $values
- */
- public function setUniqueArgs(array $values)
- {
- throw new \Exception('Setting Unique Arguments is not yet supported');
- }
-
- /**
- * Adds custom headers to the email header.
- *
- * @param string $name
- * @param string $value
- */
- public function addHeader($name, $value)
- {
- if (!isset($this->model['customHeaders'])) {
- $this->model['customHeaders'] = array();
- }
- $this->model['customHeaders'][$name] = $value;
- }
-
- /**
- * converts this object to a configuration for a SparkPost transmission.
- *
- * @return array
- */
- public function toSparkPostTransmission()
- {
- return $this->model;
- }
-}
diff --git a/lib/SendGridCompatibility/SendGrid.php b/lib/SendGridCompatibility/SendGrid.php
deleted file mode 100644
index c5e84c1..0000000
--- a/lib/SendGridCompatibility/SendGrid.php
+++ /dev/null
@@ -1,26 +0,0 @@
-<?php
-
-namespace SparkPost\SendGridCompatibility;
-
-use SparkPost\SparkPost;
-
-class SendGrid
-{
- private $sparky;
-
- public function __construct($username, $password, $options = null, $httpAdapter)
- {
- //username isn't used in our system
- $opts = array('key' => $password);
- if (!is_null($options)) {
- $opts = array_merge($opts, $options);
- }
-
- $this->sparky = new SparkPost($httpAdapter, $opts);
- }
-
- public function send(Email $email)
- {
- $this->sparky->transmission->send($email->toSparkPostTransmission());
- }
-}
diff --git a/lib/SparkPost/SparkPost.php b/lib/SparkPost/SparkPost.php
index ff8b456..81577fe 100644
--- a/lib/SparkPost/SparkPost.php
+++ b/lib/SparkPost/SparkPost.php
@@ -2,33 +2,19 @@
namespace SparkPost;
-use Ivory\HttpAdapter\Configuration;
-use Ivory\HttpAdapter\HttpAdapterInterface;
+use GuzzleHttp\Psr7\Request as Request;
+use Http\Client\HttpClient;
class SparkPost
{
- public $transmission;
- public $messageEvents;
-
- /**
- * Library version, used for setting User-Agent.
- */
- private $version = '1.2.1';
-
- /**
- * Connection config for making requests.
- */
+ private $version = '2.0.0';
private $config;
- /**
- * @var \Ivory\HttpAdapter\HttpAdapterInterface to make requests through.
- */
- public $httpAdapter;
+ public $httpClient;
+
+ private $options;
- /**
- * Default config values. Passed in values will override these.
- */
- private static $apiDefaults = [
+ private static $defaultOptions = [
'host' => 'api.sparkpost.com',
'protocol' => 'https',
'port' => 443,
@@ -38,114 +24,69 @@ class SparkPost
'timeout' => 10
];
- /**
- * 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)
+ public function __construct($httpAdapter, $options)
{
- //config needs to be setup before adapter because of default adapter settings
- $this->setConfig($settingsConfig);
+ $this->setOptions($options);
$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)
+ public function request($method, $uri, $payload = [])
{
- $this->{$endpoint} = new APIResource($this);
- $this->{$endpoint}->endpoint = $endpoint;
+
+ $method = trim(strtoupper($method));
+
+ if ($method === 'GET') {
+ $params = $payload;
+ $body = null;
+ }
+ else {
+ $params = null;
+ $body = $payload;
+ }
+
+ $url = $this->getUrl($uri, $params);
+ $headers = $this->getHttpHeaders();
+
+ $request = new Request($method, $url, $headers, $body);
- return $this->{$endpoint};
+ return $httpClient->sendRequest($request);
}
- /**
- * Merges passed in headers with default headers for http requests.
- */
public function getHttpHeaders()
{
- $defaultOptions = [
- 'Authorization' => $this->config['key'],
+ return [
+ 'Authorization' => $this->options['key'],
'Content-Type' => 'application/json',
];
-
- 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->setTimeout($this->config['timeout']);
- $httpConfig->setUserAgent('php-sparkpost/'.$this->version);
-
- return $httpConfig;
+ public function getUrl($uri, $params) {
+ return '';
}
- /**
- * Validates and sets up the httpAdapter.
- *
- * @param $httpAdapter \Ivory\HttpAdapter\HttpAdapterInterface to make requests through.
- *
- * @throws \Exception
- */
- public function setHttpAdapter(HttpAdapterInterface $httpAdapter)
+ public function setHttpAdapter(HttpClient $httpClient)
{
- $this->httpAdapter = $httpAdapter;
- $this->httpAdapter->setConfiguration($this->getHttpConfig($this->config));
+ $this->httpClient = $httpClient;
}
- /**
- * 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)
+ public function setOptions($options)
{
- // if the config map is a string we should assume that its an api key
- if (is_string($settingsConfig)) {
- $settingsConfig = ['key' => $settingsConfig];
+ // if the options map is a string we should assume that its an api key
+ if (is_string($options)) {
+ $options = ['key' => $options];
}
// Validate API key because its required
- if (!isset($settingsConfig['key']) || !preg_match('/\S/', $settingsConfig['key'])) {
+ if (!isset($this->options['key']) && (!isset($options['key']) || !preg_match('/\S/', $options['key']))) {
throw new \Exception('You must provide an API key');
}
- $this->config = self::$apiDefaults;
+ $this->options = $this->options || self::$defaultOptions;
- // set config, overriding defaults
- foreach ($settingsConfig as $configOption => $configValue) {
- if (key_exists($configOption, $this->config)) {
- $this->config[$configOption] = $configValue;
+ // set options, overriding defaults
+ foreach ($options as $option => $value) {
+ if (key_exists($option, $this->options)) {
+ $this->options[$option] = $value;
}
}
}