Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Optional Personalization arguments handling #971

Merged
merged 9 commits into from
Jun 15, 2020
96 changes: 81 additions & 15 deletions lib/mail/Mail.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

namespace SendGrid\Mail;

use InvalidArgumentException;

/**
* This class is used to construct a request body for the /mail/send API call
*
Expand Down Expand Up @@ -167,6 +169,8 @@ public function __construct(
* Personalization objects
* @param Personalization|null $personalization A pre-created
* Personalization object
*
* @throws TypeException
*/
private function addRecipientEmail(
$emailType,
Expand Down Expand Up @@ -205,6 +209,8 @@ private function addRecipientEmail(
* Personalization objects
* @param Personalization|null $personalization A pre-created
* Personalization object
*
* @throws TypeException
*/
private function addRecipientEmails(
$emailType,
Expand All @@ -219,6 +225,7 @@ private function addRecipientEmails(
$this->$emailFunctionCall(
$email,
$name = null,
$substitutions = null,
$personalizationIndex,
$personalization
);
Expand All @@ -228,6 +235,7 @@ private function addRecipientEmails(
$this->$emailFunctionCall(
$email,
$name,
$substitutions = null,
$personalizationIndex,
$personalization
);
Expand All @@ -239,9 +247,17 @@ private function addRecipientEmails(
* Add a Personalization object to the Mail object
*
* @param Personalization $personalization A Personalization object
*
* @throws TypeException
*/
public function addPersonalization($personalization)
{
if (!($personalization instanceof Personalization)) {
throw new TypeException(
'$personalization must be an instance of SendGrid\Mail\Personalization.'
);
}

$this->personalization[] = $personalization;
}

Expand All @@ -253,34 +269,72 @@ public function addPersonalization($personalization)
* Personalization objects
* @param Personalization|null $personalization A pre-created
* Personalization object
*
* @return Personalization
*
* @throws TypeException
*/
public function getPersonalization($personalizationIndex = null, $personalization = null)
{
if ($personalization !== null) {
/**
* Approach:
* - Append if provided personalization + return
* - Return last added if not provided personalizationIndex (create on empty)
* - Return existing personalizationIndex
* - InvalidArgumentException on unexpected personalizationIndex ( > count)
* - Create + add Personalization and return
*/

// If given a Personalization instance
if (null !== $personalization) {
// Just append it onto Mail and return it
$this->addPersonalization($personalization);
} else if ($personalizationIndex !== null) {
if ($personalizationIndex >= $this->getPersonalizationCount()) {
throw new \InvalidArgumentException(
'personalizationIndex ' . $personalizationIndex .
' must be less than ' . $this->getPersonalizationCount());
return $personalization;
}

// Retrieve count of existing Personalization instances
$personalizationCount = $this->getPersonalizationCount();

// Not providing a personalizationIndex?
if (null === $personalizationIndex) {
// Create new Personalization instance depending on current count
if (0 === $personalizationCount) {
$this->addPersonalization(new Personalization());
}

$personalization = $this->personalization[$personalizationIndex];
} else if ($this->getPersonalizationCount() === 0) {
$personalization = new Personalization();
$this->addPersonalization($personalization);
} else {
$personalization = \end($this->personalization);
// Return last added Personalization instance
return end($this->personalization);
}

// Existing personalizationIndex in personalization?
if (isset($this->personalization[$personalizationIndex])) {
childish-sambino marked this conversation as resolved.
Show resolved Hide resolved
// Return referred personalization
return $this->personalization[$personalizationIndex];
}

// Non-existent personalizationIndex given
// Only allow creation of next Personalization if given
// personalizationIndex equals personalizationCount
if (
($personalizationIndex < 0) ||
($personalizationIndex > $personalizationCount)
) {
throw new InvalidArgumentException(
'personalizationIndex ' . $personalizationIndex .
' must be less than ' . $personalizationCount
);
}

// Create new Personalization and return it
$personalization = new Personalization();
$this->addPersonalization($personalization);
return $personalization;
}

/**
* Retrieve a Personalization object from the Mail object
* Retrieves Personalization object collection from the Mail object.
*
* @return Personalization[]
* @return Personalization[]|null
*/
public function getPersonalizations()
{
Expand All @@ -307,6 +361,8 @@ public function getPersonalizationCount()
* Personalization objects
* @param Personalization|null $personalization A pre-created
* Personalization object
*
* @throws TypeException
*/
public function addTo(
$to,
Expand Down Expand Up @@ -339,6 +395,8 @@ public function addTo(
* Personalization objects
* @param Personalization|null $personalization A pre-created
* Personalization object
*
* @throws TypeException
*/
public function addTos(
$toEmails,
Expand All @@ -364,6 +422,8 @@ public function addTos(
* Personalization objects
* @param Personalization|null $personalization A pre-created
* Personalization object
*
* @throws TypeException
*/
public function addCc(
$cc,
Expand Down Expand Up @@ -395,6 +455,8 @@ public function addCc(
* Personalization objects
* @param Personalization|null $personalization A pre-created
* Personalization object
*
* @throws TypeException
*/
public function addCcs(
$ccEmails,
Expand All @@ -420,6 +482,8 @@ public function addCcs(
* Personalization objects
* @param Personalization|null $personalization A pre-created
* Personalization object
*
* @throws TypeException
*/
public function addBcc(
$bcc,
Expand Down Expand Up @@ -451,6 +515,8 @@ public function addBcc(
* Personalization objects
* @param Personalization|null $personalization A pre-created
* Personalization object
*
* @throws TypeException
*/
public function addBccs(
$bccEmails,
Expand Down Expand Up @@ -845,7 +911,7 @@ public function setSendAt(
*
* @param int|0 $personalizationIndex Index into the array of existing
* Personalization objects
* @return SendAt
* @return SendAt|null
*/
public function getSendAt($personalizationIndex = 0)
{
Expand Down
36 changes: 23 additions & 13 deletions lib/mail/Personalization.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ class Personalization implements \JsonSerializable
private $subject;
/** @var $headers Header[] array of header key values */
private $headers;
/** @var $substitutions Substitution[] array of substitution key values, used for legacy templates */
/** @var $substitutions array array of substitution key values, used for legacy templates */
private $substitutions;
/** @var array of dynamic template data key values */
private $dynamic_template_data;
/** @var bool if we are using dynamic templates this will be true */
private $has_dynamic_template = false;
/** @var $custom_args CustomArg[] array of custom arg key values */
/** @var $custom_args array array of custom arg key values */
private $custom_args;
/** @var $send_at SendAt object */
private $send_at;
Expand Down Expand Up @@ -141,7 +141,7 @@ public function addHeader($header)
/**
* Retrieve header key/value pairs from a Personalization object
*
* @return array
* @return array|null
*/
public function getHeaders()
{
Expand All @@ -153,9 +153,9 @@ public function getHeaders()
*
* @param Substitution|string $data DynamicTemplateData object or the key of a
* dynamic data
* @param string|null $value The value of dynmic data
* @param string|null $value The value of dynamic data
*
* @return null
* @throws TypeException
*/
public function addDynamicTemplateData($data, $value = null)
{
Expand All @@ -165,7 +165,7 @@ public function addDynamicTemplateData($data, $value = null)
/**
* Retrieve dynamic template data key/value pairs from a Personalization object
*
* @return array
* @return array|null
*/
public function getDynamicTemplateData()
{
Expand All @@ -178,6 +178,8 @@ public function getDynamicTemplateData()
* @param Substitution|string $substitution Substitution object or the key of a
* substitution
* @param string|null $value The value of a substitution
*
* @throws TypeException
*/
public function addSubstitution($substitution, $value = null)
{
Expand All @@ -191,7 +193,7 @@ public function addSubstitution($substitution, $value = null)
/**
* Retrieve substitution key/value pairs from a Personalization object
*
* @return array
* @return array|null
*/
public function getSubstitutions()
{
Expand All @@ -202,16 +204,24 @@ public function getSubstitutions()
* Add a CustomArg object to a Personalization object
*
* @param CustomArg $custom_arg CustomArg object
*
* @throws TypeException
*/
public function addCustomArg($custom_arg)
{
if (!($custom_arg instanceof CustomArg)) {
throw new TypeException(
'$custom_arg must be an instance of SendGrid\Mail\CustomArg'
);
}

$this->custom_args[$custom_arg->getKey()] = (string)$custom_arg->getValue();
}

/**
* Retrieve custom arg key/value pairs from a Personalization object
*
* @return array
* @return array|null
*/
public function getCustomArgs()
{
Expand All @@ -223,7 +233,7 @@ public function getCustomArgs()
*
* @param SendAt $send_at SendAt object
*
* @throws \SendGrid\Mail\TypeException
* @throws TypeException
*/
public function setSendAt($send_at)
{
Expand All @@ -238,7 +248,7 @@ public function setSendAt($send_at)
/**
* Retrieve a SendAt object from a Personalization object
*
* @return SendAt
* @return SendAt|null
*/
public function getSendAt()
{
Expand All @@ -250,13 +260,13 @@ public function getSendAt()
*
* @param bool $has_dynamic_template are we using dynamic templates
*
* @throws \SendGrid\Mail\TypeException
* @throws TypeException
*/
public function setHasDynamicTemplate($has_dynamic_template)
{
if (is_bool($has_dynamic_template) != true) {
throw new TypeException(
'$has_dynamic_template must be an instance of bool'
'$has_dynamic_template must be a boolean'
);
}
$this->has_dynamic_template = $has_dynamic_template;
Expand All @@ -279,7 +289,7 @@ public function getHasDynamicTemplate()
*/
public function jsonSerialize()
{
if ($this->getHasDynamicTemplate() == true) {
if ($this->getHasDynamicTemplate()) {
$dynamic_substitutions = $this->getSubstitutions();
$substitutions = null;
} else {
Expand Down
Loading