View source
<?php
namespace Drupal\gatsby;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Exception\ConnectException;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\node\NodeInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
class GatsbyPreview {
protected $httpClient;
protected $configFactory;
protected $entityTypeManager;
protected $logger;
public static $updateData = [];
public function __construct(ClientInterface $http_client, ConfigFactoryInterface $config, EntityTypeManagerInterface $entity_type_manager, LoggerChannelFactoryInterface $logger) {
$this->httpClient = $http_client;
$this->configFactory = $config;
$this->entityTypeManager = $entity_type_manager;
$this->logger = $logger
->get('gatsby');
}
public function updateData($key, $url, $json = FALSE, $preview_path = "") {
self::$updateData[$key][$url] = [
'url' => $url,
'json' => $json,
'path' => $preview_path,
];
}
public function gatsbyPrepareData(ContentEntityInterface $entity = NULL) {
$settings = $this->configFactory
->get('gatsby.settings');
$preview_url = $settings
->get('preview_callback_url');
if ($preview_url) {
$this
->updateData('preview', $preview_url, FALSE);
}
$incrementalbuild_url = $settings
->get('incrementalbuild_url');
if (!$incrementalbuild_url) {
return;
}
$build_published = $settings
->get('build_published');
if (!$build_published || $entity instanceof NodeInterface && $entity
->isPublished()) {
$this
->updateData('incrementalbuild', $incrementalbuild_url, FALSE);
}
}
public function gatsbyPrepareDelete(ContentEntityInterface $entity = NULL) {
$json = [
'id' => $entity
->uuid(),
'action' => 'delete',
];
$settings = $this->configFactory
->get('gatsby.settings');
$preview_url = $settings
->get('preview_callback_url');
if ($preview_url) {
$this
->updateData('preview', $preview_url, $json);
}
$incrementalbuild_url = $settings
->get('incrementalbuild_url');
if ($incrementalbuild_url) {
$this
->updateData('incrementalbuild', $incrementalbuild_url, $json);
}
}
public function isPreviewEntity(ContentEntityInterface $entity) {
$entityType = $entity
->getEntityTypeId();
$selectedEntityTypes = $this->configFactory
->get('gatsby.settings')
->get('preview_entity_types') ?: [];
return in_array($entityType, array_values($selectedEntityTypes), TRUE);
}
public function isConfigured() {
$settings = $this->configFactory
->get('gatsby.settings');
$preview_url = $settings
->get('preview_callback_url');
$incrementalbuild_url = $settings
->get('incrementalbuild_url');
if ($preview_url || $incrementalbuild_url) {
return TRUE;
}
return FALSE;
}
public function gatsbyUpdate() {
foreach (self::$updateData as $urls) {
foreach ($urls as $data) {
$this
->triggerRefresh($data['url'], $data['json'], $data['path']);
}
}
self::$updateData = [];
}
public function triggerRefresh($preview_callback_url, $json = FALSE, $path = "") {
if (stripos($preview_callback_url, ',')) {
$urls = array_map('trim', explode(',', $preview_callback_url));
foreach ($urls as $url) {
$this
->triggerRefresh($url, $json, $path);
}
return;
}
$data = [
'timeout' => 1,
];
if ($json) {
$data['json'] = $json;
}
try {
$this->httpClient
->post($preview_callback_url . $path, $data);
} catch (ConnectException $e) {
} catch (\Exception $e) {
$this->logger
->error($e
->getMessage());
}
}
}