whitelabel-8.x-2.x-dev/src/WhiteLabelProvider.php
src/WhiteLabelProvider.php
<?php
namespace Drupal\whitelabel;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\whitelabel\Entity\WhiteLabelInterface;
/**
* Default implementation of the white label provider.
*/
class WhiteLabelProvider implements WhiteLabelProviderInterface {
/**
* The order storage.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* The White label negotiation manager.
*
* @var \Drupal\whitelabel\WhiteLabelNegotiatorInterface
*/
protected $negotiator;
/**
* The stored white label entity, for fast serve.
*
* @var \Drupal\whitelabel\Entity\WhiteLabelInterface
*/
protected $whiteLabel = NULL;
/**
* Constructs a new WhiteLabelProvider object.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
*/
public function __construct(EntityTypeManagerInterface $entity_type_manager) {
$this->entityTypeManager = $entity_type_manager;
}
/**
* {@inheritdoc}
*/
public function setNegotiator(WhiteLabelNegotiatorInterface $negotiator) {
$this->negotiator = $negotiator;
}
/**
* {@inheritdoc}
*/
public function getWhiteLabelByToken($token) {
if (empty($token)) {
return NULL;
}
if ($whitelabels = $this->entityTypeManager->getStorage('whitelabel')->loadByProperties(['token' => $token])) {
return reset($whitelabels);
}
return NULL;
}
/**
* {@inheritdoc}
*/
public function getWhiteLabel() {
if (empty($this->whiteLabel)) {
$whiteLabel = $this->negotiator->init();
if ($whiteLabel) {
$this->setWhiteLabel($whiteLabel);
}
}
// Ensure that the owner has permission to serve the white label, and that
// the current user has permission to view it.
if (!empty($this->whiteLabel) && $this->whiteLabel->access('serve', $this->whiteLabel->getOwner()) && $this->whiteLabel->access('view')) {
return $this->whiteLabel;
}
return NULL;
}
/**
* {@inheritdoc}
*/
public function setWhiteLabel(WhiteLabelInterface $white_label) {
$this->whiteLabel = $white_label;
}
/**
* {@inheritdoc}
*/
public function resetWhiteLabel() {
$this->whiteLabel = NULL;
}
}
