Newer
Older
catch
committed
<?php
namespace Drupal\hal\Normalizer;
Alex Bronstein
committed
use Drupal\Core\Config\ConfigFactoryInterface;
catch
committed
use Drupal\Core\Entity\EntityManagerInterface;
Alex Pott
committed
use Drupal\Core\Extension\ModuleHandlerInterface;
catch
committed
use Drupal\hal\LinkManager\LinkManagerInterface;
use GuzzleHttp\ClientInterface;
catch
committed
/**
* Converts the Drupal entity object structure to a HAL array structure.
Alex Bronstein
committed
*
* @deprecated in Drupal 8.5.0, to be removed before Drupal 9.0.0.
catch
committed
*/
Alex Pott
committed
class FileEntityNormalizer extends ContentEntityNormalizer {
catch
committed
/**
* The interface or class that this Normalizer supports.
*
* @var string
*/
protected $supportedInterfaceOrClass = 'Drupal\file\FileInterface';
/**
* The HTTP client.
*
* @var \GuzzleHttp\ClientInterface
catch
committed
*/
protected $httpClient;
Alex Bronstein
committed
/**
* The HAL settings config.
*
* @var \Drupal\Core\Config\ImmutableConfig
*/
protected $halSettings;
catch
committed
/**
* Constructs a FileEntityNormalizer object.
*
* @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
* The entity manager.
* @param \GuzzleHttp\ClientInterface $http_client
catch
committed
* The HTTP Client.
catch
committed
* @param \Drupal\hal\LinkManager\LinkManagerInterface $link_manager
catch
committed
* The hypermedia link manager.
Alex Pott
committed
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* The module handler.
Alex Bronstein
committed
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The config factory.
catch
committed
*/
Alex Bronstein
committed
public function __construct(EntityManagerInterface $entity_manager, ClientInterface $http_client, LinkManagerInterface $link_manager, ModuleHandlerInterface $module_handler, ConfigFactoryInterface $config_factory) {
Alex Pott
committed
parent::__construct($link_manager, $entity_manager, $module_handler);
catch
committed
$this->httpClient = $http_client;
Alex Bronstein
committed
$this->halSettings = $config_factory->get('hal.settings');
catch
committed
}
/**
* {@inheritdoc}
*/
public function normalize($entity, $format = NULL, array $context = []) {
catch
committed
$data = parent::normalize($entity, $format, $context);
Alex Bronstein
committed
$this->addCacheableDependency($context, $this->halSettings);
if ($this->halSettings->get('bc_file_uri_as_url_normalizer')) {
// Replace the file url with a full url for the file.
$data['uri'][0]['value'] = $this->getEntityUri($entity);
}
catch
committed
return $data;
}
/**
* {@inheritdoc}
*/
public function denormalize($data, $class, $format = NULL, array $context = []) {
$file_data = (string) $this->httpClient->get($data['uri'][0]['value'])->getBody();
catch
committed
$path = 'temporary://' . drupal_basename($data['uri'][0]['value']);
$data['uri'] = file_unmanaged_save_data($file_data, $path);
catch
committed
return $this->entityManager->getStorage('file')->create($data);