File tree 2 files changed +37
-0
lines changed
src/Symfony/Component/Notifier/Bridge/Slack 2 files changed +37
-0
lines changed Original file line number Diff line number Diff line change 14
14
use Symfony \Component \Notifier \Bridge \Slack \Block \SlackBlockInterface ;
15
15
use Symfony \Component \Notifier \Bridge \Slack \Block \SlackDividerBlock ;
16
16
use Symfony \Component \Notifier \Bridge \Slack \Block \SlackSectionBlock ;
17
+ use Symfony \Component \Notifier \Exception \LogicException ;
17
18
use Symfony \Component \Notifier \Message \MessageOptionsInterface ;
18
19
use Symfony \Component \Notifier \Notification \Notification ;
19
20
22
23
*/
23
24
final class SlackOptions implements MessageOptionsInterface
24
25
{
26
+ private const MAX_BLOCKS = 50 ;
27
+
25
28
private $ options ;
26
29
27
30
public function __construct (array $ options = [])
28
31
{
29
32
$ this ->options = $ options ;
33
+
34
+ $ this ->validateNumberOfBlocks ();
30
35
}
31
36
32
37
public static function fromNotification (Notification $ notification ): self
@@ -97,6 +102,8 @@ public function asUser(bool $bool): self
97
102
*/
98
103
public function block (SlackBlockInterface $ block ): self
99
104
{
105
+ $ this ->validateNumberOfBlocks ();
106
+
100
107
$ this ->options ['blocks ' ][] = $ block ->toArray ();
101
108
102
109
return $ this ;
@@ -191,4 +198,11 @@ public function threadTs(string $threadTs): self
191
198
192
199
return $ this ;
193
200
}
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
+ }
194
208
}
Original file line number Diff line number Diff line change 14
14
use PHPUnit \Framework \TestCase ;
15
15
use Symfony \Bridge \PhpUnit \ExpectDeprecationTrait ;
16
16
use Symfony \Component \Notifier \Bridge \Slack \Block \SlackDividerBlock ;
17
+ use Symfony \Component \Notifier \Bridge \Slack \Block \SlackSectionBlock ;
17
18
use Symfony \Component \Notifier \Bridge \Slack \SlackOptions ;
19
+ use Symfony \Component \Notifier \Exception \LogicException ;
18
20
use Symfony \Component \Notifier \Notification \Notification ;
19
21
20
22
/**
@@ -187,4 +189,25 @@ public function fromNotificationProvider(): iterable
187
189
(new Notification ($ subject ))->emoji ($ emoji )->content ($ content ),
188
190
];
189
191
}
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
+ }
190
213
}
You can’t perform that action at this time.
0 commit comments