class FileResponseSubscriber in Commerce File 8.2
A subscriber to log downloads of licensed files.
Hierarchy
- class \Drupal\commerce_file\EventSubscriber\FileResponseSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface
Expanded class hierarchy of FileResponseSubscriber
1 string reference to 'FileResponseSubscriber'
1 service uses FileResponseSubscriber
File
- src/
EventSubscriber/ FileResponseSubscriber.php, line 16
Namespace
Drupal\commerce_file\EventSubscriberView source
class FileResponseSubscriber implements EventSubscriberInterface {
/**
* The current user.
*
* @var \Drupal\Core\Session\AccountInterface
*/
protected $currentUser;
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* The file download logger.
*
* @var \Drupal\commerce_file\DownloadLoggerInterface
*/
protected $downloadLogger;
/**
* The license file manager.
*
* @var \Drupal\commerce_file\LicenseFileManagerInterface
*/
protected $licenseFileManager;
/**
* Constructs a new FileResponseSubscriber.
*
* @param \Drupal\Core\Session\AccountInterface $current_user
* The current user.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
* @param \Drupal\commerce_file\DownloadLoggerInterface $download_logger
* The download logger.
* @param \Drupal\commerce_file\LicenseFileManagerInterface $license_file_manager
* The license file manager.
*/
public function __construct(AccountInterface $current_user, EntityTypeManagerInterface $entity_type_manager, DownloadLoggerInterface $download_logger, LicenseFileManagerInterface $license_file_manager) {
$this->currentUser = $current_user;
$this->entityTypeManager = $entity_type_manager;
$this->downloadLogger = $download_logger;
$this->licenseFileManager = $license_file_manager;
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
$events[KernelEvents::TERMINATE][] = [
'logFileDownload',
100,
];
return $events;
}
/**
* Logs file downloads for license owners.
*
* @param \Symfony\Component\HttpKernel\Event\PostResponseEvent $event
* The event object.
*/
public function logFileDownload(PostResponseEvent $event) {
// Not a successful response, nothing to do on our side.
if (!$event
->getResponse()
->isSuccessful()) {
return;
}
$headers = $event
->getResponse()->headers
->all();
// Check if the custom headers added by our logic in
// See commerce_file_file_download() are present.
// If these headers are present, this means a licensed file is being
// downloaded, therefore we should log the download.
if (!isset($headers['x-commerce-file-id'], $headers['x-commerce-license-id'])) {
return;
}
/** @var \Drupal\commerce_license\Entity\LicenseInterface $license */
$license = $this->entityTypeManager
->getStorage('commerce_license')
->load($headers['x-commerce-license-id'][0]);
/** @var \Drupal\file\FileInterface $file */
$file = $this->entityTypeManager
->getStorage('file')
->load($headers['x-commerce-file-id'][0]);
// The license or the file could not be loaded, stop here.
if (!$license || !$file) {
return;
}
// We skip logging file downloads if the download is not initiated by
// the license owner.
// When this happens, that means the download was initiated by an admin
// user who's not affected by file download limits, therefore it is
// pointless to log a file download that isn't going to be used.
if (!$this->licenseFileManager
->shouldLogDownload($license)) {
return;
}
$this->downloadLogger
->log($license, $file);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
FileResponseSubscriber:: |
protected | property | The current user. | |
FileResponseSubscriber:: |
protected | property | The file download logger. | |
FileResponseSubscriber:: |
protected | property | The entity type manager. | |
FileResponseSubscriber:: |
protected | property | The license file manager. | |
FileResponseSubscriber:: |
public static | function | Returns an array of event names this subscriber wants to listen to. | |
FileResponseSubscriber:: |
public | function | Logs file downloads for license owners. | |
FileResponseSubscriber:: |
public | function | Constructs a new FileResponseSubscriber. |