View source
<?php
namespace Drupal\acquia_contenthub;
use Drupal\acquia_contenthub\Session\ContentHubUserSession;
use Drupal\Component\Serialization\Json;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use Drupal\Core\Session\AccountSwitcherInterface;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpKernel\HttpKernelInterface;
class ContentHubInternalRequest {
protected $loggerFactory;
protected $kernel;
protected $contentHubSubscription;
protected $accountSwitcher;
protected $renderUser;
protected $requestStack;
public function __construct(HttpKernelInterface $kernel, ContentHubSubscription $contenthub_subscription, AccountSwitcherInterface $account_switcher, ConfigFactoryInterface $config_factory, LoggerChannelFactoryInterface $logger_factory, RequestStack $request_stack) {
$this->kernel = $kernel;
$this->contentHubSubscription = $contenthub_subscription;
$this->accountSwitcher = $account_switcher;
$this->loggerFactory = $logger_factory;
$this->renderUser = new ContentHubUserSession($config_factory
->get('acquia_contenthub.entity_config')
->get('user_role'));
$this->requestStack = $request_stack;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('http_kernel.basic'), $container
->get('acquia_contenthub.acquia_contenthub_subscription'), $container
->get('account_switcher'), $container
->get('config.factory'), $container
->get('logger.factory'), $container
->get('request_stack'));
}
public function getEntityCdfByInternalRequest($entity_type, $entity_id, $include_references = TRUE) {
global $base_path;
$this->accountSwitcher
->switchTo($this->renderUser);
try {
$params = [
'entity_type' => $entity_type,
'entity_id' => $entity_id,
$entity_type => $entity_id,
'_format' => 'acquia_contenthub_cdf',
];
if ($include_references) {
$params['include_references'] = 'true';
}
$url = Url::fromRoute('acquia_contenthub.entity.' . $entity_type . '.GET.acquia_contenthub_cdf', $params)
->toString();
$url = str_replace($base_path, '/', $url);
$master_request = $this->requestStack
->getCurrentRequest();
$request = Request::create($url, 'GET', [], $master_request->cookies
->all(), [], $master_request->server
->all());
$request = $this->contentHubSubscription
->setHmacAuthorization($request, TRUE);
$response = $this->kernel
->handle($request, HttpKernelInterface::SUB_REQUEST);
$entity_cdf_json = $response
->getContent();
$bulk_cdf = Json::decode($entity_cdf_json);
} catch (\Exception $e) {
$this->loggerFactory
->get('acquia_contenthub')
->debug('Exception: %msg', [
'%msg' => $e
->getMessage(),
]);
$bulk_cdf = [];
}
$this->accountSwitcher
->switchBack();
if (isset($bulk_cdf['message'])) {
$this->loggerFactory
->get('acquia_contenthub')
->debug('Exception: %msg', [
'%msg' => $bulk_cdf['message'],
]);
$bulk_cdf = [];
}
return empty($bulk_cdf) ? [
'entities' => [],
] : $bulk_cdf;
}
}