Skip to content

Commit

Permalink
fix: use factory for service
Browse files Browse the repository at this point in the history
  • Loading branch information
lukmzig committed Nov 21, 2024
1 parent d2e6af0 commit d599578
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 11 deletions.
19 changes: 10 additions & 9 deletions src/DependencyInjection/AdvancedObjectSearchExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,16 @@ public function loadInternal(array $config, ContainerBuilder $container)

$definition = $container->getDefinition(UpdateQueueProcessor::class);
$definition->setArgument('$messengerQueueActivated', $config['messenger_queue_processing']['activated']);

$openSearchClientId = 'pimcore.open_search_client.' . $config['client_name'];
$container->setAlias('pimcore.advanced_object_search.opensearch-client', $openSearchClientId)
->setDeprecated(
'pimcore/advanced-object-search',
'6.1',
'The "%alias_id%" service alias is deprecated and will be removed in version 7.0. ' .
'Please use "pimcore.advanced_object_search.search-client" instead.'
);
if ($config['client_type'] === ClientType::OPEN_SEARCH->value) {
$openSearchClientId = 'pimcore.open_search_client.' . $config['client_name'];
$container->setAlias('pimcore.advanced_object_search.opensearch-client', $openSearchClientId)
->setDeprecated(
'pimcore/advanced-object-search',
'6.1',
'The "%alias_id%" service alias is deprecated and will be removed in version 7.0. ' .
'Please use "pimcore.advanced_object_search.search-client" instead.'
);
}

$clientId = $this->getDefaultSearchClientId($config);
$container->setAlias('pimcore.advanced_object_search.search-client', $clientId);
Expand Down
78 changes: 78 additions & 0 deletions src/Factory/OpenSearch/LegacyServiceFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php
declare(strict_types=1);

/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Commercial License (PCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PCL
*/

namespace AdvancedObjectSearchBundle\Factory\OpenSearch;

use AdvancedObjectSearchBundle\Service;
use AdvancedObjectSearchBundle\Tools\IndexConfigService;
use OpenSearch\ClientBuilder;
use Pimcore\Bundle\ElasticsearchClientBundle\SearchClient\ElasticsearchClientInterface;
use Pimcore\Bundle\OpenSearchClientBundle\SearchClient\OpenSearchClientInterface;
use Pimcore\SearchClient\SearchClientInterface;
use Pimcore\Security\User\TokenStorageUserResolver;
use Pimcore\Translation\Translator;
use Psr\Container\ContainerInterface;
use Psr\Log\LoggerInterface;
use RuntimeException;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

/**
* @internal
* @deprecated will be removed in version 7.0
*/
final readonly class LegacyServiceFactory

Check failure on line 36 in src/Factory/OpenSearch/LegacyServiceFactory.php

View workflow job for this annotation

GitHub Actions / Static Analysis with PHPStan (8.1, highest, false)

Readonly classes are supported only on PHP 8.2 and later.

Check failure on line 36 in src/Factory/OpenSearch/LegacyServiceFactory.php

View workflow job for this annotation

GitHub Actions / Static Analysis with PHPStan (8.1, highest, 11.x-dev, true)

Readonly classes are supported only on PHP 8.2 and later.
{
public function __construct(
private LoggerInterface $logger,
private TokenStorageUserResolver $userResolver,
private EventDispatcherInterface $eventDispatcher,
private Translator $translator,
private IndexConfigService $indexConfigService,
private SearchClientInterface $client
)
{

}

public function create(
ContainerInterface $filterLocator
): Service
{
$openSearchClient = match (true) {
$this->client instanceof OpenSearchClientInterface => $this->client->getOriginalClient(),
$this->client instanceof ElasticsearchClientInterface => ClientBuilder::create()->build(),
default => null,
};

if ($openSearchClient === null) {
throw new RuntimeException('No client found for OpenSearch');
}

$service = new Service(
$this->logger,
$this->userResolver,
$filterLocator,
$this->eventDispatcher,
$this->translator,
$this->indexConfigService,
$openSearchClient
);

$service->setSearchClientInterface($this->client);

return $service;
}
}
8 changes: 6 additions & 2 deletions src/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,20 @@ services:
tags:
- { name: monolog.logger, channel: advanced_object_search.es }

bundle.advanced_object_search.service.legacy_factory:
class: AdvancedObjectSearchBundle\Factory\OpenSearch\LegacyServiceFactory
arguments:
$client: '@pimcore.advanced_object_search.search-client'

bundle.advanced_object_search.service:
alias: AdvancedObjectSearchBundle\Service

AdvancedObjectSearchBundle\Service:
factory: ['@bundle.advanced_object_search.service.legacy_factory', 'create']
arguments:
$filterLocator: '@bundle.advanced_object_search.filter_locator'
$openSearchClient: '@pimcore.advanced_object_search.opensearch-client'
calls:
- [setCoreFieldsConfig, ['%advanced_object_search.core_fields_configuration%']]
- [setSearchClientInterface, ['@pimcore.advanced_object_search.search-client']]
tags:
- { name: monolog.logger, channel: pimcore_advanced_object_search }

Expand Down

0 comments on commit d599578

Please sign in to comment.