Skip to content

Commit

Permalink
Improve error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremylichtman committed Feb 23, 2016
1 parent ac03067 commit 65cc3e8
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 43 deletions.
2 changes: 1 addition & 1 deletion campaignmonitor.links.menu.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
campaignmonitor.settings:
title: 'Campaign Monitor'
description: 'Configureation area for Campaign Monitor'
description: 'Configuration area for Campaign Monitor'
route_name: campaignmonitor.settings
parent: 'system.admin_config_services'
48 changes: 24 additions & 24 deletions src/CampaignMonitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,15 @@ protected function __construct($api_key = FALSE, $client_id = FALSE) {
* @param int $code
* Normally the HTTP response code.
*/
protected function addError($type, $message, $code = -1) {
protected function addError($message, $code = -1) {
$this->errors[] = array(
'type' => $type,
'type' => 'Error',
'code' => $code,
'message' => t($message),
);
if ($this->logErrors) {
$msg = t('Failed with code: @code and message: @msg', array('@code' => $code, '@msg' => $message));
\Drupal::logger('campaignmonitor')->log($type, $msg);
\Drupal::logger('campaignmonitor')->error($msg);
}
}

Expand Down Expand Up @@ -128,7 +128,7 @@ protected function libraryLoad($file) {
@require_once $this->libraryPath . '/' . $file;
return TRUE;
}
$this->addError(WATCHDOG_ERROR, t('Unable to load client library.'));
$this->addError(t('Unable to load client library.'));
return FALSE;
}

Expand All @@ -142,7 +142,7 @@ protected function createClientObj() {
if ($this->libraryLoad(self::CampaignMonitorCLIENT)) {
return new \CS_REST_Clients($this->client_id, $this->api_key);
}
$this->addError(WATCHDOG_ERROR, t('Failed to locate the client library.'));
$this->addError(t('Failed to locate the client library.'));
return FALSE;
}

Expand All @@ -159,7 +159,7 @@ protected function createListObj($listId) {
if ($this->libraryLoad(self::CampaignMonitorLIST)) {
return new \CS_REST_Lists($listId, $this->api_key);
}
$this->addError(WATCHDOG_ERROR, t('Failed to locate the list library.'));
$this->addError(t('Failed to locate the list library.'));
return FALSE;
}

Expand All @@ -176,7 +176,7 @@ protected function createSubscriberObj($listId) {
if ($this->libraryLoad(self::CampaignMonitorSubscribers)) {
return new \CS_REST_Subscribers($listId, $this->api_key);
}
$this->addError(WATCHDOG_ERROR, t('Failed to locate the subscribers library.'));
$this->addError(t('Failed to locate the subscribers library.'));
return FALSE;
}

Expand Down Expand Up @@ -310,7 +310,7 @@ public function getLists() {
\Drupal::cache()->set('campaignmonitor_lists', $this->lists);
}
else {
$this->addError(WATCHDOG_ERROR, $result->response->Message, $result->http_status_code);
$this->addError($result->response->Message, $result->http_status_code);
}
}
else {
Expand Down Expand Up @@ -342,7 +342,7 @@ public function getListDetails($listId) {

// Test that the listId is valid.
if (!isset($this->lists[$listId])) {
$this->addError(WATCHDOG_ERROR, t('Unknown list id @listID.', array('@listID' => $listId)));
$this->addError(t('Unknown list id @listID.', array('@listID' => $listId)));
return FALSE;
}

Expand All @@ -364,7 +364,7 @@ public function getListDetails($listId) {
\Drupal::cache()->set('campaignmonitor_lists', $this->lists);
}
else {
$this->addError(WATCHDOG_ERROR, $result->response->Message, $result->http_status_code);
$this->addError($result->response->Message, $result->http_status_code);
return FALSE;
}
}
Expand Down Expand Up @@ -396,7 +396,7 @@ public function getCustomFields($listId) {

// Test that the listId is valid.
if (!isset($this->lists[$listId])) {
$this->addError(WATCHDOG_ERROR, t('Unknown list id @listID.', array('@listID' => $listId)));
$this->addError(t('Unknown list id @listID.', array('@listID' => $listId)));
return FALSE;
}

Expand All @@ -417,7 +417,7 @@ public function getCustomFields($listId) {
\Drupal::cache()->set('campaignmonitor_lists', $this->lists);
}
else {
$this->addError(WATCHDOG_ERROR, $result->response->Message, $result->http_status_code);
$this->addError($result->response->Message, $result->http_status_code);
}
}
else {
Expand Down Expand Up @@ -448,14 +448,14 @@ public function getExtendedList($listId) {

// Test that the listId is valid.
if (!isset($this->lists[$listId])) {
$this->addError(WATCHDOG_ERROR, t('Unknown list id @listID.', array('@listID' => $listId)));
$this->addError(t('Unknown list id @listID.', array('@listID' => $listId)));
return FALSE;
}

// Load list details and custom fields (using is_array() since
// getCustomFields() may return an empty array).
if (!$this->getListDetails($listId) || !is_array($this->getCustomFields($listId))) {
$this->addError(WATCHDOG_ERROR, t('Could not retrieve extended information for @listID.', array('@listID' => $listId)));
$this->addError(t('Could not retrieve extended information for @listID.', array('@listID' => $listId)));
return FALSE;
}

Expand All @@ -478,7 +478,7 @@ public function getExtendedList($listId) {
public function updateList($listId, $options = array()) {
// Make sure that list is loaded.
if (!$this->getListDetails($listId)) {
$this->addError(WATCHDOG_ERROR, t('Could not retrieve update list information for @listID.', array('@listID' => $listId)));
$this->addError(t('Could not retrieve update list information for @listID.', array('@listID' => $listId)));
return FALSE;
}

Expand All @@ -499,7 +499,7 @@ public function updateList($listId, $options = array()) {
return TRUE;
}
else {
$this->addError(WATCHDOG_ERROR, $result->response->Message, $result->http_status_code);
$this->addError($result->response->Message, $result->http_status_code);
}
}
return FALSE;
Expand Down Expand Up @@ -545,7 +545,7 @@ public function getListStats($listId) {
\Drupal::cache()->set('campaignmonitor_list_stats', $this->listStats, $this->getCacheTimeout());
}
else {
$this->addError(WATCHDOG_ERROR, $result->response->Message, $result->http_status_code);
$this->addError($result->response->Message, $result->http_status_code);
return FALSE;
}
}
Expand Down Expand Up @@ -576,7 +576,7 @@ public function deleteList($listId) {
return TRUE;
}
else {
$this->addError(WATCHDOG_ERROR, $result->response->Message, $result->http_status_code);
$this->addError($result->response->Message, $result->http_status_code);
return FALSE;
}
}
Expand Down Expand Up @@ -615,7 +615,7 @@ public function createList($title, $unsubscribePage = '', $confirmedOptIn = FALS
return TRUE;
}
else {
$this->addError(WATCHDOG_ERROR, $result->response->Message, $result->http_status_code);
$this->addError($result->response->Message, $result->http_status_code);
return FALSE;
}
}
Expand Down Expand Up @@ -663,7 +663,7 @@ public function getCampaigns() {
\Drupal::cache()->set('campaignmonitor_campaigns', $this->campaigns, $this->getCacheTimeout());
}
else {
$this->addError(WATCHDOG_ERROR, $result->response->Message, $result->http_status_code);
$this->addError($result->response->Message, $result->http_status_code);
return FALSE;
}
}
Expand Down Expand Up @@ -738,7 +738,7 @@ public function getSubscriber($listId, $email) {
\Drupal::cache()->set('campaignmonitor_subscribers', $this->subscribers, $this->getCacheTimeout());
}
else {
$this->addError(WATCHDOG_ERROR, $result->response->Message, $result->http_status_code);
$this->addError($result->response->Message, $result->http_status_code);
return array();
}
}
Expand Down Expand Up @@ -820,7 +820,7 @@ public function subscribe($listId, $email, $name = '', $customFields = array())
'Resubscribe' => TRUE,
));
if (!$result->was_successful()) {
$this->addError(WATCHDOG_ERROR, $result->response->Message, $result->http_status_code);
$this->addError($result->response->Message, $result->http_status_code);
return FALSE;
}
$this->removeSubscriberFromCache($listId, $email);
Expand All @@ -844,7 +844,7 @@ public function unsubscribe($listId, $email) {
if ($obj = $this->createSubscriberObj($listId)) {
$result = $obj->unsubscribe($email);
if (!$result->was_successful()) {
$this->addError(WATCHDOG_ERROR, $result->response->Message, $result->http_status_code);
$this->addError($result->response->Message, $result->http_status_code);
return FALSE;
}
$this->removeSubscriberFromCache($listId, $email);
Expand All @@ -870,7 +870,7 @@ public function updateSubscriberEmail($listId, $oldEmail, $email) {
'Resubscribe' => TRUE,
));
if (!$result->was_successful()) {
$this->addError(WATCHDOG_ERROR, $result->response->Message, $result->http_status_code);
$this->addError($result->response->Message, $result->http_status_code);
return FALSE;
}
// Remove the old e-mail address from the subscriber cache.
Expand Down
60 changes: 42 additions & 18 deletions src/Form/SubscribeForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,23 @@ public function buildForm(array $form, FormStateInterface $form_state) {
'#type' => 'textfield',
'#required' => TRUE,
);

$form['lists'] = array(
'#title' => 'Newsletters',
'#type' => 'checkboxes',
'#options' => $enabled_lists,
'#required' => TRUE,
);


if (count($enabled_lists) > 1) {
$form['lists'] = array(
'#title' => 'Newsletters',
'#type' => 'checkboxes',
'#options' => $enabled_lists,
'#required' => TRUE,
);
}
// Hide if only one item.
else {
$form['lists'] = array(
'#type' => 'hidden',
'#value' => serialize($enabled_lists),
);
}

$form['subscribe'] = array(
'#type' => 'submit',
'#value' => 'Subscribe'
Expand All @@ -85,33 +94,45 @@ public function buildForm(array $form, FormStateInterface $form_state) {
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
$values = $form_state->getValues();

$email = trim($values['email']);
if (!$this->emailValidator->isValid($email)) {
$form_state->setErrorByName(
'email',
$this->t('Please submit a valid email address.')
);
}
}

/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$values = $form_state->getValues();
foreach($values['lists'] as $list_id) {

$list = $values['lists'];
// Deal with case of single item (i.e. hidden field).
if (is_array($list) == FALSE) {
$list = unserialize($list);
$list = array_keys($list);
}

foreach($list as $list_id) {

if(!$list_id) {
continue;
}

$email = trim($values['email']);
if (!$this->emailValidator->isValid($email)) {
form_set_error('', t('Please submit a valid email address.'));
$form_state['redirect'] = FALSE;
return FALSE;
}

$cm = $this->campaignMonitor;

// Update subscriber information or add new subscriber to the list.
if (!$cm->subscribe($list_id, $email)) {
form_set_error('', t('You were not subscribed to the list, please try again.'));
$form_state['redirect'] = FALSE;
drupal_set_message(
$this->t('You were not subscribed to the list, please try again.')
);
return FALSE;
}

Expand All @@ -121,7 +142,10 @@ public function submitForm(array &$form, FormStateInterface $form_state) {
drupal_goto($lists[$list_id]['details']['ConfirmationSuccessPage']);
}
else {
drupal_set_message(t('You are now subscribed to the "@list" list.', array('@list' => $lists[$list_id]['name'])), 'status');
drupal_set_message(
$this->t('You are now subscribed to the "@list" list.',
array('@list' => $lists[$list_id]['name'])),
'status');
}

}
Expand Down

0 comments on commit 65cc3e8

Please sign in to comment.