You are here

public function DownloadLogger::log in Commerce File 8.2

Logs the download of the given file, for the given license.

Parameters

\Drupal\commerce_license\Entity\LicenseInterface $license: The license.

\Drupal\file\FileInterface $file: The downloaded file.

Overrides DownloadLoggerInterface::log

File

src/DownloadLogger.php, line 115

Class

DownloadLogger
Provides a service responsible for logging licensed file downloads.

Namespace

Drupal\commerce_file

Code

public function log(LicenseInterface $license, FileInterface $file) {

  // Reset the download count static cache for the given license.
  if (isset($this->downloadCounts[$license
    ->id()])) {
    unset($this->downloadCounts[$license
      ->id()]);
  }
  $request = $this->requestStack
    ->getCurrentRequest();
  $this->connection
    ->insert(self::TABLE_NAME)
    ->fields([
    'license_id' => $license
      ->id(),
    'fid' => $file
      ->id(),
    'uid' => $license
      ->getOwnerId(),
    'timestamp' => $this->time
      ->getRequestTime(),
    'ip_address' => $request
      ->getClientIp(),
  ])
    ->execute();
  Cache::invalidateTags($license
    ->getCacheTagsToInvalidate());
}