Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #155 from caseyw/AdjustFormattingToMakeProjectCons…

…istent

Updating code to make a bit more consistent
  • Loading branch information...
commit f1dafb9c2352c1895cfc5914cc6b37276a85f77a 2 parents 12d2485 + 0766d2e
@eddiezane eddiezane authored
View
19 lib/SendGrid.php
@@ -5,8 +5,8 @@ class SendGrid
const VERSION = '3.2.0';
protected
- $namespace = 'SendGrid',
- $headers = array('Content-Type' => 'application/json'),
+ $namespace = 'SendGrid',
+ $headers = array('Content-Type' => 'application/json'),
$client,
$options;
@@ -15,7 +15,7 @@ class SendGrid
$apiKey,
$url,
$endpoint,
- $version = self::VERSION;
+ $version = self::VERSION;
public function __construct($apiUserOrKey, $apiKeyOrOptions = null, $options = array())
{
@@ -23,11 +23,11 @@ public function __construct($apiUserOrKey, $apiKeyOrOptions = null, $options = a
if (is_string($apiKeyOrOptions)) {
// Username and password
$this->apiUser = $apiUserOrKey;
- $this->apiKey = $apiKeyOrOptions;
+ $this->apiKey = $apiKeyOrOptions;
$this->options = $options;
} elseif (is_array($apiKeyOrOptions) || $apiKeyOrOptions === null) {
// API key
- $this->apiKey = $apiUserOrKey;
+ $this->apiKey = $apiUserOrKey;
$this->apiUser = null;
// With options
@@ -55,6 +55,7 @@ public function __construct($apiUserOrKey, $apiKeyOrOptions = null, $options = a
/**
* Prepares the HTTP client
+ *
* @return \Guzzle\Http\Client
*/
private function prepareHttpClient()
@@ -92,18 +93,20 @@ public function getOptions()
/**
* Makes a post request to SendGrid to send an email
+ *
* @param SendGrid\Email $email Email object built
+ *
* @throws SendGrid\Exception if the response code is not 200
* @return stdClass SendGrid response object
*/
public function send(SendGrid\Email $email)
{
- $form = $email->toWebFormat();
+ $form = $email->toWebFormat();
// Using username password
if ($this->apiUser !== null) {
$form['api_user'] = $this->apiUser;
- $form['api_key'] = $this->apiKey;
+ $form['api_key'] = $this->apiKey;
}
$response = $this->postRequest($this->endpoint, $form);
@@ -117,8 +120,10 @@ public function send(SendGrid\Email $email)
/**
* Makes the actual HTTP request to SendGrid
+ *
* @param $endpoint string endpoint to post to
* @param $form array web ready version of SendGrid\Email
+ *
* @return SendGrid\Response
*/
public function postRequest($endpoint, $form)
View
140 lib/SendGrid/Email.php
@@ -25,23 +25,24 @@ class Email
public function __construct()
{
- $this->fromName = false;
- $this->replyTo = false;
- $this->smtpapi = new \Smtpapi\Header();
+ $this->fromName = false;
+ $this->replyTo = false;
+ $this->smtpapi = new \Smtpapi\Header();
}
/**
* _removeFromList
* Given a list of key/value pairs, removes the associated keys
* where a value matches the given string ($item)
+ *
* @param Array $list - the list of key/value pairs
* @param String $item - the value to be removed
*/
private function _removeFromList(&$list, $item, $key_field = null)
{
foreach ($list as $key => $val) {
- if($key_field) {
- if($val[$key_field] == $item) {
+ if ($key_field) {
+ if ($val[$key_field] == $item) {
unset($list[$key]);
}
} else {
@@ -82,18 +83,21 @@ public function addTo($email, $name = null)
public function addSmtpapiTo($email, $name = null)
{
$this->smtpapi->addTo($email, $name);
+
return $this;
}
public function setTos(array $emails)
{
$this->to = $emails;
+
return $this;
}
public function setSmtpapiTos(array $emails)
{
$this->smtpapi->setTos($emails);
+
return $this;
}
@@ -104,6 +108,7 @@ public function addToName($name)
}
$this->toName[] = $name;
+
return $this;
}
@@ -115,12 +120,13 @@ public function getToNames()
public function setFrom($email)
{
$this->from = $email;
+
return $this;
}
public function getFrom($as_array = false)
{
- if($as_array && ($name = $this->getFromName())) {
+ if ($as_array && ($name = $this->getFromName())) {
return array("$this->from" => $name);
} else {
return $this->from;
@@ -130,6 +136,7 @@ public function getFrom($as_array = false)
public function setFromName($name)
{
$this->fromName = $name;
+
return $this;
}
@@ -141,6 +148,7 @@ public function getFromName()
public function setReplyTo($email)
{
$this->replyTo = $email;
+
return $this;
}
@@ -152,12 +160,14 @@ public function getReplyTo()
public function setCc($email)
{
$this->cc = array($email);
+
return $this;
}
public function setCcs(array $email_list)
{
$this->cc = $email_list;
+
return $this;
}
@@ -193,6 +203,7 @@ public function addCcName($name)
}
$this->ccName[] = $name;
+
return $this;
}
@@ -216,12 +227,14 @@ public function getCcNames()
public function setBcc($email)
{
$this->bcc = array($email);
+
return $this;
}
public function setBccs($email_list)
{
$this->bcc = $email_list;
+
return $this;
}
@@ -257,6 +270,7 @@ public function addBccName($name)
}
$this->bccName[] = $name;
+
return $this;
}
@@ -268,6 +282,7 @@ public function getBccNames()
public function removeBcc($email)
{
$this->_removeFromList($this->bcc, $email);
+
return $this;
}
@@ -279,6 +294,7 @@ public function getBccs()
public function setSubject($subject)
{
$this->subject = $subject;
+
return $this;
}
@@ -290,6 +306,7 @@ public function getSubject()
public function setDate($date)
{
$this->date = $date;
+
return $this;
}
@@ -301,6 +318,7 @@ public function getDate()
public function setText($text)
{
$this->text = $text;
+
return $this;
}
@@ -312,6 +330,7 @@ public function getText()
public function setHtml($html)
{
$this->html = $html;
+
return $this;
}
@@ -323,40 +342,49 @@ public function getHtml()
public function setSendAt($timestamp)
{
$this->smtpapi->setSendAt($timestamp);
+
return $this;
}
public function setSendEachAt(array $timestamps)
{
$this->smtpapi->setSendEachAt($timestamps);
+
return $this;
}
public function addSendEachAt($timestamp)
{
$this->smtpapi->addSendEachAt($timestamp);
+
return $this;
}
/**
* Convenience method to add template
+ *
* @param string The id of the template
+ *
* @return $this
*/
public function setTemplateId($templateId)
{
$this->addFilter('templates', 'enabled', 1);
$this->addFilter('templates', 'template_id', $templateId);
+
return $this;
}
/** Convenience method to set asm group id
+ *
* @param string the group id
+ *
* @return $this
*/
public function setAsmGroupId($groupId)
{
$this->smtpapi->setASMGroupID($groupId);
+
return $this;
}
@@ -364,7 +392,7 @@ public function setAttachments(array $files)
{
$this->attachments = array();
- foreach($files as $filename => $file) {
+ foreach ($files as $filename => $file) {
if (is_string($filename)) {
$this->addAttachment($file, $filename);
} else {
@@ -378,12 +406,14 @@ public function setAttachments(array $files)
public function setAttachment($file, $custom_filename = null, $cid = null)
{
$this->attachments = array($this->getAttachmentInfo($file, $custom_filename, $cid));
+
return $this;
}
public function addAttachment($file, $custom_filename = null, $cid = null)
{
$this->attachments[] = $this->getAttachmentInfo($file, $custom_filename, $cid);
+
return $this;
}
@@ -395,6 +425,7 @@ public function getAttachments()
public function removeAttachment($file)
{
$this->_removeFromList($this->attachments, $file, "file");
+
return $this;
}
@@ -403,7 +434,7 @@ private function getAttachmentInfo($file, $custom_filename = null, $cid = null)
$info = pathinfo($file);
$info['file'] = $file;
if (!is_null($custom_filename)) {
- $info['custom_filename'] = $custom_filename;
+ $info['custom_filename'] = $custom_filename;
}
if ($cid !== null) {
$info['cid'] = $cid;
@@ -415,54 +446,63 @@ private function getAttachmentInfo($file, $custom_filename = null, $cid = null)
public function setCategories($categories)
{
$this->smtpapi->setCategories($categories);
+
return $this;
}
public function setCategory($category)
{
$this->smtpapi->setCategory($category);
+
return $this;
}
public function addCategory($category)
{
$this->smtpapi->addCategory($category);
+
return $this;
}
public function removeCategory($category)
{
$this->smtpapi->removeCategory($category);
+
return $this;
}
public function setSubstitutions($key_value_pairs)
{
$this->smtpapi->setSubstitutions($key_value_pairs);
+
return $this;
}
public function addSubstitution($from_value, array $to_values)
{
$this->smtpapi->addSubstitution($from_value, $to_values);
+
return $this;
}
public function setSections(array $key_value_pairs)
{
$this->smtpapi->setSections($key_value_pairs);
+
return $this;
}
public function addSection($from_value, $to_value)
{
$this->smtpapi->addSection($from_value, $to_value);
+
return $this;
}
public function setUniqueArgs(array $key_value_pairs)
{
$this->smtpapi->setUniqueArgs($key_value_pairs);
+
return $this;
}
@@ -470,12 +510,14 @@ public function setUniqueArgs(array $key_value_pairs)
public function setUniqueArguments(array $key_value_pairs)
{
$this->smtpapi->setUniqueArgs($key_value_pairs);
+
return $this;
}
public function addUniqueArg($key, $value)
{
$this->smtpapi->addUniqueArg($key, $value);
+
return $this;
}
@@ -483,12 +525,14 @@ public function addUniqueArg($key, $value)
public function addUniqueArgument($key, $value)
{
$this->smtpapi->addUniqueArg($key, $value);
+
return $this;
}
public function setFilters($filter_settings)
{
$this->smtpapi->setFilters($filter_settings);
+
return $this;
}
@@ -496,12 +540,14 @@ public function setFilters($filter_settings)
public function setFilterSettings($filter_settings)
{
$this->smtpapi->setFilters($filter_settings);
+
return $this;
}
public function addFilter($filter_name, $parameter_name, $parameter_value)
{
$this->smtpapi->addFilter($filter_name, $parameter_name, $parameter_value);
+
return $this;
}
@@ -509,6 +555,7 @@ public function addFilter($filter_name, $parameter_name, $parameter_value)
public function addFilterSetting($filter_name, $parameter_name, $parameter_value)
{
$this->smtpapi->addFilter($filter_name, $parameter_name, $parameter_value);
+
return $this;
}
@@ -522,24 +569,28 @@ public function getHeadersJson()
if (count($this->getHeaders()) <= 0) {
return "{}";
}
+
return json_encode($this->getHeaders(), JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP);
}
public function setHeaders($key_value_pairs)
{
$this->headers = $key_value_pairs;
+
return $this;
}
public function addHeader($key, $value)
{
$this->headers[$key] = $value;
+
return $this;
}
public function removeHeader($key)
{
unset($this->headers[$key]);
+
return $this;
}
@@ -551,49 +602,67 @@ public function getSmtpapi()
public function toWebFormat()
{
$web = array(
- 'to' => $this->to,
- 'from' => $this->getFrom(),
- 'x-smtpapi' => $this->smtpapi->jsonString(),
- 'subject' => $this->getSubject(),
- 'text' => $this->getText(),
- 'html' => $this->getHtml(),
- 'headers' => $this->getHeadersJson(),
+ 'to' => $this->to,
+ 'from' => $this->getFrom(),
+ 'x-smtpapi' => $this->smtpapi->jsonString(),
+ 'subject' => $this->getSubject(),
+ 'text' => $this->getText(),
+ 'html' => $this->getHtml(),
+ 'headers' => $this->getHeadersJson(),
);
- if ($this->getToNames()) { $web['toname'] = $this->getToNames(); }
- if ($this->getCcs()) { $web['cc'] = $this->getCcs(); }
- if ($this->getCcNames()) { $web['ccname'] = $this->getCcNames(); }
- if ($this->getBccs()) { $web['bcc'] = $this->getBccs(); }
- if ($this->getBccNames()) { $web['bccname'] = $this->getBccNames(); }
- if ($this->getFromName()) { $web['fromname'] = $this->getFromName(); }
- if ($this->getReplyTo()) { $web['replyto'] = $this->getReplyTo(); }
- if ($this->getDate()) { $web['date'] = $this->getDate(); }
- if ($this->smtpapi->to && (count($this->smtpapi->to) > 0)) { $web['to'] = ""; }
+ if ($this->getToNames()) {
+ $web['toname'] = $this->getToNames();
+ }
+ if ($this->getCcs()) {
+ $web['cc'] = $this->getCcs();
+ }
+ if ($this->getCcNames()) {
+ $web['ccname'] = $this->getCcNames();
+ }
+ if ($this->getBccs()) {
+ $web['bcc'] = $this->getBccs();
+ }
+ if ($this->getBccNames()) {
+ $web['bccname'] = $this->getBccNames();
+ }
+ if ($this->getFromName()) {
+ $web['fromname'] = $this->getFromName();
+ }
+ if ($this->getReplyTo()) {
+ $web['replyto'] = $this->getReplyTo();
+ }
+ if ($this->getDate()) {
+ $web['date'] = $this->getDate();
+ }
+ if ($this->smtpapi->to && (count($this->smtpapi->to) > 0)) {
+ $web['to'] = "";
+ }
$web = $this->updateMissingTo($web);
if ($this->getAttachments()) {
- foreach($this->getAttachments() as $f) {
- $file = $f['file'];
- $extension = null;
+ foreach ($this->getAttachments() as $f) {
+ $file = $f['file'];
+ $extension = null;
if (array_key_exists('extension', $f)) {
- $extension = $f['extension'];
+ $extension = $f['extension'];
};
- $filename = $f['filename'];
- $full_filename = $filename;
+ $filename = $f['filename'];
+ $full_filename = $filename;
if (isset($extension)) {
- $full_filename = $filename.'.'.$extension;
+ $full_filename = $filename . '.' . $extension;
}
if (array_key_exists('custom_filename', $f)) {
- $full_filename = $f['custom_filename'];
+ $full_filename = $f['custom_filename'];
}
if (array_key_exists('cid', $f)) {
- $web['content['.$full_filename.']'] = $f['cid'];
+ $web['content[' . $full_filename . ']'] = $f['cid'];
}
- $contents = '@' . $file;
+ $contents = '@' . $file;
// Guzzle handles this for us.
// http://guzzle3.readthedocs.org/en/latest/http-client/request.html#post-requests
@@ -601,7 +670,7 @@ public function toWebFormat()
// $contents = new \CurlFile($file, $extension, $filename);
// }
- $web['files['.$full_filename.']'] = $contents;
+ $web['files[' . $full_filename . ']'] = $contents;
};
}
@@ -617,6 +686,7 @@ public function updateMissingTo($data)
if ($this->smtpapi->to && (count($this->smtpapi->to) > 0)) {
$data['to'] = $this->getFrom();
}
+
return $data;
}
}
View
8 lib/SendGrid/Exception.php
@@ -7,8 +7,8 @@
*/
class Exception extends \Exception
{
- public function getErrors()
- {
- return json_decode($this->message)->errors;
- }
+ public function getErrors()
+ {
+ return json_decode($this->message)->errors;
+ }
}
View
7 lib/SendGrid/Response.php
@@ -4,7 +4,6 @@
class Response
{
-
public
$code,
$headers,
@@ -13,10 +12,10 @@ class Response
public function __construct($code, $headers, $raw_body, $body)
{
- $this->code = $code;
- $this->headers = $headers;
+ $this->code = $code;
+ $this->headers = $headers;
$this->raw_body = $raw_body;
- $this->body = $body;
+ $this->body = $body;
}
public function getCode()
Please sign in to comment.
Something went wrong with that request. Please try again.