You are here

public function VclHandler::__construct in Fastly 8.3

Sets data to be processed, sets Credentials Vcl_Handler constructor.

Parameters

\Drupal\Core\Config\ConfigFactoryInterface $config_factory: The config.

string $host: The host to use to talk to the Fastly API.

\Drupal\fastly\Api $api: Fastly API for Drupal.

\Psr\Log\LoggerInterface $logger: The Fastly logger channel.

\Drupal\fastly\Services\Webhook $webhook: The Fastly webhook service.

\Symfony\Component\HttpFoundation\RequestStack $requestStack: The request stack object.

\Drupal\Core\Messenger\Messenger $messenger: Messenger.

ModuleHandlerInterface $module_handler:

File

src/VclHandler.php, line 191

Class

VclHandler
Class to control the VCL handling.

Namespace

Drupal\fastly

Code

public function __construct(ConfigFactoryInterface $config_factory, $host, Api $api, LoggerInterface $logger, Webhook $webhook, RequestStack $requestStack, Messenger $messenger, ModuleHandlerInterface $module_handler) {
  $this->moduleHandler = $module_handler;
  $vcl_dir = drupal_get_path('module', 'fastly') . '/vcl_snippets';
  $data = [
    'vcl' => [
      [
        'vcl_dir' => $vcl_dir,
        'type' => 'recv',
      ],
      [
        'vcl_dir' => $vcl_dir,
        'type' => 'deliver',
      ],
      [
        'vcl_dir' => $vcl_dir,
        'type' => 'error',
      ],
      [
        'vcl_dir' => $vcl_dir,
        'type' => 'fetch',
      ],
    ],
    'condition' => [
      [
        'name' => 'drupalmodule_request',
        'statement' => 'req.http.x-pass == "1"',
        'type' => 'REQUEST',
        'priority' => 90,
      ],
    ],
    'setting' => [
      [
        'name' => 'drupalmodule_setting',
        'action' => 'pass',
        'request_condition' => 'drupalmodule_request',
      ],
    ],
  ];

  // Give other modules ability to dlter data.
  $this->moduleHandler
    ->alter('vcl_handler_data', $data);
  $this->api = $api;
  $this->webhook = $webhook;
  $config = $config_factory
    ->get('fastly.settings');
  $this->vclData = !empty($data['vcl']) ? $data['vcl'] : FALSE;
  $this->conditionData = !empty($data['condition']) ? $data['condition'] : FALSE;
  $this->settingData = !empty($data['setting']) ? $data['setting'] : FALSE;
  $this->hostname = $host;
  $this->serviceId = getenv('FASTLY_API_SERVICE') ?: $config
    ->get('service_id');
  $this->apiKey = getenv('FASTLY_API_TOKEN') ?: $config
    ->get('api_key');
  $this->logger = $logger;
  $this->baseUrl = $requestStack
    ->getCurrentRequest()
    ->getHost();
  $connection = $this->api
    ->testFastlyApiConnection();
  if (!$connection['status']) {
    $this
      ->addError($connection['message']);
    return;
  }

  // Set credentials based data (API url, headers, last version)
  $this->versionBaseUrl = '/service/' . $this->serviceId . '/version';
  $this->headersGet = [
    'Fastly-Key' => $this->apiKey,
    'Accept' => 'application/json',
  ];
  $this->headersPost = [
    'Fastly-Key' => $this->apiKey,
    'Accept' => 'application/json',
    'Content-Type' => 'application/x-www-form-urlencoded',
  ];
  $this->lastVersionData = $this
    ->getLastVersion();
  if ($this->lastVersionData) {
    $this->lastActiveVersionNum = $this->lastVersionData->number;
  }
  $this->messenger = $messenger;
}