Skip to content

Commit 26126bf

Browse files
committed
Restore private key provider
1 parent 7333090 commit 26126bf

5 files changed

Lines changed: 55 additions & 3 deletions

File tree

UPGRADE.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ In this version, we have updated the plugin to be compatible with version 2 of S
66
- The route `@WebgriffeSyliusClerkPlugin/config/feed_routing.php` has been renamed to `@WebgriffeSyliusClerkPlugin/config/routes/feed.php`.
77
- The file `@WebgriffeSyliusClerkPlugin/config/config.yaml` has been renamed to `@WebgriffeSyliusClerkPlugin/config/config.php`.
88
- Various deprecated classes and their services have been removed. Please migrate to use the new feed v2 generation and the new API key providers as described in the documentation of the plugin.
9-
- The private_api_key configuration key under stores has been removed.
109

1110
## From 2.x to 3.x
1211

src/DependencyInjection/Configuration.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ public function getConfigTreeBuilder(): TreeBuilder
3030
->scalarNode('public_api_key')
3131
->isRequired()
3232
->end()
33+
->scalarNode('private_api_key')
34+
->isRequired()
35+
->end()
3336
->scalarNode('locale_code')
3437
->defaultNull()
3538
->end()

src/Provider/ApiKeysProvider.php

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
* @param array<array-key, array{
1414
* channel_code: string,
1515
* public_api_key: string,
16+
* private_api_key: string,
1617
* locale_code?: string,
1718
* }> $storesConfiguration
1819
*/
@@ -36,13 +37,13 @@ public function getPublicApiKey(
3637
}
3738
$atLeastOneConfigurationHasLocaleCode = array_reduce(
3839
$channelsConfiguration,
39-
fn (bool $carry, array $storeConfiguration) => $carry || array_key_exists('locale_code', $storeConfiguration),
40+
static fn (bool $carry, array $storeConfiguration) => $carry || array_key_exists('locale_code', $storeConfiguration),
4041
false,
4142
);
4243
if ($atLeastOneConfigurationHasLocaleCode) {
4344
$channelLocalesConfiguration = array_filter(
4445
$channelsConfiguration,
45-
fn (array $storeConfiguration) => array_key_exists('locale_code', $storeConfiguration) && $storeConfiguration['locale_code'] === $localeCode,
46+
static fn (array $storeConfiguration) => array_key_exists('locale_code', $storeConfiguration) && $storeConfiguration['locale_code'] === $localeCode,
4647
);
4748
if (count($channelLocalesConfiguration) === 0) {
4849
throw new ChannelApiKeysNotProvidedException('Configuration not found for channel ' . $channelCode . ' and locale ' . $localeCode);
@@ -59,4 +60,43 @@ public function getPublicApiKey(
5960

6061
return reset($channelsConfiguration)['public_api_key'];
6162
}
63+
64+
#[\Override]
65+
public function getPrivateApiKey(
66+
ChannelInterface $channel,
67+
string $localeCode,
68+
): string {
69+
$channelsConfiguration = array_filter(
70+
$this->storesConfiguration,
71+
static fn (array $storeConfiguration) => $storeConfiguration['channel_code'] === (string) $channel->getCode(),
72+
);
73+
$channelCode = (string) $channel->getCode();
74+
if (count($channelsConfiguration) === 0) {
75+
throw new ChannelApiKeysNotProvidedException('Configuration not found for channel ' . $channelCode);
76+
}
77+
$atLeastOneConfigurationHasLocaleCode = array_reduce(
78+
$channelsConfiguration,
79+
static fn (bool $carry, array $storeConfiguration) => $carry || array_key_exists('locale_code', $storeConfiguration),
80+
false,
81+
);
82+
if ($atLeastOneConfigurationHasLocaleCode) {
83+
$channelLocalesConfiguration = array_filter(
84+
$channelsConfiguration,
85+
static fn (array $storeConfiguration) => array_key_exists('locale_code', $storeConfiguration) && $storeConfiguration['locale_code'] === $localeCode,
86+
);
87+
if (count($channelLocalesConfiguration) === 0) {
88+
throw new ChannelApiKeysNotProvidedException('Configuration not found for channel ' . $channelCode . ' and locale ' . $localeCode);
89+
}
90+
if (count($channelLocalesConfiguration) > 1) {
91+
throw new ChannelApiKeysNotProvidedException('Multiple configurations found for channel ' . $channelCode . ' and locale ' . $localeCode);
92+
}
93+
94+
return reset($channelLocalesConfiguration)['private_api_key'];
95+
}
96+
if (count($channelsConfiguration) > 1) {
97+
throw new ChannelApiKeysNotProvidedException('Multiple configurations found for channel ' . $channelCode);
98+
}
99+
100+
return reset($channelsConfiguration)['private_api_key'];
101+
}
62102
}

src/Provider/ApiKeysProviderInterface.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,12 @@ public function getPublicApiKey(
1616
ChannelInterface $channel,
1717
string $localeCode,
1818
): string;
19+
20+
/**
21+
* @throws ChannelApiKeysNotProvidedException If the configuration for the given channel is not found.
22+
*/
23+
public function getPrivateApiKey(
24+
ChannelInterface $channel,
25+
string $localeCode,
26+
): string;
1927
}

tests/TestApplication/config/config.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@ webgriffe_sylius_clerk:
1515
stores:
1616
- channel_code: FASHION_WEB
1717
public_api_key: public-key
18+
private_api_key: 123abc
1819
locale_code: en_US
1920
- channel_code: WEB-US
2021
public_api_key: public-key
22+
private_api_key: 123abc
2123
locale_code: en_US
2224
pages:
2325
-

0 commit comments

Comments
 (0)