8000 Throw error if maximum block limit is reached for slack message options · symfony/symfony@4647f9c · GitHub
[go: up one dir, main page]

Skip to content

Commit 4647f9c

Browse files
committed
Throw error if maximum block limit is reached for slack message options
1 parent b5d4b33 commit 4647f9c

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

src/Symfony/Component/Notifier/Bridge/Slack/SlackOptions.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Symfony\Component\Notifier\Bridge\Slack\Block\SlackBlockInterface;
1515
use Symfony\Component\Notifier\Bridge\Slack\Block\SlackDividerBlock;
1616
use Symfony\Component\Notifier\Bridge\Slack\Block\SlackSectionBlock;
17+
use Symfony\Component\Notifier\Exception\LogicException;
1718
use Symfony\Component\Notifier\Message\MessageOptionsInterface;
1819
use Symfony\Component\Notifier\Notification\Notification;
1920

@@ -22,11 +23,15 @@
2223
*/
2324
final class SlackOptions implements MessageOptionsInterface
2425
{
26+
private const MAX_BLOCKS = 50;
27+
2528
private $options;
2629

2730
public function __construct(array $options = [])
2831
{
2932
$this->options = $options;
33+
34+
$this->validateNumberOfBlocks();
3035
}
3136

3237
public static function fromNotification(Notification $notification): self
@@ -97,6 +102,8 @@ public function asUser(bool $bool): self
97102
*/
98103
public function block(SlackBlockInterface $block): self
99104
{
105+
$this->validateNumberOfBlocks();
106+
100107
$this->options['blocks'][] = $block->toArray();
101108

102109
return $this;
@@ -191,4 +198,11 @@ public function threadTs(string $threadTs): self
191198

192199
return $this;
193200
}
201+
202+
private function validateNumberOfBlocks(): void
203+
{
204+
if (\count($this->options['blocks'] ?? []) >= self::MAX_BLOCKS) {
205+
throw new LogicException(sprintf('Maximum number of "blocks" has been reached (%d).', self::MAX_BLOCKS));
206+
}
207+
}
194208
}

src/Symfony/Component/Notifier/Bridge/Slack/Tests/SlackOptionsTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
1616
use Symfony\Component\Notifier\Bridge\Slack\Block\SlackDividerBlock;
17+
use Symfony\Component\Notifier\Bridge\Slack\Block\SlackSectionBlock;
1718
use Symfony\Component\Notifier\Bridge\Slack\SlackOptions;
19+
use Symfony\Component\Notifier\Exception\LogicException;
1820
use Symfony\Component\Notifier\Notification\Notification;
1921

2022
/**
@@ -187,4 +189,25 @@ public function fromNotificationProvider(): iterable
187189
(new Notification($subject))->emoji($emoji)->content($content),
188190
];
189191
}
192+
193+
public function testConstructThrowsWithTooManyBlocks()
194+
{
195+
$this->expectException(LogicException::class);
196+
$this->expectExceptionMessage('Maximum number of "blocks" has been reached (50).');
197+
198+
new SlackOptions(['blocks' => array_map(static function () { return ['type' => 'divider']; }, range(0, 50))]);
199+
}
200+
201+
public function testThrowsWhenBlocksLimitReached()
202+
{
203+
$options = new SlackOptions();
204+
for ($i = 0; $i < 50; ++$i) {
205+
$options->block(new SlackSectionBlock());
206+
}
207+
208+
$this->expectException(LogicException::class);
209+
$this->expectExceptionMessage('Maximum number of "blocks" has been reached (50).');
210+
211+
$options->block(new SlackSectionBlock());
212+
}
190213
}

0 commit comments

Comments
 (0)
0