You are here

protected function SnippetBuilder::saveSnippets in Hotjar 8.2

Same name and namespace in other branches
  1. 8 src/SnippetBuilder.php \Drupal\hotjar\SnippetBuilder::saveSnippets()

Saves JS snippet files based on current settings.

Return value

bool Whether the files were saved.

1 call to SnippetBuilder::saveSnippets()
SnippetBuilder::createAssets in src/SnippetBuilder.php
Prepares directory for and saves snippet files based on current settings.

File

src/SnippetBuilder.php, line 194

Class

SnippetBuilder
Snippet builder service.

Namespace

Drupal\hotjar

Code

protected function saveSnippets() {
  $snippet = $this
    ->buildSnippet();
  $snippet_path = $this
    ->getSnippetPath();
  if ($this->fileSystem
    ->realpath($snippet_path)) {
    $this->fileSystem
      ->delete($snippet_path);
  }
  $dir = $this->fileSystem
    ->realpath(dirname($snippet_path));
  $this->fileSystem
    ->prepareDirectory($dir, FileSystemInterface::CREATE_DIRECTORY | FileSystemInterface::MODIFY_PERMISSIONS);
  $path = $this->fileSystem
    ->saveData($snippet, $snippet_path, FileSystemInterface::EXISTS_REPLACE);
  if ($path === FALSE) {
    $this->messenger
      ->addMessage($this
      ->t('An error occurred saving one or more snippet files. Please try again or contact the site administrator if it persists.'));
    return FALSE;
  }
  $this->messenger
    ->addMessage($this
    ->t('Created snippet file based on configuration.'));
  $this->jsCollectionOptimizer
    ->deleteAll();
  _drupal_flush_css_js();
  return TRUE;
}