Skip to content

Commit

Permalink
NGSTACK-843 move getSiteConfigForContent method to new service SiteAc…
Browse files Browse the repository at this point in the history
…cessConfigResolver
  • Loading branch information
Katarina Miočić committed May 9, 2024
1 parent 8849dbf commit 890d577
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 44 deletions.
46 changes: 5 additions & 41 deletions lib/Core/Search/Common/PageTextExtractor/PageTextExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Ibexa\Contracts\Core\Persistence\Content\Location\Handler as LocationHandler;
use Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException;
use Ibexa\Contracts\Core\Repository\Exceptions\UnauthorizedException;
use Netgen\IbexaSearchExtra\Core\Search\Common\SiteAccessConfigResolver;
use Netgen\IbexaSearchExtra\Exception\IndexPageUnavailableException;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
Expand Down Expand Up @@ -38,14 +39,11 @@ class PageTextExtractor extends \Netgen\IbexaSearchExtra\Core\Search\Common\Page

private LoggerInterface $logger;

/**
* @param array<string, mixed> $sitesConfig
*/
public function __construct(
private readonly ContentHandler $contentHandler,
private readonly LocationHandler $locationHandler,
private readonly RouterInterface $router,
private readonly array $sitesConfig
private readonly SiteAccessConfigResolver $siteAccessConfigResolver
) {
$this->logger = new NullLogger();
}
Expand All @@ -63,7 +61,7 @@ public function setLogger(LoggerInterface $logger): void
*/
public function extractPageText(int $contentId, string $languageCode): array
{
$siteConfig = $this->getSiteConfigForContent($contentId);
$siteConfig = $this->siteAccessConfigResolver->getSiteConfigForContent($contentId);

if (isset($this->cache[$contentId][$languageCode])) {
return $this->cache[$contentId][$languageCode];
Expand Down Expand Up @@ -128,7 +126,7 @@ private function generateUrl(string $languageCode, int $contentId, array $siteCo

private function resolveSiteAccess(ContentInfo $contentInfo, string $languageCode): string
{
$siteConfig = $this->getSiteConfigForContent($contentInfo->id);
$siteConfig = $this->siteAccessConfigResolver->getSiteConfigForContent($contentInfo->id);

if (!isset($siteConfig['languages_siteaccess_map'][$languageCode])) {
throw new RuntimeException(
Expand Down Expand Up @@ -177,7 +175,7 @@ private function recursiveExtractTextArray(DOMNode $node, array &$textArray, int

private function getFieldName(DOMNode $node, int $contentId): null|string
{
$siteConfig = $this->getSiteConfigForContent($contentId);
$siteConfig = $this->siteAccessConfigResolver->getSiteConfigForContent($contentId);
$fields = $siteConfig['fields'];

foreach ($fields as $level => $tags) {
Expand Down Expand Up @@ -269,38 +267,4 @@ private function extractTextArray(string $html, int $contentId): array

return $textArray;
}

public function getSiteConfigForContent(int $contentId): array
{
$contentInfo = $this->contentHandler->loadContentInfo($contentId);

try {
$location = $this->locationHandler->load($contentInfo->mainLocationId);
} catch (NotFoundException) {
throw new RuntimeException(
sprintf(
'Content #%d does not have a location',
$contentInfo->id,
),
);
}

$pathString = $location->pathString;
$pathArray = explode('/', $pathString);

foreach ($this->sitesConfig as $site => $siteConfig) {
if (in_array($siteConfig['tree_root_location_id'], $pathArray, false)) {
$siteConfig['site'] = $site;
return $siteConfig;
}
}

throw new RuntimeException(
sprintf(
"Failed to match content ID %d to a siteaccess",
$contentInfo->id
)
);

}
}
56 changes: 56 additions & 0 deletions lib/Core/Search/Common/SiteAccessConfigResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace Netgen\IbexaSearchExtra\Core\Search\Common;

use Ibexa\Contracts\Core\Persistence\Content\Handler as ContentHandler;
use Ibexa\Contracts\Core\Persistence\Content\Location\Handler as LocationHandler;
use Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException;
use RuntimeException;

class SiteAccessConfigResolver
{

/**
* @param array<string, mixed> $sitesConfig
*/
public function __construct(
private readonly ContentHandler $contentHandler,
private readonly LocationHandler $locationHandler,
private readonly array $sitesConfig
) {
}

public function getSiteConfigForContent(int $contentId): array
{
$contentInfo = $this->contentHandler->loadContentInfo($contentId);

try {
$location = $this->locationHandler->load($contentInfo->mainLocationId);
} catch (NotFoundException) {
throw new RuntimeException(
sprintf(
'Content #%d does not have a location',
$contentInfo->id,
),
);
}

$pathString = $location->pathString;
$pathArray = explode('/', $pathString);

foreach ($this->sitesConfig as $site => $siteConfig) {
if (in_array($siteConfig['tree_root_location_id'], $pathArray, false)) {
$siteConfig['site'] = $site;
return $siteConfig;
}
}

throw new RuntimeException(
sprintf(
"Failed to match content ID %d to a siteaccess",
$contentInfo->id
)
);

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,19 @@

use Ibexa\Contracts\Core\Persistence\Content as SPIContent;
use Ibexa\Contracts\Core\Persistence\Content\Type\Handler as ContentTypeHandler;
use Ibexa\Contracts\Core\Persistence\Content\Location\Handler as LocationHandler;
use Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException;
use Ibexa\Contracts\Core\Search\Field;
use Ibexa\Contracts\Core\Search\FieldType\FullTextField;
use Netgen\IbexaSearchExtra\Core\Search\Common\PageTextExtractor;
use Netgen\IbexaSearchExtra\Core\Search\Common\SiteAccessConfigResolver;
use Netgen\IbexaSearchExtra\Core\Search\Elasticsearch\DocumentMapper\BlockTranslationFieldMapper;

class BlockPageTextFieldMapper extends BlockTranslationFieldMapper
{
public function __construct(
private readonly PageTextExtractor $pageTextExtractor,
private readonly ContentTypeHandler $contentTypeHandler,
private readonly SiteAccessConfigResolver $siteAccessConfigResolver
) {}

public function accept(SPIContent $content, string $languageCode): bool
Expand All @@ -28,7 +29,7 @@ public function accept(SPIContent $content, string $languageCode): bool
*/
public function mapFields(SPIContent $content, string $languageCode): array
{
$siteConfig = $this->pageTextExtractor->getSiteConfigForContent($content->versionInfo->contentInfo->id);
$siteConfig = $this->siteAccessConfigResolver->getSiteConfigForContent($content->versionInfo->contentInfo->id);
$fields = [];
$contentType = $this->contentTypeHandler->load($content->versionInfo->contentInfo->contentTypeId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,18 @@ services:
- '@Ibexa\Contracts\Core\Persistence\Content\Handler'
- '@Ibexa\Contracts\Core\Persistence\Content\Location\Handler'
- '@Ibexa\Bundle\Core\Routing\UrlAliasRouter'
- '%netgen_ibexa_search_extra.page_indexing.sites%'
- '@netgen.ibexa_search_extra.page_indexing.site_access_resolver'
calls:
- setLogger: ['@?logger']

netgen.ibexa_search_extra.page_indexing.site_access_resolver:
class: Netgen\IbexaSearchExtra\Core\Search\Common\SiteAccessConfigResolver
arguments:
- '@Ibexa\Contracts\Core\Persistence\Content\Handler'
- '@Ibexa\Contracts\Core\Persistence\Content\Location\Handler'
- '%netgen_ibexa_search_extra.page_indexing.sites%'


netgen.ibexa_search_extra.page_indexing:
class: Netgen\IbexaSearchExtra\Command\IndexPageContentCommand
tags:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ services:
arguments:
- '@netgen.ibexa_search_extra.page_indexing.page_text_extractor'
- '@Ibexa\Contracts\Core\Persistence\Content\Type\Handler'
- '@netgen.ibexa_search_extra.page_indexing.site_access_resolver'
tags:
- { name: netgen.ibexa_search_extra.elasticsearch.field_mapper.block_translation }

Expand Down

0 comments on commit 890d577

Please sign in to comment.