This repository has been archived by the owner on Feb 9, 2023. It is now read-only.
/
ListOptionsProvider.php
75 lines (64 loc) · 1.92 KB
/
ListOptionsProvider.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<?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));
}
}