You are here

public function VclHandler::uploadMaintenancePage in Fastly 8.3

Upload maintenance page.

Parameters

string $html: Content for maintenance page.

Return value

bool TRUE if New Error/Maintenance page is updated and activated, FALSE if unsuccessful.

File

src/VclHandler.php, line 375

Class

VclHandler
Class to control the VCL handling.

Namespace

Drupal\fastly

Code

public function uploadMaintenancePage($html) {
  try {
    $clone = $this
      ->cloneLastActiveVersion();
    if (FALSE === $clone) {
      $this
        ->addError($this
        ->t('Unable to clone last version'));
      return FALSE;
    }
    $condition = [
      'name' => 'drupalmodule_error_page_condition',
      'statement' => 'req.http.ResponseObject == "970"',
      'type' => 'REQUEST',
    ];
    $_condition = $this
      ->getCondition($condition["name"]);
    if ($_condition
      ->getStatusCode() == "404") {
      $this
        ->insertCondition($condition);
    }
    $response = [
      'name' => self::ERROR_PAGE_RESPONSE_OBJECT,
      'request_condition' => $condition["name"],
      'content' => $html,
      'status' => "503",
      'response' => "Service Temporarily Unavailable",
    ];
    $createResponse = $this
      ->createResponse($this->lastClonedVersion, $response);
    if (!$createResponse) {
      $this
        ->addError($this
        ->t('Failed to create a RESPONSE object.'));
      return FALSE;
    }
    $validate = $this
      ->validateVersion($this->lastClonedVersion);
    if (!$validate) {
      $this
        ->addError($this
        ->t('Failed to validate service version: @last_cloned_version', [
        '@last_cloned_version' => $this->lastClonedVersion,
      ]));
      return FALSE;
    }
    $vcl_dir = drupal_get_path('module', 'fastly') . '/vcl_snippets/errors';
    $singleVclData['vcl_dir'] = $vcl_dir;
    $singleVclData['type'] = 'deliver';
    $requests = [];
    if (!empty($singleVclData)) {
      $requests = array_merge($requests, $this
        ->prepareSingleVcl($singleVclData, "drupalmodule_error_page"));
    }
    $responses = [];
    foreach ($requests as $value) {
      if (!isset($value['type'])) {
        continue;
      }
      $url = $value['url'];
      $data = $value['data'];
      $type = $value['type'];
      $headers = [];
      $response = $this
        ->vclRequestWrapper($url, $headers, $data, $type);
      $responses[] = $response;
    }
    unset($responses);
    $request = $this
      ->prepareActivateVersion();
    $response = $this
      ->vclRequestWrapper($request['url'], $request['headers'], [], $request['type']);
    if ($response
      ->getStatusCode() != "200") {
      return FALSE;
    }
    $this->webhook
      ->sendWebHook($this
      ->t('*New Error/Maintenance page has updated and activated under config version @last_cloned_version', [
      '@last_cloned_version' => $this->lastClonedVersion,
    ]), "maintenance_page");
    return TRUE;
  } catch (\Exception $e) {
    $this
      ->addError($this
      ->t('@message', [
      '@message' => $e
        ->getMessage(),
    ]));
    return FALSE;
  }
}