You are here

public function FacConfig::deleteFiles in Fast Autocomplete 8

Deletes Fast Autocomplete configuration JSON files.

Parameters

string $expiry_time: The expiry time for the files.

File

src/Entity/FacConfig.php, line 320

Class

FacConfig
Defines the FacConfig entity.

Namespace

Drupal\fac\Entity

Code

public function deleteFiles($expiry_time = NULL) {
  if (empty($expiry_time)) {

    // No date and time given so just delete the entire directory.
    \Drupal::service('file_system')
      ->deleteRecursive($this
      ->getFilesPath());
  }
  else {
    try {

      // Get all Fast Autocomplete json files.
      $json_files = \Drupal::service('file_system')
        ->scanDirectory($this
        ->getFilesPath(), '/.*\\.json$/');

      // Loop through all the files and delete those that have expired.
      foreach ($json_files as $json_file) {
        if (filectime($json_file->uri) < $expiry_time) {
          \Drupal::service('file_system')
            ->delete($json_file->uri);
        }
      }
    } catch (NotRegularDirectoryException $e) {

      // The directory does not exist. No action needed.
    }
  }
}