Skip to content

Commit 565c9e0

Browse files
committed
TASK: Extract duplicate fulltext root search
1 parent 022a937 commit 565c9e0

3 files changed

Lines changed: 31 additions & 20 deletions

File tree

Classes/Driver/AbstractIndexerDriver.php

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
namespace Flowpack\ElasticSearch\ContentRepositoryAdaptor\Driver;
34

45
/*
@@ -22,9 +23,9 @@ abstract class AbstractIndexerDriver extends AbstractDriver
2223
* Whether the node is configured as fulltext root.
2324
*
2425
* @param NodeInterface $node
25-
* @return boolean
26+
* @return bool
2627
*/
27-
protected function isFulltextRoot(NodeInterface $node)
28+
protected function isFulltextRoot(NodeInterface $node): bool
2829
{
2930
if ($node->getNodeType()->hasConfiguration('search')) {
3031
$elasticSearchSettingsForNode = $node->getNodeType()->getConfiguration('search');
@@ -35,4 +36,24 @@ protected function isFulltextRoot(NodeInterface $node)
3536

3637
return false;
3738
}
39+
40+
/**
41+
* @param NodeInterface $node
42+
* @return NodeInterface|null
43+
*/
44+
protected function findClosestFulltextRoot(NodeInterface $node)
45+
{
46+
$closestFulltextNode = $node;
47+
while (!$this->isFulltextRoot($closestFulltextNode)) {
48+
$closestFulltextNode = $closestFulltextNode->getParent();
49+
if ($closestFulltextNode === null) {
50+
// root of hierarchy, no fulltext root found anymore, abort silently...
51+
$this->logger->log(sprintf('NodeIndexer: No fulltext root found for node %s (%s)', $node->getPath(), $node->getIdentifier()), LOG_WARNING, null, 'ElasticSearch (CR)');
52+
53+
return null;
54+
}
55+
}
56+
57+
return $closestFulltextNode;
58+
}
3859
}

Classes/Driver/Version1/IndexerDriver.php

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
namespace Flowpack\ElasticSearch\ContentRepositoryAdaptor\Driver\Version1;
34

45
/*
@@ -76,15 +77,9 @@ public function document($indexName, NodeInterface $node, ElasticSearchDocument
7677
*/
7778
public function fulltext(NodeInterface $node, array $fulltextIndexOfNode, $targetWorkspaceName = null)
7879
{
79-
$closestFulltextNode = $node;
80-
while (!$this->isFulltextRoot($closestFulltextNode)) {
81-
$closestFulltextNode = $closestFulltextNode->getParent();
82-
if ($closestFulltextNode === null) {
83-
// root of hierarchy, no fulltext root found anymore, abort silently...
84-
$this->logger->log(sprintf('NodeIndexer: No fulltext root found for node %s (%s)', $node->getPath(), $node->getIdentifier()), LOG_WARNING, null, 'ElasticSearch (CR)');
85-
86-
return null;
87-
}
80+
$closestFulltextNode = $this->findClosestFulltextRoot($node);
81+
if ($closestFulltextNode === null) {
82+
return null;
8883
}
8984

9085
$closestFulltextNodeContextPath = $closestFulltextNode->getContextPath();

Classes/Driver/Version2/IndexerDriver.php

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
namespace Flowpack\ElasticSearch\ContentRepositoryAdaptor\Driver\Version2;
34

45
/*
@@ -79,15 +80,9 @@ public function document($indexName, NodeInterface $node, ElasticSearchDocument
7980
*/
8081
public function fulltext(NodeInterface $node, array $fulltextIndexOfNode, $targetWorkspaceName = null)
8182
{
82-
$closestFulltextNode = $node;
83-
while (!$this->isFulltextRoot($closestFulltextNode)) {
84-
$closestFulltextNode = $closestFulltextNode->getParent();
85-
if ($closestFulltextNode === null) {
86-
// root of hierarchy, no fulltext root found anymore, abort silently...
87-
$this->logger->log(sprintf('NodeIndexer: No fulltext root found for node %s (%s)', $node->getPath(), $node->getIdentifier()), LOG_WARNING, null, 'ElasticSearch (CR)');
88-
89-
return null;
90-
}
83+
$closestFulltextNode = $this->findClosestFulltextRoot($node);
84+
if ($closestFulltextNode === null) {
85+
return null;
9186
}
9287

9388
$closestFulltextNodeContextPath = $closestFulltextNode->getContextPath();

0 commit comments

Comments
 (0)