LicenseFileManager.php in Commerce File 8.2
File
src/LicenseFileManager.php
View source
<?php
namespace Drupal\commerce_file;
use Drupal\commerce\PurchasableEntityInterface;
use Drupal\commerce_license\Entity\LicenseInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\file\FileInterface;
class LicenseFileManager implements LicenseFileManagerInterface {
protected $currentUser;
protected $configFactory;
protected $entityTypeManager;
protected $downloadLogger;
protected $isLicensable = [];
protected $licenses = [];
public function __construct(AccountInterface $current_user, ConfigFactoryInterface $config_factory, EntityTypeManagerInterface $entity_type_manager, DownloadLoggerInterface $download_logger) {
$this->currentUser = $current_user;
$this->configFactory = $config_factory;
$this->entityTypeManager = $entity_type_manager;
$this->downloadLogger = $download_logger;
}
public function canDownload(LicenseInterface $license, FileInterface $file, AccountInterface $account = NULL) {
$account = $account ?: $license
->getOwner();
if ($account
->hasPermission('bypass license control') || $account
->hasPermission('administer commerce_license')) {
return TRUE;
}
if (!$license
->access('view', $account) || $license
->getState()
->getId() !== 'active') {
return FALSE;
}
$download_limit = $this
->getDownloadLimit($license);
if (!$download_limit) {
return TRUE;
}
$counts = $this->downloadLogger
->getDownloadCounts($license);
if (isset($counts[$file
->id()]) && $counts[$file
->id()] >= $download_limit) {
return FALSE;
}
return TRUE;
}
public function getActiveLicenses(FileInterface $file, AccountInterface $account = NULL, PurchasableEntityInterface $purchasable_entity = NULL) {
$account = $account ?: $this->currentUser;
$cache_key_components = [
$file
->id(),
$account
->id(),
];
if ($purchasable_entity) {
$cache_key_components[] = $purchasable_entity
->id();
}
$cache_key = implode(':', $cache_key_components);
if (array_key_exists($cache_key, $this->licenses)) {
return $this->licenses[$cache_key];
}
$this->licenses[$cache_key] = [];
if ($purchasable_entity) {
$product_variation_ids = [
$purchasable_entity
->id(),
];
}
else {
$product_variation_storage = $this->entityTypeManager
->getStorage('commerce_product_variation');
$results = $product_variation_storage
->getQuery()
->accessCheck(FALSE)
->condition('commerce_file.target_id', $file
->id())
->execute();
if (!$results) {
return [];
}
$product_variation_ids = array_keys($results);
}
$license_storage = $this->entityTypeManager
->getStorage('commerce_license');
$results = $license_storage
->getQuery()
->condition('type', 'commerce_file')
->condition('state', 'active')
->condition('product_variation', $product_variation_ids, 'IN')
->condition('uid', $account
->id())
->accessCheck(FALSE)
->sort('license_id')
->execute();
if (!$results) {
return [];
}
$this->licenses[$cache_key] = array_values($license_storage
->loadMultiple(array_keys($results)));
return $this->licenses[$cache_key];
}
public function getDownloadLimit(LicenseInterface $license) {
$download_limit = 0;
$settings = $this->configFactory
->get('commerce_file.settings')
->get();
if (!empty($settings['enable_download_limit'])) {
$download_limit = $settings['download_limit'];
}
if ($license
->hasField('file_download_limit') && !$license
->get('file_download_limit')
->isEmpty()) {
$download_limit = $license
->get('file_download_limit')->value;
}
return $download_limit;
}
public function isLicensable(FileInterface $file) {
if (isset($this->isLicensable[$file
->id()])) {
return $this->isLicensable[$file
->id()];
}
$product_variation_storage = $this->entityTypeManager
->getStorage('commerce_product_variation');
$query = $product_variation_storage
->getQuery()
->accessCheck(FALSE)
->condition('commerce_file.target_id', $file
->id())
->count();
$this->isLicensable[$file
->id()] = (bool) $query
->execute() > 0;
return $this->isLicensable[$file
->id()];
}
public function shouldLogDownload(LicenseInterface $license, AccountInterface $account = NULL) {
$account = $account ?: $this->currentUser;
if ($account
->hasPermission('bypass license control') || $account
->hasPermission('administer commerce_license')) {
return FALSE;
}
return $account
->id() == $license
->getOwnerId();
}
public function resetCache() {
$this->isLicensable = [];
$this->licenses = [];
}
}