Skip to content

Commit

Permalink
Enable Gally by Website
Browse files Browse the repository at this point in the history
  • Loading branch information
PierreGauthier committed Nov 26, 2024
1 parent eccc835 commit b364197
Show file tree
Hide file tree
Showing 23 changed files with 453 additions and 57 deletions.
82 changes: 82 additions & 0 deletions src/Config/ConfigManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php
/**
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade Gally to newer versions in the future.
*
* @package Gally
* @author Gally Team <[email protected]>
* @copyright 2024-present Smile
* @license Open Software License v. 3.0 (OSL-3.0)
*/

declare(strict_types=1);

namespace Gally\OroPlugin\Config;

use Doctrine\Persistence\ManagerRegistry;
use Gally\OroPlugin\Search\SearchEngine;
use Oro\Bundle\ConfigBundle\Config\ConfigManager as OroConfigManager;
use Oro\Bundle\SecurityBundle\Encoder\SymmetricCrypterInterface;
use Oro\Bundle\WebsiteBundle\Entity\Website;

class ConfigManager
{
public function __construct(
private ManagerRegistry $registry,
private OroConfigManager $configManager,
private SymmetricCrypterInterface $crypter,
) {
}

public function getConfigValue(string $key, int $websiteId = null)
{
if ($websiteId) {
$website = $this->registry
->getManagerForClass(Website::class)
->getRepository(Website::class)
->find($websiteId);

return $this->configManager->get($key, false, false, $website);
}

return $this->configManager->get($key);
}

public function isGallyEnabled(int $websiteId = null): bool
{
return (bool) $this->getConfigValue('gally_oro.enabled', $websiteId);
}

public function getGallyUrl(): string
{
return $this->getConfigValue('gally_oro.url') ?? '';
}

public function getGallyEmail(): string
{
return $this->getConfigValue('gally_oro.email') ?? '';
}

public function getGallyPassword(): string
{
$encryptedPassword = $this->getConfigValue('gally_oro.password') ?? '';
$decryptedPassword = $this->crypter->decryptData($encryptedPassword);

return $decryptedPassword ?: $encryptedPassword;
}

public function getDsn(): string
{
$url = parse_url($this->getGallyUrl());

return sprintf(
'%s://%s:%s@%s:%s',
SearchEngine::ENGINE_NAME,
rawurlencode($this->getGallyEmail()),
rawurlencode($this->getGallyPassword()),
$url['host'],
$url['port'] ?? ('https' === $url['scheme'] ? 443 : 80)
);
}
}
2 changes: 1 addition & 1 deletion src/Decorator/SavePriceFilterUnit.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function apply(FilterDatasourceAdapterInterface $ds, $data): bool
$this->contextProvider->setPriceFilterUnit($data['unit']);
}

return $this->decorated->apply($ds, $data);
return $this->decorated->apply($ds, $data); // @phpstan-ignore argument.type
}

public function prepareData(array $data): array
Expand Down
47 changes: 47 additions & 0 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php
/**
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade Gally to newer versions in the future.
*
* @package Gally
* @author Gally Team <[email protected]>
* @copyright 2024-present Smile
* @license Open Software License v. 3.0 (OSL-3.0)
*/

declare(strict_types=1);

namespace Gally\OroPlugin\DependencyInjection;

use Oro\Bundle\ConfigBundle\DependencyInjection\SettingsBuilder;
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;

class Configuration implements ConfigurationInterface
{
public const ROOT_NODE = 'gally_oro';

/**
* {@inheritDoc}
*/
public function getConfigTreeBuilder(): TreeBuilder
{
$treeBuilder = new TreeBuilder(self::ROOT_NODE);
/** @var ArrayNodeDefinition $rootNode */
$rootNode = $treeBuilder->getRootNode();

SettingsBuilder::append(
$rootNode,
[
'enabled' => ['type' => 'boolean', 'value' => false],
'url' => ['type' => 'string', 'value' => ''],
'email' => ['type' => 'string', 'value' => ''],
'password' => ['type' => 'string', 'value' => ''],
]
);

return $treeBuilder;
}
}
9 changes: 9 additions & 0 deletions src/DependencyInjection/GallyOroExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

namespace Gally\OroPlugin\DependencyInjection;

use Oro\Bundle\ConfigBundle\DependencyInjection\SettingsBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader;
Expand All @@ -23,7 +24,15 @@ class GallyOroExtension extends Extension
{
public function load(array $configs, ContainerBuilder $container)
{
$config = $this->processConfiguration($this->getConfiguration($configs, $container), $configs);
$container->prependExtensionConfig($this->getAlias(), SettingsBuilder::getSettings($config));

$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
$loader->load('services.yml');
}

public function getAlias(): string
{
return 'gally_oro';
}
}
13 changes: 5 additions & 8 deletions src/Factory/ConfigurationFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,17 @@

namespace Gally\OroPlugin\Factory;

use Gally\OroPlugin\Config\ConfigManager;
use Gally\Sdk\Client\Configuration;
use Oro\Bundle\SearchBundle\Engine\EngineParameters;

class ConfigurationFactory
{
public static function create(EngineParameters $engineParameters): Configuration
public static function create(ConfigManager $configManager): Configuration
{
$scheme = '443' === $engineParameters->getPort() ? 'https' : 'http';
$url = "$scheme://{$engineParameters->getHost()}:{$engineParameters->getPort()}";

return new Configuration(
$url,
stripslashes($engineParameters->getUser()),
stripslashes($engineParameters->getPassword()),
$configManager->getGallyUrl(),
$configManager->getGallyEmail(),
$configManager->getGallyPassword(),
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@

namespace Gally\OroPlugin\Indexer\EventListener;

use Gally\OroPlugin\Config\ConfigManager;
use Gally\OroPlugin\Indexer\Indexer;
use Gally\OroPlugin\Search\SearchEngine;
use Oro\Bundle\LocaleBundle\Entity\Localization;
use Oro\Bundle\ProductBundle\Entity\Product;
use Oro\Bundle\ProductBundle\EventListener\WebsiteSearchProductIndexerListenerInterface;
use Oro\Bundle\SearchBundle\Engine\EngineParameters;
use Oro\Bundle\WebsiteSearchBundle\Engine\AbstractIndexer;
use Oro\Bundle\WebsiteSearchBundle\Engine\Context\ContextTrait;
use Oro\Bundle\WebsiteSearchBundle\Event\IndexEntityEvent;

Expand All @@ -31,13 +31,14 @@ class WebsiteSearchChildDataIndexerListener implements WebsiteSearchProductIndex
use ContextTrait;

public function __construct(
private EngineParameters $engineParameters,
private ConfigManager $configManager,
) {
}

public function onWebsiteSearchIndex(IndexEntityEvent $event): void
{
if (SearchEngine::ENGINE_NAME !== $this->engineParameters->getEngineName()) {
$currentWebsiteId = $event->getContext()[AbstractIndexer::CONTEXT_CURRENT_WEBSITE_ID_KEY];
if (!$this->configManager->isGallyEnabled($currentWebsiteId)) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
namespace Gally\OroPlugin\Indexer\EventListener;

use Doctrine\Persistence\ManagerRegistry;
use Gally\OroPlugin\Search\SearchEngine;
use Gally\OroPlugin\Config\ConfigManager;
use Oro\Bundle\InventoryBundle\Entity\InventoryLevel;
use Oro\Bundle\ProductBundle\EventListener\WebsiteSearchProductIndexerListenerInterface;
use Oro\Bundle\SearchBundle\Engine\EngineParameters;
use Oro\Bundle\WarehouseBundle\Provider\EnabledWarehousesProvider;
use Oro\Bundle\WebsiteSearchBundle\Engine\AbstractIndexer;
use Oro\Bundle\WebsiteSearchBundle\Engine\Context\ContextTrait;
use Oro\Bundle\WebsiteSearchBundle\Event\IndexEntityEvent;

Expand All @@ -32,14 +32,15 @@ class WebsiteSearchInventoryLevelIndexerListener implements WebsiteSearchProduct

public function __construct(
private ManagerRegistry $doctrine,
private EngineParameters $engineParameters,
private ConfigManager $configManager,
private EnabledWarehousesProvider $enabledWarehousesProvider,
) {
}

public function onWebsiteSearchIndex(IndexEntityEvent $event): void
{
if (SearchEngine::ENGINE_NAME !== $this->engineParameters->getEngineName()) {
$currentWebsiteId = $event->getContext()[AbstractIndexer::CONTEXT_CURRENT_WEBSITE_ID_KEY];
if (!$this->configManager->isGallyEnabled($currentWebsiteId)) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@
namespace Gally\OroPlugin\Indexer\EventListener;

use Doctrine\Persistence\ManagerRegistry;
use Gally\OroPlugin\Config\ConfigManager;
use Gally\OroPlugin\Indexer\Indexer;
use Gally\OroPlugin\Search\SearchEngine;
use Oro\Bundle\LocaleBundle\Entity\Localization;
use Oro\Bundle\LocaleBundle\Helper\LocalizationHelper;
use Oro\Bundle\ProductBundle\EventListener\WebsiteSearchProductIndexerListenerInterface;
use Oro\Bundle\SearchBundle\Engine\EngineParameters;
use Oro\Bundle\WebCatalogBundle\Entity\ContentNode;
use Oro\Bundle\WebCatalogBundle\Provider\WebCatalogProvider;
use Oro\Bundle\WebsiteBundle\Entity\Website;
use Oro\Bundle\WebsiteSearchBundle\Engine\AbstractIndexer;
use Oro\Bundle\WebsiteSearchBundle\Engine\Context\ContextTrait;
use Oro\Bundle\WebsiteSearchBundle\Event\IndexEntityEvent;
use Oro\Bundle\WebsiteSearchBundle\Manager\WebsiteContextManager;
Expand All @@ -38,19 +38,19 @@ class WebsiteSearchWebCatalogIndexerListener implements WebsiteSearchProductInde
public function __construct(
private WebsiteContextManager $websiteContextManager,
private ManagerRegistry $doctrine,
private EngineParameters $engineParameters,
private ConfigManager $configManager,
private WebCatalogProvider $webCatalogProvider,
protected LocalizationHelper $localizationHelper,
) {
}

public function onWebsiteSearchIndex(IndexEntityEvent $event): void
{
if (SearchEngine::ENGINE_NAME !== $this->engineParameters->getEngineName()) {
$currentWebsiteId = $event->getContext()[AbstractIndexer::CONTEXT_CURRENT_WEBSITE_ID_KEY];
if (!$this->configManager->isGallyEnabled($currentWebsiteId)) {
return;
}

// Todo manage partial update ?
if (!$this->hasContextFieldGroup($event->getContext(), 'main')) {
return;
}
Expand Down Expand Up @@ -89,8 +89,8 @@ public function onWebsiteSearchIndex(IndexEntityEvent $event): void

$event->addField($nodeId, 'level', $level);
$event->addField($nodeId, 'path', $path);
$event->addField($nodeId, 'name', $name?->getString());
$event->addField($nodeId, 'url', $url?->getText());
$event->addField($nodeId, 'name', $name?->getString()); // @phpstan-ignore nullsafe.neverNull
$event->addField($nodeId, 'url', $url?->getText()); // @phpstan-ignore nullsafe.neverNull
$event->addField($nodeId, 'tree', $this->getTree($root, $node, $localization));
}
}
Expand All @@ -112,7 +112,7 @@ private function getTree(ContentNode $root, ContentNode $node, Localization $loc
? []
: array_merge(
$this->getTree($root, $node->getParentNode(), $localization),
[$this->localizationHelper->getLocalizedValue($node->getTitles(), $localization)?->getString()],
[$this->localizationHelper->getLocalizedValue($node->getTitles(), $localization)?->getString()], // @phpstan-ignore nullsafe.neverNull
);
}
}
Loading

0 comments on commit b364197

Please sign in to comment.