You are here

public function VclHandler::execute in Fastly 8.3

Main execute function.

Takes values inserted into constructor, builds requests and sends them via Fastly API.

Parameters

bool $activate: (optional) TRUE to update and active, FALSE to update only.

Return value

mixed TRUE if executes successfully, FALSE if unsuccessful.

File

src/VclHandler.php, line 469

Class

VclHandler
Class to control the VCL handling.

Namespace

Drupal\fastly

Code

public function execute($activate = FALSE) {

  // Check if there are connection errors from construct.
  $errors = $this
    ->getErrors();
  if (!empty($errors)) {
    foreach ($errors as $error) {
      $this->messenger
        ->addError($error);
    }
    return FALSE;
  }

  // Check if last version is fetched.
  if ($this->lastVersionData === FALSE) {
    $this
      ->addError($this
      ->t('Last version does not exist'));
    return FALSE;
  }

  // Check if any of the data is set.
  if (empty($this->vclData) && empty($this->conditionData) && empty($this->settingData)) {
    $this
      ->addError($this
      ->t('No update data set, please specify, vcl, condition or setting data'));
    return FALSE;
  }
  try {
    if (FALSE === $this
      ->cloneLastActiveVersion()) {
      $this
        ->addError($this
        ->t('Unable to clone last version'));
      return FALSE;
    }
    $requests = [];
    if (!empty($this->vclData)) {
      $requests = array_merge($requests, $this
        ->prepareVcl());
    }
    if (!empty($this->conditionData)) {
      $conditions = $this
        ->prepareCondition();
      if (FALSE === $conditions) {
        $this
          ->addError($this
          ->t('Unable to insert new condition'));
        return FALSE;
      }
      $requests = array_merge($requests, $conditions);
    }
    if (!empty($this->settingData)) {
      $requests = array_merge($requests, $this
        ->prepareSetting());
    }
    if (!$this
      ->validateVersion()) {
      $this
        ->addError($this
        ->t('Version not validated'));
      return FALSE;
    }

    // Set Request Headers.
    foreach ($requests as $key => $request) {
      if (in_array($request['type'], [
        "POST",
        "PUT",
      ])) {
        $requests[$key]['headers'] = $this->headersPost;
      }
      else {
        $requests[$key]['headers'] = $this->headersGet;
      }
    }

    // Send Requests.
    $responses = [];
    foreach ($requests as $key => $value) {
      if (!isset($value['type'])) {
        continue;
      }
      $url = $value['url'];
      $data = $value['data'];
      $type = $value['type'];
      $headers = $value['headers'];
      $response = $this
        ->vclRequestWrapper($url, $headers, $data, $type);
      $responses[] = $response;
    }
    $pass = TRUE;
    foreach ($responses as $response) {
      if ($response
        ->getStatusCode() != "200") {
        $pass = FALSE;
        $this
          ->addError($this
          ->t('Some of the API requests failed, enable debugging and check logs for more information.'));
        $this->logger
          ->critical('VCL update failed : @body', [
          '@body' => json_decode($response
            ->getBody()),
        ]);
      }
    }

    // Activate version if vcl is successfully uploaded.
    if ($pass && $activate) {
      $request = $this
        ->prepareActivateVersion();
      $response = $this
        ->vclRequestWrapper($request['url'], $request['headers'], [], $request['type']);
      if ($response
        ->getStatusCode() != "200") {
        $this
          ->addError($this
          ->t('Some of the API requests failed, enable debugging and check logs for more information.'));
        $this->logger
          ->critical('Activation of new version failed : @body', [
          '@body' => $response
            ->getBody(),
        ]);
      }
      else {
        $this->logger
          ->info('VCL updated, version activated : ', [
          '@last_cloned_version' => $this->lastClonedVersion,
        ]);
      }
    }
    elseif ($pass && !$activate) {
      $message = $this
        ->t('VCL updated, but not activated.');
      $this->logger
        ->info($message);
      return $message;
    }
    $this->webhook
      ->sendWebHook($this
      ->t('VCL updated, but not activated on %base_url', [
      '%base_url' => $this->baseUrl,
    ]), "vcl_update");
  } catch (Exception $e) {
    $this
      ->addError($this
      ->t('Some of the API requests failed, enable debugging and check logs for more information.'));
    $this->logger
      ->critical('VCL update failed : @message', [
      '@message' => $e
        ->getMessage(),
    ]);
    foreach ($this
      ->getErrors() as $error) {

      // $error should have been passed through t() before $this->setError.
      $this->messenger
        ->addError($error);
    }
    return FALSE;
  }
  return TRUE;
}