Skip to content

Commit 33811bf

Browse files
committed
Update: config provider
1 parent 3bcf040 commit 33811bf

File tree

6 files changed

+54
-14
lines changed

6 files changed

+54
-14
lines changed

src/Domain/Configuration/Model/ConfigOption.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,12 @@
77
enum ConfigOption: string
88
{
99
case MaintenanceMode = 'maintenancemode';
10-
case SendSubscribeMessage = 'subscribemessage';
10+
case SubscribeMessage = 'subscribemessage';
11+
case SubscribeEmailSubject = 'subscribesubject';
12+
case UnsubscribeUrl = 'unsubscribeurl';
13+
case ConfirmationUrl = 'confirmationurl';
14+
case PreferencesUrl = 'preferencesurl';
15+
case SubscribeUrl = 'subscribeurl';
16+
case Domain = 'domain';
17+
case Website = 'website';
1118
}

src/Domain/Configuration/Repository/ConfigRepository.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,8 @@
88

99
class ConfigRepository extends AbstractRepository
1010
{
11+
public function findValueByItem(string $name): ?string
12+
{
13+
return $this->findOneBy(['item' => $name])?->getValue();
14+
}
1115
}

src/Domain/Configuration/Service/Provider/ConfigProvider.php

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,19 @@
77
use InvalidArgumentException;
88
use PhpList\Core\Domain\Configuration\Model\ConfigOption;
99
use PhpList\Core\Domain\Configuration\Repository\ConfigRepository;
10+
use Psr\SimpleCache\CacheInterface;
1011

1112
class ConfigProvider
1213
{
1314
private array $booleanValues = [
1415
ConfigOption::MaintenanceMode,
15-
ConfigOption::SendSubscribeMessage,
1616
];
1717

18-
private ConfigRepository $configRepository;
19-
20-
public function __construct(ConfigRepository $configRepository)
21-
{
22-
$this->configRepository = $configRepository;
18+
public function __construct(
19+
private readonly ConfigRepository $configRepository,
20+
private readonly CacheInterface $cache,
21+
private readonly int $ttlSeconds = 300
22+
) {
2323
}
2424

2525
public function isEnabled(ConfigOption $key): bool
@@ -35,11 +35,35 @@ public function isEnabled(ConfigOption $key): bool
3535
/**
3636
* Get configuration value by its key
3737
*/
38-
public function getValue(string $ikey, ?string $default = null): ?string
38+
public function getValue(ConfigOption $key, ?string $default = null): ?string
3939
{
40-
$config = $this->configRepository->findOneBy(['item' => $ikey]);
40+
if (in_array($key, $this->booleanValues)) {
41+
throw new InvalidArgumentException('Key is a boolean value, use isEnabled instead');
42+
}
43+
$cacheKey = 'cfg:' . $key->value;
44+
$value = $this->cache->get($cacheKey);
45+
if ($value === null) {
46+
$value = $this->configRepository->findValueByItem($key->value);
47+
$this->cache->set($cacheKey, $value, $this->ttlSeconds);
48+
}
4149

42-
return $config?->getValue() ?? $default;
50+
return $value ?? $default;
4351
}
4452

53+
public function getValueWithNamespace(ConfigOption $key, ?string $default = null): ?string
54+
{
55+
$full = $this->getValue($key);
56+
if ($full !== null && $full !== '') {
57+
return $full;
58+
}
59+
60+
if (str_contains($key->value, ':')) {
61+
[$parent] = explode(':', $key->value, 2);
62+
$parentKey = ConfigOption::from($parent);
63+
64+
return $this->getValue($parentKey, $default);
65+
}
66+
67+
return $default;
68+
}
4569
}

src/Domain/Subscription/Model/Dto/SubscriberImportOptions.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ public function __construct(
1414
public readonly array $listIds = [],
1515
public readonly bool $dryRun = false,
1616
public readonly bool $skipInvalidEmail = true,
17+
public readonly bool $notifySubscribers = false,
1718
) {
1819
}
1920
}

src/Domain/Subscription/Service/Manager/SubscriptionManager.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,12 @@ public function __construct(
3232
$this->translator = $translator;
3333
}
3434

35-
public function addSubscriberToAList(Subscriber $subscriber, int $listId): Subscription
35+
public function addSubscriberToAList(Subscriber $subscriber, int $listId): ?Subscription
3636
{
3737
$existingSubscription = $this->subscriptionRepository
3838
->findOneBySubscriberEmailAndListId($listId, $subscriber->getEmail());
3939
if ($existingSubscription) {
40-
return $existingSubscription;
40+
return null;
4141
}
4242
$subscriberList = $this->subscriberListRepository->find($listId);
4343
if (!$subscriberList) {

src/Domain/Subscription/Service/SubscriberCsvImporter.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,15 +185,19 @@ private function processRow(
185185

186186
$this->processAttributes($subscriber, $dto);
187187

188+
$addedNewSubscriberToList = false;
188189
if (!$subscriber->isBlacklisted() && count($options->listIds) > 0) {
189190
foreach ($options->listIds as $listId) {
190-
$this->subscriptionManager->addSubscriberToAList($subscriber, $listId);
191+
$created = $this->subscriptionManager->addSubscriberToAList($subscriber, $listId);
192+
if ($created) {
193+
$addedNewSubscriberToList = true;
194+
}
191195
}
192196
}
193197

194198
if (!$options->dryRun) {
195199
$this->entityManager->flush();
196-
if ($this->configProvider->isEnabled(ConfigOption::SendSubscribeMessage)) {
200+
if ($options->notifySubscribers && $addedNewSubscriberToList) {
197201
$this->sendSubscribeEmail($dto->email, $options->listIds);
198202
}
199203
}

0 commit comments

Comments
 (0)