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 ae53d2a commit a9aae10
Show file tree
Hide file tree
Showing 15 changed files with 330 additions and 302 deletions.
115 changes: 47 additions & 68 deletions src/Controllers/ChimpleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,11 @@
*/
class ChimpleController extends PageController
{
/**
* @var string
*/
private static $url_segment = 'mc-subscribe/v1';
private static string $url_segment = 'mc-subscribe/v1';

/**
* @var bool
*/
private static $hide_generic_form = true;
private static bool $hide_generic_form = true;

/**
* @var array
*/
private static $allowed_actions = [
private static array $allowed_actions = [
'SubscribeForm',
'XhrSubscribeForm'
];
Expand All @@ -66,14 +57,11 @@ public function index()

public function pageTitle($complete = null)
{
switch($complete) {
case 'y':
return _t(__CLASS__. '.DEFAULT_TITLE_SUCCESSFUL', 'Thanks for subscribing');
case 'n':
return _t(__CLASS__. '.DEFAULT_TITLE_NOT_SUCCESSFUL', 'Sorry, there was an error');
default:
return _t(__CLASS__. '.DEFAULT_TITLE', 'Subscribe');
}
return match ($complete) {
'y' => _t(self::class. '.DEFAULT_TITLE_SUCCESSFUL', 'Thanks for subscribing'),
'n' => _t(self::class. '.DEFAULT_TITLE_NOT_SUCCESSFUL', 'Sorry, there was an error'),
default => _t(self::class. '.DEFAULT_TITLE', 'Subscribe'),
};
}

public function Link($action = null)
Expand Down Expand Up @@ -120,11 +108,8 @@ public function setFormNameSuffix(string $suffix = ''): self
*/
public function getFormNameSuffix(): string
{
if($this->formNameSuffix) {
$suffix = "_{$this->formNameSuffix}";
} else {
$suffix = "";
}
$suffix = $this->formNameSuffix ? "_{$this->formNameSuffix}" : "";

return $suffix;
}

Expand All @@ -133,11 +118,8 @@ public function getFormNameSuffix(): string
*/
public function getSubscriptionForm($useXhr = false): ?SubscribeForm
{
if($useXhr) {
$form = $this->XhrSubscribeForm();
} else {
$form = $this->SubscribeForm();
}
$form = $useXhr ? $this->XhrSubscribeForm() : $this->SubscribeForm();

return $form;
}

Expand Down Expand Up @@ -230,32 +212,30 @@ protected function configureForm(SubscribeForm $form): SubscribeForm
*/
protected function getFields(): FieldList
{
$fields = FieldList::create(
$name = TextField::create('Name', _t(__CLASS__. '.NAME', 'Name'))
->setAttribute('placeholder', _t(__CLASS__. '.YOUR_NAME', 'Your name'))
->setAttribute('title', _t(__CLASS__. '.NAME', 'Name'))
return FieldList::create(
$name = TextField::create('Name', _t(self::class. '.NAME', 'Name'))
->setAttribute('placeholder', _t(self::class. '.YOUR_NAME', 'Your name'))
->setAttribute('title', _t(self::class. '.NAME', 'Name'))
->setAttribute('required', 'required'),
$email = EmailField::create('Email', _t(__CLASS__. '.EMAIL', 'Email'))
->setAttribute('placeholder', _t(__CLASS__. '.EMAIL_ADDRESS', 'Email address'))
->setAttribute('title', _t(__CLASS__. '.EMAIL', 'Email'))
$email = EmailField::create('Email', _t(self::class. '.EMAIL', 'Email'))
->setAttribute('placeholder', _t(self::class. '.EMAIL_ADDRESS', 'Email address'))
->setAttribute('title', _t(self::class. '.EMAIL', 'Email'))
->setAttribute('required', 'required')
);
return $fields;
}

/**
* Get actions for the form
*/
protected function getActions(): FieldList
{
$actions = FieldList::create(
return FieldList::create(
FormAction::create(
'subscribe',
_t(__CLASS__ . '.SUBSCRIBE', 'Subscribe')
_t(self::class . '.SUBSCRIBE', 'Subscribe')
)->setUseButtonTag(true)
->addExtraClass('signup')
);
return $actions;
}

/**
Expand All @@ -271,14 +251,13 @@ protected function getValidator(): ?Validator
* Returns the validation callback upon errors
* A response is returned only upon errors in XHR submissions
* See FormRequestHandler::getValidationErrorResponse();
* @return callable
*/
protected function getCallbackForXhrValidation(): callable
{
return function (ValidationResult $result) {
// Fail, using the first message returned from the validation result
$messages = $result->getMessages();
$message = (!empty($messages[0]['message']) ? $messages[0]['message'] : '');
$message = (empty($messages[0]['message']) ? '' : $messages[0]['message']);
return $this->xhrError(400, $message);
};
}
Expand Down Expand Up @@ -308,26 +287,26 @@ private function handleError($code, $error_message, Form $form = null)
{
if($this->request->isAjax()) {
return $this->xhrError($code, $error_message);
} elseif($form) {
} elseif($form instanceof \SilverStripe\Forms\Form) {
// set session error on the form
$form->sessionError($error_message, ValidationResult::TYPE_ERROR);
}
return;
return null;
}

/**
* Handle successful submissions, based on the request type
*/
private function handleSuccess($code, $message, Form $form = null)
private function handleSuccess(int $code, Form $form = null)
{
$success_message = Config::inst()->get(MailchimpConfig::class, 'success_message');
if($this->request->isAjax()) {
return $this->xhrSuccess($code, $message, $success_message);
} elseif($form) {
return $this->xhrSuccess($code, $success_message);
} elseif($form instanceof \SilverStripe\Forms\Form) {
// set session message on the form
$form->sessionMessage($success_message, ValidationResult::TYPE_GOOD);
}
return;
return null;
}

/**
Expand All @@ -342,7 +321,7 @@ public function subscribe($data = [], Form $form = null)
$code = "";// MailchimpConfig.Code
$list_id = "";

if(!$form) {
if(!$form instanceof \SilverStripe\Forms\Form) {
throw new RequestException("Forbidden", 403);
}

Expand All @@ -351,14 +330,14 @@ public function subscribe($data = [], Form $form = null)
if(empty($data['code'])) {
// fail with error
$error_message = _t(
__CLASS__ . '.NO_CODE',
self::class . '.NO_CODE',
"No code was provided"
);
$error_code = 400;// default to invalid data

} else {

$code = strip_tags(trim($data['code'] ?: ''));
$code = strip_tags(trim((string) ($data['code'] ?: '')));
$error_message = "";
$error_code = 400;// default to invalid data
$mc_config = null;
Expand All @@ -368,7 +347,7 @@ public function subscribe($data = [], Form $form = null)
$enabled = MailchimpConfig::isEnabled();
if(!$enabled) {
$error_message = _t(
__CLASS__ . '.SUBSCRIPTIONS_NOT_AVAILABLE',
self::class . '.SUBSCRIPTIONS_NOT_AVAILABLE',
"Subscriptions are not available at the moment"
);
}
Expand All @@ -378,43 +357,45 @@ public function subscribe($data = [], Form $form = null)
if (empty($data['Email'])) {
// fail with error
$error_message = _t(
__CLASS__ . '.NO_EMAIL_ADDRESS',
self::class . '.NO_EMAIL_ADDRESS',
"No e-mail address was provided"
);
}

if (!Email::is_valid_address($data['Email'])) {
$error_message = _t(
__CLASS__ . '.INVALID_EMAIL_ADDRESS',
self::class . '.INVALID_EMAIL_ADDRESS',
"Please provide a valid e-mail address, '{email}' is not valid",
[
'email' => htmlspecialchars($data['Email'])
'email' => htmlspecialchars((string) $data['Email'])
]
);
}
}

if (!$error_message) {
// check code provided
if (!$code) {
if ($code === '' || $code === '0') {
$error_message = _t(
__CLASS__ . ".GENERIC_ERROR_1",
self::class . ".GENERIC_ERROR_1",
"Sorry, the sign-up could not be completed"
);
} else {
$mc_config = MailchimpConfig::getConfig('', '', $code);
if (empty($mc_config->ID)) {
$error_message = _t(
__CLASS__ . ".GENERIC_ERROR_2",
self::class . ".GENERIC_ERROR_2",
"Sorry, the sign-up could not be completed"
);
}

$list_id = $mc_config->getMailchimpListId();
}
}

if (!$list_id) {
$error_message = _t(
__CLASS__ . ".GENERIC_ERROR_3",
self::class . ".GENERIC_ERROR_3",
"Sorry, the sign-up could not be completed"
);
}
Expand Down Expand Up @@ -457,7 +438,7 @@ public function subscribe($data = [], Form $form = null)
}

// handle a successful subscription
$response = $this->handleSuccess(200, "OK", $form);
$response = $this->handleSuccess(200, $form);
if($response && ($response instanceof HTTPResponse)) {
// handle responses for e.g XHR
return $response;
Expand All @@ -473,7 +454,7 @@ public function subscribe($data = [], Form $form = null)
} catch (RequestException $e) {
$error_message = $e->getMessage();
$error_code = $e->getCode();
} catch (\Exception $e) {
} catch (\Exception) {
// general exceptin
$error_message = Config::inst()->get(MailchimpConfig::class, 'error_message');
$error_code = 500;
Expand All @@ -497,11 +478,10 @@ public function subscribe($data = [], Form $form = null)

/**
* Return error response for XHR
* @return HTTPResponse
*/
private function xhrError($code = 500, $message = "", $description = "")
private function xhrError($code = 500, $message = ""): \SilverStripe\Control\HTTPResponse
{
$response = new HTTPResponse();
$response = \SilverStripe\Control\HTTPResponse::create();
$response->setStatusCode($code);
$response->addHeader('Content-Type', 'application/json');
$response->addHeader('X-Submission-OK', '0');
Expand All @@ -511,11 +491,10 @@ private function xhrError($code = 500, $message = "", $description = "")

/**
* Return success response for XHR
* @return HTTPResponse
*/
private function xhrSuccess($code = 200, $message = "", $description = "")
private function xhrSuccess(int $code = 200, $description = ""): \SilverStripe\Control\HTTPResponse
{
$response = new HTTPResponse();
$response = \SilverStripe\Control\HTTPResponse::create();
$response->setStatusCode($code);
$response->addHeader('Content-Type', 'application/json');
$response->addHeader('X-Submission-OK', '1');
Expand Down
6 changes: 3 additions & 3 deletions src/Controllers/MailchimpAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@
*/
class MailchimpAdmin extends ModelAdmin
{
private static $managed_models = [
private static array $managed_models = [
MailchimpSubscriber::class,
MailchimpConfig::class
];

private static $menu_title = 'Mailchimp';
private static string $menu_title = 'Mailchimp';

private static $url_segment = 'mailchimp';
private static string $url_segment = 'mailchimp';
}
2 changes: 1 addition & 1 deletion src/Extensions/DisableSecurityTokenExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ class DisableSecurityTokenExtension extends Extension
{
public function updateChimpleSubscribeForm()
{
$this->owner->disableSecurityToken();
$this->getOwner()->disableSecurityToken();
}
}
2 changes: 2 additions & 0 deletions src/Extensions/PageExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public function ChimpleGlobalSubscribeForm()
if ($config) {
return $config->SubscribeForm();
}

return null;
}

Expand All @@ -34,6 +35,7 @@ public function ChimpleSubscribeForm($config_code)
if($config) {
return $config->SubscribeForm();
}

return null;
}
}
18 changes: 9 additions & 9 deletions src/Extensions/SiteConfigExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,40 +11,40 @@

class SiteConfigExtension extends DataExtension
{
private static $db = [
private static array $db = [
'MailchimpEnabled' => 'Boolean'
];

private static $has_one = [
private static array $has_one = [
'MailchimpConfig' => MailchimpConfig::class // global element for configuration
];

#[\Override]
public function updateCmsFields(FieldList $fields)
{
$fields->addFieldsToTab(
'Root.Mailchimp',
[
CheckboxField::create(
'MailchimpEnabled',
_t(__CLASS__. '.MAILCHIMP_ENABLED', 'Mailchimp subscriptions enabled')
_t(self::class. '.MAILCHIMP_ENABLED', 'Mailchimp subscriptions enabled')
),
DropdownField::create(
'MailchimpConfigID',
_t(__CLASS__. '.DEFAULT_MAILCHIMP_CONFIG', 'Default Mailchimp configuration'),
_t(self::class. '.DEFAULT_MAILCHIMP_CONFIG', 'Default Mailchimp configuration'),
MailchimpConfig::get()->map('ID', 'TitleCode')
)->setEmptyString('')
]
);
}

#[\Override]
public function onAfterWrite()
{
parent::onAfterWrite();
if($this->owner->MailchimpConfigID) {
if($config = MailchimpConfig::get()->byId($this->owner->MailchimpConfigID)) {
$config->IsGlobal = 1;
$config->write();
}
if ($this->getOwner()->MailchimpConfigID && ($config = MailchimpConfig::get()->byId($this->getOwner()->MailchimpConfigID))) {
$config->IsGlobal = 1;
$config->write();
}
}

Expand Down
Loading

0 comments on commit a9aae10

Please sign in to comment.