This repository has been archived by the owner on Feb 9, 2023. It is now read-only.
Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
pimcore-mailchimp/Util/ListOptionsProvider.php
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
75 lines (64 sloc)
1.92 KB
This file contains 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
<?php | |
namespace Wgg\MailchimpBundle\Util; | |
use Pimcore\Cache; | |
use Pimcore\Model\DataObject\ClassDefinition\DynamicOptionsProvider\MultiSelectOptionsProviderInterface; | |
use Pimcore\Model\DataObject\ClassDefinition\DynamicOptionsProvider\SelectOptionsProviderInterface; | |
use Wgg\MailchimpBundle\WggMailchimpBundle; | |
use function implode; | |
use function md5; | |
class ListOptionsProvider implements MultiSelectOptionsProviderInterface, SelectOptionsProviderInterface | |
{ | |
private ApiClient $apiClient; | |
public function __construct(ApiClient $apiClient) | |
{ | |
$this->apiClient = $apiClient; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function getOptions($context, $fieldDefinition): array | |
{ | |
return $this->getRawOptions(); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function hasStaticOptions($context, $fieldDefinition): bool | |
{ | |
return false; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function getDefaultValue($context, $fieldDefinition) | |
{ | |
return null; | |
} | |
/** | |
* @return array<array{key: string, value: string}> | |
*/ | |
public function getRawOptions(): array | |
{ | |
$data = []; | |
$listIds = $this->apiClient->getListIds(); | |
if ($listIds) { | |
$cacheKey = $this->getCacheKey($listIds); | |
if (!$data = Cache::load($cacheKey)) { | |
Cache::clearTag(WggMailchimpBundle::CACHE_TAG); | |
foreach ($listIds as $listId) { | |
$list = $this->apiClient->lists->getList($listId, 'name,id'); | |
$data[] = [ | |
'key' => $list->name, | |
'value' => $list->id, | |
]; | |
} | |
Cache::save($data, $cacheKey, [WggMailchimpBundle::CACHE_TAG]); | |
} | |
} | |
return $data; | |
} | |
private function getCacheKey(array $listIds): string | |
{ | |
return md5(implode('|', $listIds)); | |
} | |
} |