Skip to content

Commit

Permalink
Resource base locator unified
Browse files Browse the repository at this point in the history
  • Loading branch information
sitepark-veltrup committed Feb 27, 2024
1 parent 586301a commit 3d1062b
Show file tree
Hide file tree
Showing 11 changed files with 109 additions and 36 deletions.
23 changes: 14 additions & 9 deletions src/Console/Command/Indexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class Indexer extends Command
{
private IndexerProgressBar $progressBar;
private SymfonyStyle $io;
private TypifiedInput $input;
private OutputInterface $output;

/**
* phpcs:ignore
Expand Down Expand Up @@ -90,14 +90,15 @@ protected function execute(
OutputInterface $output
): int {

$this->input = new TypifiedInput($input);
$typedInput = new TypifiedInput($input);
$this->output = $output;
$this->io = new SymfonyStyle($input, $output);
$this->progressBar = $this->progressBarFactory->create($output);

$paths = $this->input->getArrayArgument('paths');
$paths = $typedInput->getArrayArgument('paths');

$cleanupThreshold = empty($paths)
? $this->input->getIntOption('cleanup-threshold')
? $typedInput->getIntOption('cleanup-threshold')
: 0;

if (empty($paths)) {
Expand All @@ -108,18 +109,18 @@ protected function execute(
}

$parameter = new IndexerParameter(
$this->input->getStringArgument('solr-core'),
$typedInput->getStringArgument('solr-core'),
$cleanupThreshold,
$this->input->getIntOption('chunk-size'),
$typedInput->getIntOption('chunk-size'),
$paths
);

$this->solrIndexerBuilder
->resourceDir($this->input->getStringArgument('resource-dir'))
->resourceDir($typedInput->getStringArgument('resource-dir'))
->progressBar($this->progressBar)
->documentEnricherList($this->documentEnricherList)
->solrConnectionUrl(
$this->input->getStringArgument('solr-connection-url')
$typedInput->getStringArgument('solr-connection-url')
);

$indexer = $this->solrIndexerBuilder->build();
Expand All @@ -133,7 +134,11 @@ protected function execute(
protected function errorReport(): void
{
foreach ($this->progressBar->getErrors() as $error) {
$this->io->error($error->getMessage());
if ($this->io->isVerbose() && $this->getApplication() !== null) {
$this->getApplication()->renderThrowable($error, $this->output);
} else {
$this->io->error($error->getMessage());
}
}
}
}
24 changes: 24 additions & 0 deletions src/Console/Command/ResourceBaseLocatorBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

namespace Atoolo\Search\Console\Command;

use Atoolo\Resource\Loader\ServerVarResourceBaseLocator;
use Atoolo\Resource\ResourceBaseLocator;

class ResourceBaseLocatorBuilder
{
public function build(string $resourceDir): ResourceBaseLocator
{
$subDirectory = null;
if (is_dir($resourceDir . '/objects')) {
$subDirectory = 'objects';
}
$_SERVER['RESOURCE_ROOT'] = $resourceDir;
return new ServerVarResourceBaseLocator(
'RESOURCE_ROOT',
$subDirectory
);
}
}
15 changes: 6 additions & 9 deletions src/Console/Command/SolrIndexerBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Atoolo\Search\Console\Command;

use Atoolo\Resource\Loader\ServerVarResourceBaseLocator;
use Atoolo\Resource\Loader\SiteKitLoader;
use Atoolo\Resource\Loader\SiteKitNavigationHierarchyLoader;
use Atoolo\Search\Console\Command\Io\IndexerProgressBar;
Expand Down Expand Up @@ -32,6 +31,10 @@ class SolrIndexerBuilder
private IndexerProgressBar $progressBar;
private string $solrConnectionUrl;

public function __construct(
private readonly ResourceBaseLocatorBuilder $resourceBaseLocatorBuilder
) {
}
public function resourceDir(string $resourceDir): SolrIndexerBuilder
{
$this->resourceDir = $resourceDir;
Expand Down Expand Up @@ -65,14 +68,8 @@ public function solrConnectionUrl(

public function build(): SolrIndexer
{
$subDirectory = null;
if (is_dir($this->resourceDir . '/objects')) {
$subDirectory = 'objects';
}
$_SERVER['RESOURCE_ROOT'] = $this->resourceDir;
$resourceBaseLocator = new ServerVarResourceBaseLocator(
'RESOURCE_ROOT',
$subDirectory
$resourceBaseLocator = $this->resourceBaseLocatorBuilder->build(
$this->resourceDir
);
$finder = new LocationFinder($resourceBaseLocator);
$resourceLoader = new SiteKitLoader($resourceBaseLocator);
Expand Down
14 changes: 11 additions & 3 deletions src/Console/Command/SolrMoreLikeThisBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,26 @@
namespace Atoolo\Search\Console\Command;

use Atoolo\Resource\Loader\SiteKitLoader;
use Atoolo\Resource\Loader\StaticResourceBaseLocator;
use Atoolo\Search\Service\Search\ExternalResourceFactory;
use Atoolo\Search\Service\Search\InternalMediaResourceFactory;
use Atoolo\Search\Service\Search\InternalResourceFactory;
use Atoolo\Search\Service\Search\SolrMoreLikeThis;
use Atoolo\Search\Service\Search\SolrResultToResourceResolver;
use Atoolo\Search\Service\SolrParameterClientFactory;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;

class SolrMoreLikeThisBuilder
{
private string $resourceDir;
private string $solrConnectionUrl;

public function __construct(
private readonly ResourceBaseLocatorBuilder $resourceBaseLocatorBuilder,
private readonly LoggerInterface $logger = new NullLogger()
) {
}

public function resourceDir(string $resourceDir): SolrMoreLikeThisBuilder
{
$this->resourceDir = $resourceDir;
Expand All @@ -33,7 +40,7 @@ public function solrConnectionUrl(

public function build(): SolrMoreLikeThis
{
$resourceBaseLocator = new StaticResourceBaseLocator(
$resourceBaseLocator = $this->resourceBaseLocatorBuilder->build(
$this->resourceDir
);
$resourceLoader = new SiteKitLoader($resourceBaseLocator);
Expand All @@ -53,7 +60,8 @@ public function build(): SolrMoreLikeThis
new InternalMediaResourceFactory($resourceLoader)
];
$solrResultToResourceResolver = new SolrResultToResourceResolver(
$resourceFactoryList
$resourceFactoryList,
$this->logger
);

return new SolrMoreLikeThis(
Expand Down
8 changes: 6 additions & 2 deletions src/Console/Command/SolrSelectBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Atoolo\Search\Console\Command;

use Atoolo\Resource\Loader\SiteKitLoader;
use Atoolo\Resource\Loader\StaticResourceBaseLocator;
use Atoolo\Search\Service\Search\ExternalResourceFactory;
use Atoolo\Search\Service\Search\InternalMediaResourceFactory;
use Atoolo\Search\Service\Search\InternalResourceFactory;
Expand All @@ -19,6 +18,11 @@ class SolrSelectBuilder
private string $resourceDir;
private string $solrConnectionUrl;

public function __construct(
private readonly ResourceBaseLocatorBuilder $resourceBaseLocatorBuilder
) {
}

public function resourceDir(string $resourceDir): SolrSelectBuilder
{
$this->resourceDir = $resourceDir;
Expand All @@ -34,7 +38,7 @@ public function solrConnectionUrl(

public function build(): SolrSelect
{
$resourceBaseLocator = new StaticResourceBaseLocator(
$resourceBaseLocator = $this->resourceBaseLocatorBuilder->build(
$this->resourceDir
);
$resourceLoader = new SiteKitLoader($resourceBaseLocator);
Expand Down
4 changes: 2 additions & 2 deletions src/Service/Search/SolrMoreLikeThis.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use Atoolo\Search\Service\SolrClientFactory;
use Solarium\Core\Client\Client;
use Solarium\QueryType\MoreLikeThis\Query as SolrMoreLikeThisQuery;
use Solarium\QueryType\Select\Result\Result as SelectResult;
use Solarium\QueryType\MoreLikeThis\Result as SolrMoreLikeThisResult;

/**
* Implementation of the "More-Like-This" on the basis of a Solr index.
Expand Down Expand Up @@ -58,7 +58,7 @@ private function buildSolrQuery(
}

private function buildResult(
SelectResult $result
SolrMoreLikeThisResult $result
): SearchResult {

$resourceList = $this->resultToResourceResolver
Expand Down
31 changes: 31 additions & 0 deletions test/Console/Command/ResourceBaseLocatorBuilderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace Atoolo\Search\Test\Console\Command;

use Atoolo\Search\Console\Command\ResourceBaseLocatorBuilder;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;

#[CoversClass(ResourceBaseLocatorBuilder::class)]
class ResourceBaseLocatorBuilderTest extends TestCase
{
public function testBuild(): void
{

$resourceDir = __DIR__ .
'/../../../var/test/ResourceBaseLocatorBuilderTest';
$objectsDir = $resourceDir . '/objects';
mkdir($objectsDir, 0777, true);

$builder = new ResourceBaseLocatorBuilder();
$locator = $builder->build($resourceDir);

$this->assertEquals(
$objectsDir,
$locator->locate(),
'unexpected resource dir'
);
}
}
12 changes: 5 additions & 7 deletions test/Console/Command/SolrIndexerBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Atoolo\Search\Test\Console\Command;

use Atoolo\Search\Console\Command\Io\IndexerProgressBar;
use Atoolo\Search\Console\Command\ResourceBaseLocatorBuilder;
use Atoolo\Search\Console\Command\SolrIndexerBuilder;
use Atoolo\Search\Service\Indexer\DocumentEnricher;
use PHPUnit\Framework\Attributes\CoversClass;
Expand All @@ -16,14 +17,11 @@ class SolrIndexerBuilderTest extends TestCase
public function testBuild(): void
{

$resourceDir = __DIR__ .
'/../../../var/test/SolrIndexerBuilderTest';
$objectsDir = $resourceDir . '/objects';
mkdir($objectsDir, 0777, true);

$builder = new SolrIndexerBuilder();
$builder = new SolrIndexerBuilder(
$this->createStub(ResourceBaseLocatorBuilder::class)
);
$builder
->resourceDir($resourceDir)
->resourceDir('test')
->documentEnricherList([$this->createStub(DocumentEnricher::class)])
->progressBar($this->createStub(IndexerProgressBar::class))
->solrConnectionUrl('http://localhost:8382');
Expand Down
5 changes: 4 additions & 1 deletion test/Console/Command/SolrMoreLikeThisBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Atoolo\Search\Test\Console\Command;

use Atoolo\Search\Console\Command\ResourceBaseLocatorBuilder;
use Atoolo\Search\Console\Command\SolrMoreLikeThisBuilder;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;
Expand All @@ -13,7 +14,9 @@ class SolrMoreLikeThisBuilderTest extends TestCase
{
public function testBuild(): void
{
$builder = new SolrMoreLikeThisBuilder();
$builder = new SolrMoreLikeThisBuilder(
$this->createStub(ResourceBaseLocatorBuilder::class)
);
$builder
->resourceDir('test.php')
->solrConnectionUrl('http://localhost:8382');
Expand Down
5 changes: 4 additions & 1 deletion test/Console/Command/SolrSelectBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Atoolo\Search\Test\Console\Command;

use Atoolo\Search\Console\Command\ResourceBaseLocatorBuilder;
use Atoolo\Search\Console\Command\SolrSelectBuilder;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;
Expand All @@ -13,7 +14,9 @@ class SolrSelectBuilderTest extends TestCase
{
public function testBuild(): void
{
$builder = new SolrSelectBuilder();
$builder = new SolrSelectBuilder(
$this->createStub(ResourceBaseLocatorBuilder::class)
);
$builder->resourceDir('test')
->solrConnectionUrl('http://localhost:8382');

Expand Down
4 changes: 2 additions & 2 deletions test/Service/Search/SolrMoreLikeThisTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
use PHPUnit\Framework\TestCase;
use Solarium\Client;
use Solarium\QueryType\MoreLikeThis\Query as SolrMoreLikeThisQuery;
use Solarium\QueryType\MoreLikeThis\Result as SolrMoreLikeThisResult;
use Solarium\QueryType\Select\Query\FilterQuery;
use Solarium\QueryType\Select\Result\Result as SelectResult;

#[CoversClass(SolrMoreLikeThis::class)]
class SolrMoreLikeThisTest extends TestCase
Expand All @@ -39,7 +39,7 @@ protected function setUp(): void

$client->method('createMoreLikeThis')->willReturn($query);

$result = $this->createStub(SelectResult::class);
$result = $this->createStub(SolrMoreLikeThisResult::class);
$client->method('execute')->willReturn($result);

$this->resource = $this->createStub(Resource::class);
Expand Down

0 comments on commit 3d1062b

Please sign in to comment.