Skip to content

Commit

Permalink
[rector] Automated updates generated by rector configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesDPC authored and github-actions[bot] committed Aug 7, 2024
1 parent e8cdf01 commit 61afe8b
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 21 deletions.
16 changes: 7 additions & 9 deletions src/Controllers/ChimpleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,19 +108,15 @@ public function setFormNameSuffix(string $suffix = ''): self
*/
public function getFormNameSuffix(): string
{
$suffix = $this->formNameSuffix ? '_' . $this->formNameSuffix : "";

return $suffix;
return $this->formNameSuffix ? '_' . $this->formNameSuffix : "";
}

/**
* Get a subscription form based on parameters
*/
public function getSubscriptionForm($useXhr = false): ?SubscribeForm
{
$form = $useXhr ? $this->XhrSubscribeForm() : $this->SubscribeForm();

return $form;
return $useXhr ? $this->XhrSubscribeForm() : $this->SubscribeForm();
}

/**
Expand Down Expand Up @@ -254,7 +250,7 @@ protected function getValidator(): ?Validator
*/
protected function getCallbackForXhrValidation(): callable
{
return function (ValidationResult $result) {
return function (ValidationResult $result): \SilverStripe\Control\HTTPResponse {
// Fail, using the first message returned from the validation result
$messages = $result->getMessages();
$message = (empty($messages[0]['message']) ? '' : $messages[0]['message']);
Expand Down Expand Up @@ -283,21 +279,22 @@ protected function getCallbackForValidation(SubscribeForm $form): callable
/**
* Handle errors, based on the request type
*/
private function handleError($code, $error_message, Form $form = null)
private function handleError($code, $error_message, Form $form = null): ?\SilverStripe\Control\HTTPResponse
{
if($this->request->isAjax()) {
return $this->xhrError($code, $error_message);
} elseif($form instanceof \SilverStripe\Forms\Form) {
// set session error on the form
$form->sessionError($error_message, ValidationResult::TYPE_ERROR);
}

return null;
}

/**
* Handle successful submissions, based on the request type
*/
private function handleSuccess(int $code, Form $form = null)
private function handleSuccess(int $code, Form $form = null): ?\SilverStripe\Control\HTTPResponse
{
$success_message = Config::inst()->get(MailchimpConfig::class, 'success_message');
if($this->request->isAjax()) {
Expand All @@ -306,6 +303,7 @@ private function handleSuccess(int $code, Form $form = null)
// set session message on the form
$form->sessionMessage($success_message, ValidationResult::TYPE_GOOD);
}

return null;
}

Expand Down
4 changes: 3 additions & 1 deletion src/Extensions/SiteConfigExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class SiteConfigExtension extends DataExtension
'MailchimpConfig' => MailchimpConfig::class // global element for configuration
];

#[\Override]
public function updateCmsFields(FieldList $fields): void
{
$fields->addFieldsToTab(
Expand All @@ -37,10 +38,11 @@ public function updateCmsFields(FieldList $fields): void
);
}

#[\Override]
public function onAfterWrite(): void
{
parent::onAfterWrite();
if ($this->owner->MailchimpConfigID && ($config = MailchimpConfig::get()->byId($this->owner->MailchimpConfigID))) {
if ($this->getOwner()->MailchimpConfigID && ($config = MailchimpConfig::get()->byId($this->getOwner()->MailchimpConfigID))) {
$config->IsGlobal = 1;
$config->write();
}
Expand Down
2 changes: 2 additions & 0 deletions src/Jobs/MailchimpCleanupJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ private function processSubscriptions(): bool

$this->addMessage(sprintf('Deleted %d subscribers with status ', $success_deletes) . MailchimpSubscriber::CHIMPLE_STATUS_SUCCESS);
}

$this->currentStep = $success_deletes;
$this->totalSteps = $success_deletes;

Expand All @@ -116,6 +117,7 @@ private function processSubscriptions(): bool

$this->addMessage(sprintf('Deleted %d subscribers with status ', $failed_deletes) . MailchimpSubscriber::CHIMPLE_STATUS_FAIL);
}

$this->currentStep = $success_deletes + $failed_deletes;
$this->totalSteps = $success_deletes + $failed_deletes;

Expand Down
4 changes: 2 additions & 2 deletions src/Jobs/MailchimpSubscribeJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function setup(): void
$this->totalSteps = 1;
}

private function getTotalResults($results): int|float
private function getTotalResults(array|bool $results): int|float
{
$total = 0;
foreach ($results as $count) {
Expand All @@ -57,7 +57,7 @@ private function getTotalResults($results): int|float
return $total;
}

private function getTotalNonFailedResults(array $results)
private function getTotalNonFailedResults(array $results): int|float
{
$copy = $results;
unset($copy[ MailchimpSubscriber::CHIMPLE_STATUS_FAIL]);
Expand Down
29 changes: 21 additions & 8 deletions src/Models/MailchimpConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,19 @@
class MailchimpConfig extends DataObject implements TemplateGlobalProvider, PermissionProvider
{
public $Title;

public $Code;

public $IsGlobal;

public $ID;

public $UseXHR;

public $Heading;

private static string $list_id = "";

// default list (audience) ID
private static string $api_key = "";// API key provided by Mailchimp

Expand Down Expand Up @@ -105,12 +112,12 @@ class MailchimpConfig extends DataObject implements TemplateGlobalProvider, Perm
'UseXHR' => 1
];

public function TitleCode()
public function TitleCode(): string
{
return sprintf('%s (%s)', $this->Title, $this->Code);
}

public static function isEnabled()
public static function isEnabled(): bool
{
$site_config = SiteConfig::current_site_config();
return $site_config->MailchimpEnabled == 1;
Expand Down Expand Up @@ -140,18 +147,19 @@ public static function getDataCentre(): string
return empty($parts[1]) ? '' : $parts[1];
}

public function TitleWithCode()
public function TitleWithCode(): string
{
return $this->Title . sprintf(' - (code %s)', $this->Code);
}

public function TitleWithDetails()
public function TitleWithDetails(): string
{
$title = $this->Title;
$list_id = $this->getMailchimpListId();
return $title . sprintf(' (list %s)', $list_id);
}

#[\Override]
public function onBeforeWrite(): void
{
if (!$this->Code) {
Expand All @@ -163,7 +171,7 @@ public function onBeforeWrite(): void
if($this->IsGlobal == 1) {
// Ensure only this config is marked global
DB::query(
'UPDATE `ChimpleConfig` SET IsGlobal = 0 WHERE IsGlobal = 1 AND ID <> \'' . Convert::raw2sql($this->ID) . "'"
"UPDATE `ChimpleConfig` SET IsGlobal = 0 WHERE IsGlobal = 1 AND ID <> '" . Convert::raw2sql($this->ID) . "'"
);
}
}
Expand All @@ -189,7 +197,7 @@ public function getMailchimpListId()
return $list_id;
}

public function HasMailchimpListId()
public function HasMailchimpListId(): bool
{
return $this->getMailchimpListId() != '';
}
Expand All @@ -214,6 +222,7 @@ public static function getConfig($id = '', $list_id = '', $code = '')
/**
* @inheritdoc
*/
#[\Override]
public function getCMSFields()
{
$fields = null;
Expand Down Expand Up @@ -351,6 +360,7 @@ public function MailchimpLink()
/**
* Ensure the subscription for the global footer is added
*/
#[\Override]
public function requireDefaultRecords(): void
{
$config = MailchimpConfig::get()->filter(['IsGlobal' => 1])->first();
Expand Down Expand Up @@ -420,9 +430,8 @@ public function SubscribeForm($force_xhr = null)

/**
* Return alerts for the form
* @return string
*/
public function Alerts()
public function Alerts(): string
{
return '<div class="hidden alert alert-success" data-type="success">'
. _t(self::class . '.SUBSCRIBE_SUCCESS', htmlspecialchars((string) $this->config()->get('success_message')))
Expand All @@ -433,21 +442,25 @@ public function Alerts()
. '<div class="hidden alert alert-info" data-type="info"></div>';// info added by JS
}

#[\Override]
public function canView($member = null)
{
return Permission::checkMember($member, 'MAILCHIMP_CONFIG_VIEW');
}

#[\Override]
public function canCreate($member = null, $context = [])
{
return Permission::checkMember($member, 'MAILCHIMP_CONFIG_CREATE');
}

#[\Override]
public function canEdit($member = null)
{
return Permission::checkMember($member, 'MAILCHIMP_CONFIG_EDIT');
}

#[\Override]
public function canDelete($member = null)
{
return Permission::checkMember($member, 'MAILCHIMP_CONFIG_DELETE');
Expand Down
5 changes: 4 additions & 1 deletion src/Models/MailchimpSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,7 @@ public function getSurnameFromName(): ?string
if (isset($parts[1]) && ($parts[1] !== '' && $parts[1] !== '0')) {
return $parts[1];
}

return null;
}

Expand Down Expand Up @@ -514,6 +515,7 @@ public function getSubscribeRecord(): array
if(!$email_type || $email_type != self::MAILCHIMP_EMAIL_TYPE_HTML || $email_type != self::MAILCHIMP_EMAIL_TYPE_TEXT) {
$email_type = self::MAILCHIMP_EMAIL_TYPE_HTML;
}

return [
'email_address' => $this->Email,
'email_type' => $email_type,
Expand Down Expand Up @@ -555,6 +557,7 @@ private function obfuscate(): void
if($sub_length <= 0) {
return str_repeat($chr, $length);
}

return substr_replace($in, str_repeat($chr, $sub_length), 1, $sub_length);
};
$this->Email = $obfuscate($this->Email);
Expand Down Expand Up @@ -642,7 +645,7 @@ public function subscribe(): bool

$result = false;

if(!$existing) {
if($existing === [] || $existing === false) {
$operation_path = sprintf('lists/%s/members', $list_id);
$params = $this->getSubscribeRecord();
// add tags
Expand Down

0 comments on commit 61afe8b

Please sign in to comment.