SubscriberCommon.php in Acquia Content Hub 8
File
acquia_contenthub_subscriber/src/SubscriberCommon.php
View source
<?php
namespace Drupal\acquia_contenthub_subscriber;
use Acquia\ContentHubClient\Entity as ContentHubEntity;
use Drupal\acquia_contenthub\ContentHubEntityDependency;
use Drupal\acquia_contenthub\ContentHubSearch;
use Drupal\Core\Entity\EntityTypeManagerInterface;
class SubscriberCommon {
protected $entities = [];
protected $filterMap = [];
protected $generator;
protected $contentHubFilterStorage;
protected $search;
public function __construct(EntityTypeManagerInterface $entity_type_manager, ContentHubSearch $search, ESQueryGeneratorInterface $generator) {
$this->contentHubFilterStorage = $entity_type_manager
->getStorage('contenthub_filter');
$this->search = $search;
$this->generator = $generator;
}
public function executeAllFilters($include_none = FALSE) {
$contenthub_filters = $this->contentHubFilterStorage
->loadMultiple();
foreach ($contenthub_filters as $filter) {
if ($filter
->getPublishStatus() === FALSE && !$include_none) {
continue;
}
$entities = $this
->executeFilter($filter, 0, 1);
$total = $entities['total'];
$iterations = ceil($total / 1000);
for ($i = 0; $i < $iterations; $i++) {
$start = $i * 1000;
$this->entities += $this
->executeFilter($filter, $start);
}
}
unset($this->entities['total']);
return $this->entities;
}
public function executeFilter(ContentHubFilterInterface $contenthub_filter, $start = 0, $size = 1000) {
$options = [
'start' => $start,
'count' => $size,
];
$conditions = $contenthub_filter
->getConditions();
if (!empty($conditions)) {
$items = $this
->getElasticSearchQueryResponse($contenthub_filter, NULL, NULL, $options);
$entities = [
'total' => $items['total'] ?? 0,
];
if (!isset($items['hits'])) {
return $entities;
}
foreach ($items['hits'] as $item) {
if (!in_array($item['_source']['data']['type'], ContentHubEntityDependency::getPostDependencyEntityTypes())) {
$entities[$item['_source']['data']['uuid']] = new ContentHubEntity($item['_source']['data']);
}
}
$entities_uuids = $entities;
foreach (array_keys($entities_uuids) as $uuid) {
$this->filterMap[$uuid] = $contenthub_filter
->id();
}
return $entities;
}
return [];
}
public function getFilterEntities() {
return $this->entities;
}
public function resetFilterEntities() {
$this->entities = [];
}
public function getFilterMap() {
return $this->filterMap;
}
protected function getElasticSearchQueryResponse(ContentHubFilterInterface $filter, $asset_uuid, $asset_type, array $options = []) {
return $this->search
->executeSearchQuery($this->generator
->getElasticSearchQuery($filter, $asset_uuid, $asset_type, $options));
}
}