acquia_vwo-1.0.x-dev/modules/acquia_vwo_content/src/Storage/AcquiaVwoStorage.php
modules/acquia_vwo_content/src/Storage/AcquiaVwoStorage.php
<?php
namespace Drupal\acquia_vwo_content\Storage;
use Drupal\Core\Database\Connection;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use Drupal\Component\Datetime\TimeInterface;
/**
* Storage for VWO mappings.
*/
class AcquiaVwoStorage {
/**
* The database connection.
*
* @var \Drupal\Core\Database\Connection
*/
protected $database;
/**
* The logger.
*
* @var \Drupal\Core\Logger\LoggerChannelInterface
*/
protected $logger;
/**
* The time service.
*
* @var \Drupal\Component\Datetime\TimeInterface
*/
protected $time;
/**
* Constructor.
*
* @param \Drupal\Core\Database\Connection $database
* The database connection.
* @param \Drupal\Core\Logger\LoggerChannelFactoryInterface $logger_factory
* The logger factory.
* @param \Drupal\Component\Datetime\TimeInterface $time
* The time service.
*/
public function __construct(Connection $database, LoggerChannelFactoryInterface $logger_factory, TimeInterface $time) {
$this->database = $database;
$this->logger = $logger_factory->get('acquia_vwo_content');
$this->time = $time;
}
/**
* Saves the VWO mapping for an entity.
*
* @param string $entity_uuid
* The entity UUID.
* @param string $entity_type_id
* The entity type ID.
* @param string $entity_id
* The entity ID.
* @param string $view_mode
* The view mode.
* @param string $vwo_id
* The VWO ID.
* @param string $export_status
* The export status.
*
* @return void
* Returns nothing.
*/
public function saveVwoMapping(string $entity_uuid, string $entity_type_id, string $entity_id, string $view_mode, string $vwo_id, string $export_status) {
try {
$fields = [
'entity_uuid' => $entity_uuid,
'entity_type_id' => $entity_type_id,
'entity_id' => $entity_id,
'view_mode' => $view_mode,
'vwo_id' => $vwo_id,
'export_status' => $export_status,
'modified' => $this->time->getRequestTime(),
];
// Check if the entity with the same UUID and view mode already exists.
$existing = $this->database->select('acquia_vwo_content_storage', 's')
->fields('s', ['entity_uuid'])
->condition('entity_uuid', $entity_uuid)
->condition('view_mode', $view_mode)
->execute()
->fetchField();
if (!$existing) {
$fields['created'] = $this->time->getRequestTime();
$this->database->insert('acquia_vwo_content_storage')
->fields($fields)
->execute();
}
else {
$this->database->update('acquia_vwo_content_storage')
->fields($fields)
->condition('entity_uuid', $entity_uuid)
->condition('view_mode', $view_mode)
->execute();
}
}
catch (\Exception $e) {
$this->logger->error('Failed to save VWO mapping: @message', ['@message' => $e->getMessage()]);
}
}
/**
* Fetches the VWO_ID for a given entity UUID.
*
* @param string $entity_uuid
* The entity UUID.
* @param string $view_mode
* The view mode.
*
* @return string|null
* The VWO ID or NULL if not found.
*/
public function getVwoId($entity_uuid, $view_mode): string|null {
try {
return $this->database->select('acquia_vwo_content_storage', 'v')
->fields('v', ['vwo_id'])
->condition('entity_uuid', $entity_uuid)
->condition('view_mode', $view_mode)
->execute()
->fetchField();
}
catch (\Exception $e) {
$this->logger->error('Failed to fetch VWO ID: @message', ['@message' => $e->getMessage()]);
return NULL;
}
}
/**
* Deletes the VWO mapping for a given entity UUID.
*
* @param string $entity_uuid
* The entity UUID.
* @param string $view_mode
* The view mode.
*
* @return void
* Returns nothing.
*/
public function deleteVwoMapping($entity_uuid, $view_mode): void {
try {
$this->database->delete('acquia_vwo_content_storage')
->condition('entity_uuid', $entity_uuid)
->condition('view_mode', $view_mode)
->execute();
}
catch (\Exception $e) {
$this->logger->error('Failed to delete VWO mapping: @message', ['@message' => $e->getMessage()]);
}
}
/**
* Sets the export status for an entity.
*
* @param string $entity_uuid
* The entity UUID.
* @param string $entity_type_id
* The entity type ID.
* @param string|null $entity_id
* The entity ID.
* @param bool $mark
* TRUE to mark for export, FALSE to unmark.
*/
public function setExportStatus(string $entity_uuid, string $entity_type_id, string|null $entity_id, bool $mark) {
try {
$fields = [
'export_to_vwo' => $mark ? 1 : 0,
'entity_type_id' => $entity_type_id,
'entity_id' => $entity_id,
'modified' => $this->time->getRequestTime(),
];
$this->database->merge('acquia_vwo_content_storage')
->key('entity_uuid', $entity_uuid)
->fields($fields)
->execute();
}
catch (\Exception $e) {
$this->logger->error('Failed to set export status: @message', ['@message' => $e->getMessage()]);
}
}
/**
* Checks if an entity is marked for export.
*
* @param string $entity_uuid
* The entity UUID.
*
* @return mixed|null
* The export status or NULL if not found.
*/
public function isMarkedForExport(string $entity_uuid) {
try {
return $this->database->select('acquia_vwo_content_storage', 's')
->fields('s', ['export_to_vwo'])
->condition('entity_uuid', $entity_uuid)
->execute()
->fetchField();
}
catch (\Exception $e) {
$this->logger->error('Failed to fetch VWO ID: @message', ['@message' => $e->getMessage()]);
return NULL;
}
}
}
