You are here

public function CampaignMonitorManager::getSubscriber in Campaign Monitor 8.2

Get values entered by the subscriber, when subscribing to a list.

Parameters

string $listId: The unique Campaign Monitor list ID.

string $email: The e-mail address that identifies the subscriber.

Return value

mixed|array|false An array containing subscriber information or FALSE on failure.

1 call to CampaignMonitorManager::getSubscriber()
CampaignMonitorManager::isSubscribed in src/CampaignMonitorManager.php
Check if user, identified by e-mail, is subscribed to a given list.

File

src/CampaignMonitorManager.php, line 869

Class

CampaignMonitorManager
Manager for Campaignmonitor.

Namespace

Drupal\campaignmonitor

Code

public function getSubscriber($listId, $email) {
  $fetch = FALSE;
  if (!isset($this->subscribers[$listId . $email])) {

    // Not found inside object, try the cache.
    if (($cache = $this->cacheBackend
      ->get('campaignmonitor_subscribers')) && !empty($cache->data)) {

      // Cache information found.
      $this->subscribers = $cache->data;
      if (!isset($this->subscribers[$listId . $email])) {

        // Not found inside cache either.
        $fetch = TRUE;
      }
    }
    else {

      // No cache found or expired.
      $fetch = TRUE;
    }
  }
  if ($fetch) {
    if ($obj = $this
      ->createSubscriberObj($listId)) {
      $this->subscribers[$listId . $email] = [];
      $result = $obj
        ->get($email);
      if ($result
        ->was_successful()) {
        foreach ($result->response as $key => $value) {
          if ($key == 'CustomFields') {

            // Convert the custom fields object into a keyed array.
            $this->subscribers[$listId . $email][$key] = [];
            foreach ($value as $field) {

              // Check if the field has been set. If not, set the value.
              if (!isset($this->subscribers[$listId . $email][$key][$field->Key])) {
                $this->subscribers[$listId . $email][$key][$field->Key] = $field->Value;
              }
              elseif (!is_array($this->subscribers[$listId . $email][$key][$field->Key])) {

                // If the field is not an array, assign an array to the field,
                // containing the previous value of the field and this new
                // value.
                $this->subscribers[$listId . $email][$key][$field->Key] = [
                  $this->subscribers[$listId . $email][$key][$field->Key],
                  $field->Value,
                ];
              }
              else {
                $this->subscribers[$listId . $email][$key][$field->Key][] = $field->Value;
              }
            }
          }
          else {
            $this->subscribers[$listId . $email][$key] = $value;
          }
        }

        // Save the subscriber information in the cache.
        $this->cacheBackend
          ->set('campaignmonitor_subscribers', $this->subscribers, $this
          ->getCacheTimeout());
      }
      else {
        $this
          ->addError('error', $result->response->Message, $result->http_status_code);
        return [];
      }
    }
    else {
      return FALSE;
    }
  }
  return $this->subscribers[$listId . $email];
}