-
Notifications
You must be signed in to change notification settings - Fork 7
feat: Added admin attribute functionalities #52
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
05ce8db
Made start on refactoring attribute landing admin functionalities
jansentjeu 782b65c
Made start on backend API controller functionality
jansentjeu 6324a58
Implemented facet backend API functionality
jansentjeu a939d74
Added caching for backend API requests
jansentjeu c83ac55
Made start on FacetAttributes backend API functionality
jansentjeu 5097332
Added FacetAttributes backend API functionality
jansentjeu 41e5385
Added facet request classes
jansentjeu 2380c9d
Updated versions of required packages
jansentjeu b515929
Fixed deprecated functionality
jansentjeu d96391c
Set other attribute and other attribute value as attribute and value
jansentjeu af12848
Other field instead of random first option when option not exists any…
jansentjeu 556787e
Use break instead of return $this for clarity
jansentjeu 701b303
Revert required version changes
jansentjeu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,234 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Tweakwise\AttributeLandingTweakwise\ApiClient; | ||
|
|
||
| use Exception; | ||
| use GuzzleHttp\Client; | ||
| use GuzzleHttp\ClientInterface; | ||
| use GuzzleHttp\Exception\GuzzleException; | ||
| use Magento\Framework\App\CacheInterface; | ||
| use Magento\Framework\Exception\LocalizedException; | ||
| use Magento\Framework\Serialize\Serializer\Json; | ||
| use Magento\Store\Model\Store; | ||
| use Psr\Http\Message\ResponseInterface; | ||
| use Psr\Log\LoggerInterface; | ||
| use Tweakwise\AttributeLandingTweakwise\Model\Config; | ||
|
|
||
| class BackendApiClient | ||
| { | ||
| private const TWEAKWISE_BACKEND_API_BASE_URL = 'https://navigator-api.tweakwise.com'; | ||
| private const CACHE_LIFETIME = 600; | ||
|
|
||
| /** | ||
| * @var ClientInterface|null | ||
| */ | ||
| private ?ClientInterface $httpClient = null; | ||
|
|
||
| /** | ||
| * @param Config $config | ||
| * @param LoggerInterface $logger | ||
| * @param Json $jsonSerializer | ||
| * @param CacheInterface $cache | ||
| */ | ||
| public function __construct( | ||
| private readonly Config $config, | ||
| private readonly LoggerInterface $logger, | ||
| private readonly Json $jsonSerializer, | ||
| private readonly CacheInterface $cache | ||
| ) { | ||
| } | ||
|
|
||
| /** | ||
| * @return ClientInterface | ||
| */ | ||
| private function getHttpClient(): ClientInterface | ||
| { | ||
| if ($this->httpClient === null) { | ||
| $this->httpClient = new Client( | ||
| [ | ||
| 'base_uri' => self::TWEAKWISE_BACKEND_API_BASE_URL | ||
| ] | ||
| ); | ||
| } | ||
|
|
||
| return $this->httpClient; | ||
| } | ||
|
|
||
| /** | ||
| * @param string $path | ||
| * @param Store $store | ||
| * @return ResponseInterface | ||
| * @throws GuzzleException | ||
| * @throws LocalizedException | ||
| */ | ||
| public function doRequest( | ||
| string $path, | ||
| Store $store | ||
| ): ResponseInterface { | ||
| try { | ||
| return $this->getHttpClient() | ||
| ->request( | ||
| 'GET', | ||
| $path, | ||
| [ | ||
| 'headers' => [ | ||
| 'TWN-InstanceKey' => $this->config->getGeneralAuthenticationKey($store), | ||
| 'TWN-Authentication' => $this->config->getBackendApiToken($store) | ||
| ] | ||
| ] | ||
| ); | ||
| } catch (Exception $e) { | ||
| $this->logger->critical( | ||
| 'Tweakwise Backend API request failed', | ||
| [ | ||
| 'path' => $path, | ||
| 'exception' => $e->getMessage() | ||
| ] | ||
| ); | ||
| throw $e; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * @param Store $store | ||
| * @return array | ||
| */ | ||
| public function getAttributes(Store $store): array | ||
| { | ||
| $cacheKey = $this->getCacheKey('attributes', (int)$store->getId()); | ||
| if ($this->cacheExists($cacheKey)) { | ||
| return $this->getFromCache($cacheKey); | ||
| } | ||
|
|
||
| $attributes = []; | ||
| try { | ||
| $response = $this->doRequest('attribute', $store); | ||
| $contents = $response->getBody()->getContents(); | ||
| $result = $this->jsonSerializer->unserialize($contents); | ||
|
|
||
| if (!isset($result['Records'])) { | ||
| return []; | ||
| } | ||
|
|
||
| foreach ($result['Records'] as $record) { | ||
| $attributes[] = [ | ||
| 'value' => $record['UrlName'], | ||
| 'label' => $record['Name'], | ||
| 'id' => $record['Id'] | ||
| ]; | ||
| } | ||
|
|
||
| $this->cache->save($this->jsonSerializer->serialize($attributes), $cacheKey, [], $this->getCacheLifetime()); | ||
|
|
||
| return $attributes; | ||
| } catch (GuzzleException | Exception $e) { | ||
| $this->logger->critical( | ||
| 'Retrieving attributes from Tweakiwse Backend API failed', | ||
| [ | ||
| 'exception' => $e->getMessage() | ||
| ] | ||
| ); | ||
| return []; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * @param string $attributeCode | ||
| * @param Store $store | ||
| * @return int|null | ||
| */ | ||
| public function getAttributeIdByCode(string $attributeCode, Store $store): ?int | ||
| { | ||
| $attributes = $this->getAttributes($store); | ||
| $index = array_search($attributeCode, array_column($attributes, 'value'), true); | ||
|
|
||
| return $index !== false ? (int)$attributes[$index]['id'] : null; | ||
| } | ||
|
|
||
| /** | ||
| * @param int $attributeId | ||
| * @param Store $store | ||
| * @return array | ||
| */ | ||
| public function getAttributeValues(int $attributeId, Store $store): array | ||
| { | ||
| $cacheKey = $this->getCacheKey('attribute_values', (int)$store->getId(), $attributeId); | ||
| if ($this->cacheExists($cacheKey)) { | ||
| return $this->getFromCache($cacheKey); | ||
| } | ||
|
|
||
| $attributeValues = []; | ||
| try { | ||
| $response = $this->doRequest(sprintf('attribute/%s/values', $attributeId), $store); | ||
| $contents = $response->getBody()->getContents(); | ||
| $result = $this->jsonSerializer->unserialize($contents); | ||
|
|
||
| if (!isset($result['Records'])) { | ||
| return []; | ||
| } | ||
|
|
||
| foreach ($result['Records'] as $record) { | ||
| $attributeValues[] = [ | ||
| 'value' => $record['Value'], | ||
| 'label' => $record['Value'], | ||
| ]; | ||
| } | ||
|
|
||
| $this->cache->save($this->jsonSerializer->serialize($attributeValues), $cacheKey, [], $this->getCacheLifetime()); | ||
|
|
||
| return $attributeValues; | ||
| } catch (GuzzleException | Exception $e) { | ||
| $this->logger->critical( | ||
| 'Retrieving attribute valus from Tweakiwse Backend API failed', | ||
| [ | ||
| 'exception' => $e->getMessage() | ||
| ] | ||
| ); | ||
| return []; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * @param string $type | ||
| * @param int $storeId | ||
| * @param int|null $attributeId | ||
| * @return string | ||
| */ | ||
| private function getCacheKey(string $type, int $storeId, ?int $attributeId = null): string | ||
| { | ||
| if ($attributeId) { | ||
| return sprintf('tweakwise_backend_api_result_%s_%s_%s', $type, $attributeId, $storeId); | ||
| } | ||
| return sprintf('tweakwise_backend_api_result_%s_%s', $type, $storeId); | ||
| } | ||
|
|
||
| /** | ||
| * @param string $cacheKey | ||
| * @return bool | ||
| */ | ||
| private function cacheExists(string $cacheKey): bool | ||
| { | ||
| /** @phpstan-ignore-next-line */ | ||
| return $this->cache->load($cacheKey) !== false; | ||
| } | ||
|
|
||
| /** | ||
| * @param string $cacheKey | ||
| * @return array | ||
| */ | ||
| private function getFromCache(string $cacheKey): array | ||
| { | ||
| return (array)$this->jsonSerializer->unserialize($this->cache->load($cacheKey)); | ||
| } | ||
|
|
||
| /** | ||
| * Protected function added so that cache lifetime is overwritable | ||
| * @return int | ||
| */ | ||
| protected function getCacheLifetime(): int | ||
| { | ||
| return self::CACHE_LIFETIME; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Tweakwise\AttributeLandingTweakwise\Controller\Adminhtml\Ajax; | ||
|
|
||
| use Magento\Framework\App\Action\HttpPostActionInterface; | ||
| use Magento\Framework\Exception\LocalizedException; | ||
| use Magento\Store\Model\Store; | ||
| use Magento\Store\Model\StoreManagerInterface; | ||
| use Tweakwise\AttributeLandingTweakwise\ApiClient\BackendApiClient; | ||
| use Tweakwise\AttributeLandingTweakwise\Model\Config; | ||
|
|
||
| abstract class AbstractFacetController implements HttpPostActionInterface | ||
| { | ||
| public const OTHER_ATTRIBUTE_VALUE = 'tw_other'; | ||
|
|
||
| /** | ||
| * @param Config $config | ||
| * @param StoreManagerInterface $storeManager | ||
| * @param BackendApiClient $backendApiClient | ||
| */ | ||
| public function __construct( | ||
| private readonly Config $config, | ||
| protected readonly StoreManagerInterface $storeManager, | ||
| protected readonly BackendApiClient $backendApiClient, | ||
| ) { | ||
| } | ||
|
|
||
| /** | ||
| * @param Store|null $store | ||
| * @return bool | ||
| * @throws LocalizedException | ||
| */ | ||
| protected function isBackendApiEnabled(?Store $store = null): bool | ||
| { | ||
| return (bool)$this->config->getBackendApiToken($store); | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.