Skip to content

Commit a58cbd7

Browse files
committed
Add ContextBlock for slack notifier
1 parent ff97b5f commit a58cbd7

File tree

2 files changed

+142
-0
lines changed

2 files changed

+142
-0
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace Symfony\Component\Notifier\Bridge\Slack\Block;
15+
16+
final class SlackContextBlock extends AbstractSlackBlock
17+
{
18+
private const ELEMENT_LIMIT = 10;
19+
20+
public function __construct()
21+
{
22+
$this->options['type'] = 'context';
23+
}
24+
25+
public function text(string $text, bool $markdown = true, bool $emoji = true, bool $verbatim = false): self
26+
{
27+
if (self::ELEMENT_LIMIT === \count($this->options['elements'] ?? [])) {
28+
throw new \LogicException('Maximum number of elements should not exceed 10.');
29+
}
30+
31+
$element = [
32+
'type' => $markdown ? 'mrkdwn' : 'plain_text',
33+
'text' => $text,
34+
];
35+
if (!$markdown) {
36+
$element['emoji'] = $emoji;
37+
$element['verbatim'] = $verbatim;
38+
}
39+
$this->options['elements'][] = $element;
40+
41+
return $this;
42+
}
43+
44+
public function image(string $url, string $text): self
45+
{
46+
if (self::ELEMENT_LIMIT === \count($this->options['elements'] ?? [])) {
47+
throw new \LogicException('Maximum number of elements should not exceed 10.');
48+
}
49+
50+
$this->options['elements'][] = [
51+
'type' => 'image',
52+
'image_url' => $url,
53+
'alt_text' => $text,
54+
];
55+
56+
return $this;
57+
}
58+
59+
public function id(string $id): self
60+
{
61+
$this->options['block_id'] = $id;
62+
63+
return $this;
64+
}
65+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Notifier\Bridge\Slack\Tests\Block;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Notifier\Bridge\Slack\Block\SlackContextBlock;
16+
17+
final class SlackContextBlockTest extends TestCase
18+
{
19+
public function testCanBeInstantiated(): void
20+
{
21+
$context = new SlackContextBlock();
22+
$context->text('context text');
23+
$context->image('https://example.com/image.jpg', 'an image');
24+
$context->id('context_id');
25+
26+
$this->assertSame([
27+
'type' => 'context',
28+
'elements' => [
29+
[
30+
'type' => 'mrkdwn',
31+
'text' => 'context text',
32+
],
33+
[
34+
'type' => 'image',
35+
'image_url' => 'https://example.com/image.jpg',
36+
'alt_text' => 'an image',
37+
],
38+
],
39+
'block_id' => 'context_id',
40+
], $context->toArray());
41+
}
42+
43+
public function testSetsPropertiesForPlainText(): void
44+
{
45+
$context = new SlackContextBlock();
46+
$context->text('plain context text', false, false, true);
47+
48+
$this->assertSame([
49+
'type' => 'context',
50+
'elements' => [
51+
[
52+
'type' => 'plain_text',
53+
'text' => 'plain context text',
54+
'emoji' => false,
55+
'verbatim' => true,
56+
],
57+
],
58+
], $context->toArray());
59+
}
60+
61+
public function testThrowsWhenElementsLimitReached(): void
62+
{
63+
$context = new SlackContextBlock();
64+
for ($i = 0; $i < 10; ++$i) {
65+
if (0 === $i % 2) {
66+
$context->text($i);
67+
} else {
68+
$context->image($i, $i);
69+
}
70+
}
71+
72+
$this->expectException(\LogicException::class);
73+
$this->expectExceptionMessage('Maximum number of elements should not exceed 10.');
74+
75+
$context->text('fail');
76+
}
77+
}

0 commit comments

Comments
 (0)