Skip to content

Commit

Permalink
make phpstan happy
Browse files Browse the repository at this point in the history
  • Loading branch information
lekoala committed Feb 5, 2024
1 parent 25c25ce commit 6231772
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 30 deletions.
24 changes: 24 additions & 0 deletions src/EmailUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,30 @@ public static function inline_styles($html, $css = '')
return $html;
}

/**
* @param array<string|int,string|null>|string|null|bool $email
* @return string|null
*/
public static function stringify($email)
{
if (!$email || is_bool($email)) {
return null;
}
if (is_array($email)) {
return $email[1] . ' <' . $email[0] . '>';
}
return $email;
}

/**
* @param array<string|int,string|null>|string|null|bool $email
* @return bool
*/
public static function validate($email)
{
return boolval(filter_var(self::stringify($email), FILTER_VALIDATE_EMAIL));
}

/**
* Convert an html email to a text email while keeping formatting and links
*
Expand Down
59 changes: 29 additions & 30 deletions src/SparkPostAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace LeKoala\SparkPost;

use \Exception;
use Exception;
use SilverStripe\Forms\Tab;
use SilverStripe\Forms\Form;
use SilverStripe\Forms\TabSet;
Expand Down Expand Up @@ -150,7 +150,7 @@ public function settings($request)

/**
* @param HTTPRequest $request
* @return HTTPResponse|null
* @return HTTPResponse|string
*/
public function send_test($request)
{
Expand All @@ -168,8 +168,7 @@ public function send_test($request)
$email->setTo($to);

$email->send();
var_dump('Email sent!');
return null;
return 'Email sent';
}

/**
Expand All @@ -192,8 +191,6 @@ public function getEditForm($id = null, $fields = null)
$id = $this->currentPageID();
}

$form = parent::getEditForm($id);

/** @var DataObject|null $record */
$record = $this->getRecord($id);

Expand Down Expand Up @@ -283,9 +280,10 @@ public function getEditForm($id = null, $fields = null)
$toolsHtml = '<h2>Tools</h2>';

// Show default from email
$defaultEmail = SparkPostHelper::resolveDefaultFromEmail();
$toolsHtml .= "<p>Default sending email: " . $defaultEmail . " (" . SparkPostHelper::resolveDefaultFromEmailType() . ")</p>";
if (!SparkPostHelper::isEmailDomainReady($defaultEmail)) {
$defaultEmail = SparkPostHelper::resolveDefaultFromEmail();
$defaultEmailDisplayed = EmailUtils::stringify($defaultEmail);
$toolsHtml .= "<p>Default sending email: " . $defaultEmailDisplayed . " (" . SparkPostHelper::resolveDefaultFromEmailType() . ")</p>";
if (!SparkPostHelper::isEmailDomainReady($defaultEmailDisplayed)) {
$toolsHtml .= '<p style="color:red">The default email is not ready to send emails</p>';
}

Expand Down Expand Up @@ -327,9 +325,6 @@ public function getEditForm($id = null, $fields = null)
$messagesTab->addExtraClass('ui-state-active');
}

$actions = new FieldList();


// Build replacement form
$form = Form::create(
$this,
Expand Down Expand Up @@ -396,8 +391,9 @@ public function getCacheEnabled()
protected function getCachedData($method, $params, $expireInSeconds = 60)
{
$enabled = $this->getCacheEnabled();
$cacheResult = false;
$cache = $this->getCache();
if ($enabled) {
$cache = $this->getCache();
$key = md5(serialize($params));
$cacheResult = $cache->get($key);
}
Expand Down Expand Up @@ -572,7 +568,7 @@ public function Messages()
$params = $this->getParams();

$messages = $this->getCachedData('searchEvents', $params, 60 * self::MESSAGE_CACHE_MINUTES);
if ($messages === false) {
if ($messages === false || !$messages) {
if ($this->lastException) {
return $this->lastException->getMessage();
}
Expand All @@ -592,19 +588,17 @@ public function Messages()
}

$list = new ArrayList();
if ($messages) {
foreach ($messages as $message) {
// If we have a transmission id but no subject, try to find the transmission details
if (isset($message['transmission_id']) && empty($message['subject']) && isset($transmissions[$message['transmission_id']])) {
$message = array_merge($transmissions[$message['transmission_id']], $message);
}
// In some case (errors, etc) we don't have a friendly from
if (empty($message['friendly_from']) && isset($message['msg_from'])) {
$message['friendly_from'] = $message['msg_from'];
}
$m = new ArrayData($message);
$list->push($m);
foreach ($messages as $message) {
// If we have a transmission id but no subject, try to find the transmission details
if (isset($message['transmission_id']) && empty($message['subject']) && isset($transmissions[$message['transmission_id']])) {
$message = array_merge($transmissions[$message['transmission_id']], $message);
}
// In some case (errors, etc) we don't have a friendly from
if (empty($message['friendly_from']) && isset($message['msg_from'])) {
$message['friendly_from'] = $message['msg_from'];
}
$m = new ArrayData($message);
$list->push($m);
}

return $list;
Expand Down Expand Up @@ -787,7 +781,11 @@ public function WebhookUrl()
}
}
$protocol = Director::protocol();
return $protocol . $this->getDomain() . '/sparkpost/incoming';
$domain = $this->getDomain();
if (!$domain) {
throw new Exception("No domain for webhook");
}
return $protocol . $domain . '/sparkpost/incoming';
}

/**
Expand Down Expand Up @@ -939,7 +937,7 @@ public function VerifySendingDomain()
$client = SparkPostHelper::getClient();

$host = $this->getDomain();
if (!$host && is_bool($host)) {
if (!$host || is_bool($host)) {
return false;
}

Expand Down Expand Up @@ -1044,8 +1042,9 @@ public function getDomainFromHost()
public function getDomainFromEmail()
{
$email = SparkPostHelper::resolveDefaultFromEmail(null, false);
if ($email) {
$domain = substr((string)strrchr($email, "@"), 1);
if ($email && is_string($email)) {
$emailat = (string)strrchr($email, "@");
$domain = substr($emailat, 1);
if (!$domain) {
return false;
}
Expand Down

0 comments on commit 6231772

Please sign in to comment.