Skip to content

Commit fdcc377

Browse files
Slavomir Sidordrupol
authored andcommitted
Multiple group code configuration.
1 parent 0e3c06c commit fdcc377

5 files changed

Lines changed: 22 additions & 5 deletions

File tree

src/DependencyInjection/Configuration.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ public function getConfigTreeBuilder(): TreeBuilder
2626
->scalarNode('base_url')->isRequired()->cannotBeEmpty()->end()
2727
->scalarNode('system_key')->isRequired()->cannotBeEmpty()->end()
2828
->scalarNode('system_password')->isRequired()->cannotBeEmpty()->end()
29-
->scalarNode('group_code')->isRequired()->cannotBeEmpty()->end();
29+
->arrayNode('group_code')
30+
->scalarPrototype()->end()
31+
->isRequired()->cannotBeEmpty()->end();
3032

3133
return $treeBuilder;
3234
}

src/Exception/NotificationException.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,9 @@ public static function statusCodeError(int $statusCode): self
4545
sprintf('Wrong status code from CNS: %s', $statusCode)
4646
);
4747
}
48+
49+
public static function unknownGroupCode(string $groupCode): self
50+
{
51+
return new self(sprintf('Unknown gropu code: \'%s\'', $groupCode));
52+
}
4853
}

src/Service/NotificationService.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ final class NotificationService implements NotificationServiceInterface
2828
{
2929
private const API_ENDPOINT_PATTERN = '%s/v1/notifications?clientSystemKey=%s&clientSystemPassword=%s';
3030

31+
public const GROUP_CODE_DEFAULT = 'default';
32+
3133
/**
3234
* @var array<string, string>
3335
*/
@@ -43,8 +45,13 @@ public function __construct(
4345
$this->configuration = $parameterBag->get('cns_client');
4446
}
4547

46-
public function send(NotificationInterface $notification): int
48+
public function send(NotificationInterface $notification, string $groupCode = self::GROUP_CODE_DEFAULT): int
4749
{
50+
if(!array_key_exists($groupCode, $this->configuration['group_code']))
51+
{
52+
throw NotificationException::unknownGroupCode($groupCode);
53+
}
54+
4855
try {
4956
$response = $this
5057
->httpClient
@@ -58,7 +65,7 @@ public function send(NotificationInterface $notification): int
5865
),
5966
[
6067
'json' => [
61-
'notificationGroupCode' => $this->configuration['group_code'],
68+
'notificationGroupCode' => $this->configuration['group_code'][$groupCode],
6269
'recipients' => $notification->getRecipients(),
6370
'defaultContent' => $notification->getContent(),
6471
],

src/Service/NotificationServiceInterface.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,8 @@ interface NotificationServiceInterface
1919
/**
2020
* @throws NotificationException
2121
*/
22-
public function send(NotificationInterface $notification): int;
22+
public function send(
23+
NotificationInterface $notification,
24+
string $groupCode = NotificationService::GROUP_CODE_DEFAULT
25+
): int;
2326
}

tests/Service/NotificationServiceTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ private function getParameterBag(): ParameterBagInterface
209209
->method('get')
210210
->with('cns_client')
211211
->willReturn([
212-
'group_code' => 'group_code',
212+
'group_code' => ['default' => 'group_code'],
213213
'base_url' => 'https://example.com',
214214
'system_key' => 'system_key',
215215
'system_password' => 'system_password',

0 commit comments

Comments
 (0)