Skip to content

Commit

Permalink
FFWEB-3058: Remove FF version and API version
Browse files Browse the repository at this point in the history
 Remove FF version and API version with refactoring. New SDK support only ng and v5 for API.
  • Loading branch information
Rayn93 committed May 29, 2024
1 parent 9fe1252 commit 064cdd6
Show file tree
Hide file tree
Showing 17 changed files with 35 additions and 117 deletions.
4 changes: 1 addition & 3 deletions src/Controller/Adminhtml/FieldRoles/Update.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use Magento\Store\Model\StoreManagerInterface;
use Omikron\FactFinder\Communication\Client\ClientBuilder;
use Omikron\FactFinder\Communication\Resource\AdapterFactory;
use Omikron\FactFinder\Communication\Version;
use Omikron\Factfinder\Model\Api\CredentialsFactory;
use Omikron\Factfinder\Model\Config\CommunicationConfig;
use Omikron\Factfinder\Model\FieldRoles;
Expand Down Expand Up @@ -60,8 +59,7 @@ public function execute()
$this->communicationConfig->getApiVersion()
);
$searchAdapter = $adapterFactory->getSearchAdapter();
$response = $searchAdapter->search($this->communicationConfig->getChannel($storeId), 'Search.ff');
$searchResult = $this->communicationConfig->getVersion() === Version::NG ? $response : $response['searchResult'];
$searchResult = $searchAdapter->search($this->communicationConfig->getChannel($storeId), 'Search.ff');
$result->setData(['message' => __('Search result does not contain field roles')]);

if (isset($searchResult['fieldRoles'])) {
Expand Down
5 changes: 3 additions & 2 deletions src/Controller/Adminhtml/TestConnection/TestConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Omikron\FactFinder\Communication\Client\ClientBuilder;
use Omikron\FactFinder\Communication\Credentials;
use Omikron\FactFinder\Communication\Resource\AdapterFactory;
use Omikron\FactFinder\Communication\Version;
use Omikron\Factfinder\Logger\FactFinderLogger;
use Omikron\Factfinder\Model\Api\CredentialsFactory;
use Omikron\Factfinder\Model\Config\AuthConfig;
Expand Down Expand Up @@ -50,8 +51,8 @@ public function execute()

$adapterFactory = new AdapterFactory(
$clientBuilder,
$request->getParam('version'),
$request->getParam('ff_api_version')
Version::NG,
'v5'
);
$searchAdapter = $adapterFactory->getSearchAdapter();
$searchAdapter->search($request->getParam('channel'), '*');
Expand Down
4 changes: 2 additions & 2 deletions src/Model/Api/CredentialsFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ public function create(array $authData = null)
[
'username' => $this->authConfig->getUsername(),
'password' => $this->authConfig->getPassword(),
'prefix' => $this->authConfig->getAuthenticationPrefix(),
'postfix' => $this->authConfig->getAuthenticationPostfix()
'prefix' => '',
'postfix' => '',
]);
}
}
25 changes: 2 additions & 23 deletions src/Model/Api/PushImport.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Omikron\FactFinder\Communication\Client\ClientBuilder;
use Omikron\FactFinder\Communication\Client\ClientException;
use Omikron\FactFinder\Communication\Resource\AdapterFactory;
use Omikron\FactFinder\Communication\Version;
use Omikron\Factfinder\Model\Config\CommunicationConfig;
use Omikron\Factfinder\Model\Config\ExportConfig;
use Psr\Log\LoggerInterface;
Expand Down Expand Up @@ -54,7 +53,7 @@ public function execute(int $storeId): bool
return false;
}

if ($this->communicationConfig->getVersion() === Version::NG && $importAdapter->running($channel)) {
if ($importAdapter->running($channel)) {
throw new ClientException("Can't start a new import process. Another one is still going");
}

Expand All @@ -75,9 +74,7 @@ public function getPushImportResult(): string

private function prepareListFromPushImportResponses(array $responses): string
{
return strtolower($this->communicationConfig->getVersion()) === 'ng'
? $this->ngResponse($responses)
: $this->standardResponse($responses);
return $this->ngResponse($responses);
}

private function ngResponse(array $responses): string
Expand All @@ -101,22 +98,4 @@ private function ngResponse(array $responses): string

return sprintf($list, $listContent);
}

private function standardResponse(array $responses): string
{
$list = '<ul>%s</ul>';
$listContent = '';

if (!empty($responses['status'])) {
$statusList = sprintf(
'<ul>%s</ul>',
implode('', array_map(fn (string $message): string => sprintf('<li>%s</li>', $message), $responses['status']))
);

$statusMessages = sprintf('<li><i>Status messages</i></li><li>%s</li>', $statusList);
$listContent .= $statusMessages;
}

return sprintf($list, $listContent);
}
}
12 changes: 0 additions & 12 deletions src/Model/Config/AuthConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ class AuthConfig
{
private const PATH_USERNAME = 'factfinder/general/username';
private const PATH_PASSWORD = 'factfinder/general/password';
private const PATH_AUTH_PREFIX = 'factfinder/general/prefix';
private const PATH_AUTH_POSTFIX = 'factfinder/general/postfix';

private ScopeConfigInterface $scopeConfig;

Expand All @@ -30,14 +28,4 @@ public function getPassword(): string
{
return (string) $this->scopeConfig->getValue(self::PATH_PASSWORD, Scope::SCOPE_STORE);
}

public function getAuthenticationPrefix(): string
{
return (string) $this->scopeConfig->getValue(self::PATH_AUTH_PREFIX, Scope::SCOPE_STORE);
}

public function getAuthenticationPostfix(): string
{
return (string) $this->scopeConfig->getValue(self::PATH_AUTH_POSTFIX, Scope::SCOPE_STORE);
}
}
7 changes: 2 additions & 5 deletions src/Model/Config/CommunicationConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,12 @@
use Magento\Framework\UrlInterface;
use Magento\Store\Model\ScopeInterface;
use Omikron\Factfinder\Api\Config\ParametersSourceInterface;
use Omikron\FactFinder\Communication\Version;
use Omikron\Factfinder\Controller\Router;

class CommunicationConfig implements ParametersSourceInterface
{
private const PATH_CHANNEL = 'factfinder/general/channel';
private const PATH_ADDRESS = 'factfinder/general/address';
private const PATH_VERSION = 'factfinder/general/version';
private const PATH_API_VERSION = 'factfinder/general/ff_api_version';
private const PATH_IS_ENABLED = 'factfinder/general/is_enabled';
private const PATH_USE_PROXY = 'factfinder/general/ff_enrichment';
private const PATH_DATA_TRANSFER_IMPORT = 'factfinder/data_transfer/ff_push_import_enabled';
Expand Down Expand Up @@ -55,7 +52,7 @@ public function isPushImportEnabled(int $scopeId = null): bool

public function getVersion(): string
{
return (string) $this->scopeConfig->getValue(self::PATH_VERSION, ScopeInterface::SCOPE_STORES);
return 'ng';
}

public function getApiKey(): string
Expand All @@ -79,7 +76,7 @@ public function getParameters(): array

public function getApiVersion(): string
{
return (string) $this->scopeConfig->getValue(self::PATH_API_VERSION, ScopeInterface::SCOPE_STORES) ?? 'v4';
return 'v5';
}

private function getServerUrl(): string
Expand Down
6 changes: 3 additions & 3 deletions src/Model/Config/ExportConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\Serialize\SerializerInterface;
use Magento\Store\Model\ScopeInterface;
use Omikron\FactFinder\Communication\Version;

class ExportConfig
{
Expand Down Expand Up @@ -44,20 +43,21 @@ public function getPushImportDataTypes(int $scopeId = null): array
{
$configPath = 'factfinder/data_transfer/ff_push_import_type';
$dataTypes = (string) $this->scopeConfig->getValue($configPath, ScopeInterface::SCOPE_STORES, $scopeId);
$isNg = $this->communicationConfig->getVersion() === Version::NG;

return explode(',', $isNg ? $dataTypes : str_replace('search', 'data', $dataTypes));
return explode(',', $dataTypes);
}

private function getAttributeCodes(?int $storeId, callable $condition): array
{
$rows = array_filter($this->getConfigValue($storeId), $condition);

return array_values(array_unique(array_column($rows, 'code')));
}

private function getConfigValue(?int $storeId): array
{
$value = $this->scopeConfig->getValue(self::CONFIG_PATH, ScopeInterface::SCOPE_STORES, $storeId);

return array_map(
fn (array $row): array => ['multi' => !!$row['multi']] + $row,
(array) $this->serializer->unserialize($value ?: '[]')
Expand Down
6 changes: 1 addition & 5 deletions src/Model/Ssr/PriceFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Omikron\Factfinder\Model\Ssr;

use Magento\Framework\Pricing\PriceCurrencyInterface;
use Omikron\FactFinder\Communication\Version;
use Omikron\Factfinder\Model\Config\CommunicationConfig;
use Omikron\Factfinder\Model\FieldRoles;

Expand All @@ -21,11 +20,8 @@ public function __construct(
public function format(array $searchResult): array
{
$priceField = $this->fieldRoles->getFieldRole('price');
$isNG = $this->communicationConfig->getVersion() === Version::NG;
$records = $isNG ? $searchResult['hits'] : $searchResult['searchResult']['records'];
$recordField = $isNG ? 'masterValues' : 'record';

return ['records' => array_map($this->price($priceField, $recordField), $records)] + $searchResult;
return ['records' => array_map($this->price($priceField, 'masterValues'), $searchResult['hits'])] + $searchResult;
}

protected function price(string $priceField, string $recordField): callable
Expand Down
5 changes: 1 addition & 4 deletions src/Model/Ssr/SearchAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use Omikron\FactFinder\Communication\Client\ClientBuilder;
use Omikron\FactFinder\Communication\Client\ClientException;
use Omikron\FactFinder\Communication\Version;
use Omikron\Factfinder\Model\Api\CredentialsFactory;
use Omikron\Factfinder\Model\Config\CommunicationConfig;
use Psr\Http\Message\ResponseInterface;
Expand Down Expand Up @@ -50,8 +49,6 @@ private function createEndpoint(string $paramString, bool $navigationRequest)
$apiVersion = $this->communicationConfig->getApiVersion();
$endpoint = $navigationRequest ? 'navigation' : 'search';

return $this->communicationConfig->getVersion() == Version::NG
? "rest/{$apiVersion}/{$endpoint}/{$channel}?{$paramString}"
: "Search.ff?channel={$channel}&{$paramString}&format=json";
return "rest/{$apiVersion}/{$endpoint}/{$channel}?{$paramString}";
}
}
5 changes: 1 addition & 4 deletions src/Plugin/MapFieldRoles.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Omikron\Factfinder\Plugin;

use Omikron\FactFinder\Communication\Version;
use Omikron\Factfinder\Model\Config\CommunicationConfig;
use Omikron\Factfinder\Model\FieldRoles;

Expand All @@ -24,9 +23,7 @@ public function __construct(private readonly CommunicationConfig $communicationC
*/
public function aroundSaveFieldRoles(FieldRoles $subject, callable $proceed, array $fieldRoles, int $storeId)
{
$isNg = $this->communicationConfig->getVersion() === Version::NG;

return $proceed($isNg ? $this->map($fieldRoles) : $fieldRoles, $storeId);
return $proceed($this->map($fieldRoles), $storeId);
}

protected function map(array $fieldRoles): array
Expand Down
8 changes: 2 additions & 6 deletions src/Test/Unit/Model/Config/ExportConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,8 @@ public function test_single_fields_are_calculated_correctly()
$this->assertCount(2, $result);
}

public function test_correct_push_data_types_are_returned_for_different_ff_versions()
public function test_correct_push_data_types_are_returned()
{
$result = $this->testee->getPushImportDataTypes(1);
$this->assertContains('data', $result);
$this->assertNotContains('search', $result);

$result = $this->testee->getPushImportDataTypes(1);
$this->assertContains('search', $result);
$this->assertNotContains('data', $result);
Expand All @@ -63,7 +59,7 @@ protected function setUp(): void
['factfinder/export/attributes', Scope::SCOPE_STORES, 42,'{"_1":{"code":"color","multi":"0","numerical":"0"},"_2":{"code":"climate","multi":"1","numerical":"0"},"_3":{"code":"gender","multi":"0","numerical":"0"},"_4":{"code":"size","multi":"1","numerical":"1"}}']
]);
$communicationConfig = $this->createMock(CommunicationConfig::class);
$communicationConfig->method('getVersion')->willReturnOnConsecutiveCalls('7.3', 'ng');
$communicationConfig->method('getVersion')->willReturn('ng');
$this->testee = new ExportConfig($scopeConfig, new Json(), $communicationConfig);
}
}
8 changes: 0 additions & 8 deletions src/Test/Unit/Plugin/MapFieldRolesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,6 @@ public function test_it_will_map_field_roles_in_ng()
$this->plugin->aroundSaveFieldRoles($this->createMock(FieldRoles::class), $callbackMock, $this->fieldRoles, 1);
}

public function test_it_will_not_map_field_roles_in_pre_ng()
{
$this->configMock->method('getVersion')->willReturn('7.3');
//in fact, this field role does exist in 7.3 but mocked field roles are of NG format
$callbackMock = fn (array $fieldRoles, int $storeId) => $this->assertArrayNotHasKey('masterArticleNumber', $fieldRoles);
$this->plugin->aroundSaveFieldRoles($this->createMock(FieldRoles::class), $callbackMock, $this->fieldRoles, 1);
}

protected function setUp(): void
{
$this->configMock = $this->createMock(CommunicationConfig::class);
Expand Down
2 changes: 1 addition & 1 deletion src/Test/Unit/ViewModel/CommunicationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ protected function setUp(): void

$this->parametersProviderMock->method('getParameters')->willReturn([
'url' => 'http://some-url',
'version' => '7.3',
'version' => 'ng',
'user-id' => null,
'channel' => 'some-channel',
'use-cache' => 'true',
Expand Down
7 changes: 0 additions & 7 deletions src/ViewModel/CategoryPath.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Magento\Catalog\Model\Category;
use Magento\Framework\Registry;
use Magento\Framework\View\Element\Block\ArgumentInterface;
use Omikron\FactFinder\Communication\Version;
use Omikron\Factfinder\Model\Config\CommunicationConfig;

class CategoryPath implements ArgumentInterface
Expand All @@ -20,12 +19,6 @@ public function __construct(
) {
}

public function __toString()
{
return $this->communicationConfig->getVersion() === Version::NG ? $this->getCategoryPath(
) : '';
}

public function getCategoryPath(): array
{
$values = [];
Expand Down
2 changes: 1 addition & 1 deletion src/ViewModel/Communication.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function getParameters(array $blockParams = []): array

$params = $this->parametersProvider->getParameters();

return ['version' => $params['version'] ?? 'ng']
return ['version' => 'ng']
+ array_filter($this->mergeParameters($blockParams, $params) + $blockParams + $params, 'boolval');
}

Expand Down
42 changes: 15 additions & 27 deletions src/etc/adminhtml/system/general.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,21 @@
<label>Channel</label>
<validate>required-entry</validate>
</field>
<field id="version" translate="label comment" type="select" sortOrder="12" showInDefault="1" showInWebsite="1" showInStore="1">
<label>FACT-Finder version</label>
<options>
<option label="NG">ng</option>
<option label="7.3">7.3</option>
<option label="7.2">7.2</option>
</options>
</field>
<field id="ff_api_version" translate="label comment" type="select" sortOrder="13" showInDefault="1" showInWebsite="1" showInStore="1">
<label>FACT-Finder Api version</label>
<options>
<option label="v4">v4</option>
<option label="v5">v5</option>
</options>
</field>
<!-- <field id="version" translate="label comment" type="select" sortOrder="12" showInDefault="1" showInWebsite="1" showInStore="1">-->
<!-- <label>FACT-Finder version</label>-->
<!-- <options>-->
<!-- <option label="NG">ng</option>-->
<!-- <option label="7.3">7.3</option>-->
<!-- <option label="7.2">7.2</option>-->
<!-- </options>-->
<!-- </field>-->
<!-- <field id="ff_api_version" translate="label comment" type="select" sortOrder="13" showInDefault="1" showInWebsite="1" showInStore="1">-->
<!-- <label>FACT-Finder Api version</label>-->
<!-- <options>-->
<!-- <option label="v4">v4</option>-->
<!-- <option label="v5">v5</option>-->
<!-- </options>-->
<!-- </field>-->
<field id="username" translate="label" type="text" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Username</label>
<validate>required-entry</validate>
Expand All @@ -46,18 +46,6 @@
<label>API key</label>
<backend_model>Magento\Config\Model\Config\Backend\Encrypted</backend_model>
</field>
<field id="prefix" translate="label" type="text" sortOrder="50" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Authentication Prefix</label>
<depends>
<field negative="1" id="factfinder/general/version">ng</field>
</depends>
</field>
<field id="postfix" translate="label" type="text" sortOrder="60" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Authentication Postfix</label>
<depends>
<field negative="1" id="factfinder/general/version">ng</field>
</depends>
</field>
<field id="ff_build_connection" translate="label comment" type="button" sortOrder="71" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Test Connection</label>
<frontend_model>Omikron\Factfinder\Block\Adminhtml\System\Config\Button\TestConnection</frontend_model>
Expand Down
4 changes: 0 additions & 4 deletions src/etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@
<ff_enrichment>0</ff_enrichment>
<tracking_product_number_field_role>{"brand":"Brand","campaignProductNumber":"ProductNumber","deeplink":"Deeplink","description":"Description","displayProductNumber":"ProductNumber","ean":"EAN","imageUrl":"ImageURL","masterArticleNumber":"Master","price":"Price","productName":"Name","trackingProductNumber":"ProductNumber"}</tracking_product_number_field_role>
</general>
<advanced>
<version>7.3</version>
<ff_api_version>v4</ff_api_version>
</advanced>
<components>
<campaign_advisor>1</campaign_advisor>
<campaign_redirect>1</campaign_redirect>
Expand Down

0 comments on commit 064cdd6

Please sign in to comment.