You are here

public function HostMultipleRefreshConfirm::submitForm in http:BL 8

Form submission handler.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Overrides FormInterface::submitForm

File

src/Form/HostMultipleRefreshConfirm.php, line 189

Class

HostMultipleRefreshConfirm
Provides a multiple host expiry refresh confirmation form.

Namespace

Drupal\httpbl\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  if ($form_state
    ->getValue('confirm') && !empty($this->hostInfo)) {

    // Build expiry values.
    $safe_offset = \Drupal::state()
      ->get('httpbl.safe_offset') ?: 10800;
    $safe_timestamp = \Drupal::time()
      ->getRequestTime() + $safe_offset;
    $safe_time_message = \Drupal::service('date.formatter')
      ->formatTimeDiffUntil($safe_timestamp);
    $grey_offset = \Drupal::state()
      ->get('httpbl.greylist_offset') ?: 86400;
    $grey_timestamp = \Drupal::time()
      ->getRequestTime() + $grey_offset;
    $grey_time_message = \Drupal::service('date.formatter')
      ->formatTimeDiffUntil($grey_timestamp);
    $black_offset = \Drupal::state()
      ->get('httpbl.blacklist_offset') ?: 31536000;
    $black_timestamp = \Drupal::time()
      ->getRequestTime() + $black_offset;
    $black_time_message = \Drupal::service('date.formatter')
      ->formatTimeDiffUntil($black_timestamp);

    // Prepare work arrays for status types.
    $safe_hosts = [];
    $grey_hosts = [];
    $black_hosts = [];

    /** @var \Drupal\httpbl\HostInterface[] $hosts */
    $hosts = $this->storage
      ->loadMultiple(array_keys($this->hostInfo));
    foreach ($this->hostInfo as $id => $host_ips) {
      foreach ($host_ips as $host_ip) {
        $host = $hosts[$id];
        $status = $host
          ->getHostStatus();
        switch ($status) {
          case '0':
            $safe_hosts[$id] = $host;
            break;
          case '1':
            $black_hosts[$id] = $host;
            break;
          case '2':
            $grey_hosts[$id] = $host;
            break;
        }
      }
    }
    if ($safe_hosts) {
      foreach ($safe_hosts as $safe_host) {
        $host = $safe_host;
        $host
          ->setExpiry($safe_timestamp);
        $host
          ->setSource(HTTPBL_ADMIN_SOURCE);
        $host
          ->save();
      }
      $this->logTrapper
        ->trapNotice('Refreshed expiry for @count white-listed hosts to @time.', [
        '@count' => count($safe_hosts),
        '@time' => $safe_time_message,
      ]);
      $safe_count = count($safe_hosts);
      drupal_set_message($this
        ->formatPlural($safe_count, 'Refreshed 1 white-listed host.', 'Refreshed @count white-listed hosts.'));
    }
    if ($grey_hosts) {
      foreach ($grey_hosts as $grey_host) {
        $host = $grey_host;
        $host
          ->setExpiry($grey_timestamp);
        $host
          ->setSource(HTTPBL_ADMIN_SOURCE);
        $host
          ->save();
      }
      $this->logTrapper
        ->trapNotice('Refreshed expiry for @count grey-listed hosts to @time.', [
        '@count' => count($grey_hosts),
        '@time' => $grey_time_message,
      ]);
      $grey_count = count($grey_hosts);
      drupal_set_message($this
        ->formatPlural($grey_count, 'Refreshed 1 grey-listed host.', 'Refreshed @count grey-listed hosts.'));
    }
    if ($black_hosts) {
      foreach ($black_hosts as $black_host) {
        $host = $black_host;
        $host
          ->setExpiry($black_timestamp);
        $host
          ->setSource(HTTPBL_ADMIN_SOURCE);
        $host
          ->save();
      }
      $this->logTrapper
        ->trapNotice('Refreshed expiry for @count blacklisted hosts to @time.', [
        '@count' => count($black_hosts),
        '@time' => $black_time_message,
      ]);
      $black_count = count($black_hosts);
      drupal_set_message($this
        ->formatPlural($black_count, 'Refreshed 1 blacklisted host.', 'Refreshed @count blacklisted hosts.'));
    }
    $this->tempStoreFactory
      ->get('host_multiple_refresh_confirm')
      ->delete(\Drupal::currentUser()
      ->id());
  }
  $form_state
    ->setRedirect('entity.host.collection');
}