Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[Notifier] Add ContextBlock for slack notifier #39258

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be removed as we don't use it in Symfony.


namespace Symfony\Component\Notifier\Bridge\Slack\Block;

final class SlackContextBlock extends AbstractSlackBlock
{
private const ELEMENT_LIMIT = 10;

public function __construct()
{
$this->options['type'] = 'context';
}

public function text(string $text, bool $markdown = true, bool $emoji = true, bool $verbatim = false): self
{
if (self::ELEMENT_LIMIT === \count($this->options['elements'] ?? [])) {
throw new \LogicException(sprintf('Maximum number of elements should not exceed %d.', self::ELEMENT_LIMIT));
}

$element = [
'type' => $markdown ? 'mrkdwn' : 'plain_text',
'text' => $text,
];
if ($markdown) {
$element['verbatim'] = $verbatim;
norkunas marked this conversation as resolved.
Show resolved Hide resolved
} else {
$element['emoji'] = $emoji;
}
$this->options['elements'][] = $element;

return $this;
}

public function image(string $url, string $text): self
{
if (self::ELEMENT_LIMIT === \count($this->options['elements'] ?? [])) {
throw new \LogicException(sprintf('Maximum number of elements should not exceed %d.', self::ELEMENT_LIMIT));
}

$this->options['elements'][] = [
'type' => 'image',
'image_url' => $url,
'alt_text' => $text,
];

return $this;
}

public function id(string $id): self
{
$this->options['block_id'] = $id;

return $this;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Notifier\Bridge\Slack\Tests\Block;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Notifier\Bridge\Slack\Block\SlackContextBlock;

final class SlackContextBlockTest extends TestCase
{
public function testCanBeInstantiated(): void
{
$context = new SlackContextBlock();
$context->text('context text without emoji', false, false);
$context->text('context text verbatim', true, false, true);
$context->image('https://example.com/image.jpg', 'an image');
$context->id('context_id');

$this->assertSame([
'type' => 'context',
'elements' => [
[
'type' => 'plain_text',
'text' => 'context text without emoji',
'emoji' => false,
],
[
'type' => 'mrkdwn',
'text' => 'context text verbatim',
'verbatim' => true,
],
[
'type' => 'image',
'image_url' => 'https://example.com/image.jpg',
'alt_text' => 'an image',
],
],
'block_id' => 'context_id',
], $context->toArray());
}

public function testThrowsWhenElementsLimitReached(): void
{
$context = new SlackContextBlock();
for ($i = 0; $i < 10; ++$i) {
if (0 === $i % 2) {
$context->text($i);
} else {
$context->image($i, $i);
}
}

$this->expectException(\LogicException::class);
$this->expectExceptionMessage('Maximum number of elements should not exceed 10.');

$context->text('fail');
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.