Skip to content

Commit

Permalink
Ability to provide channel to formatters. (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
rmikalkenas authored Feb 28, 2020
1 parent 7a14db7 commit 8711aae
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 4 deletions.
19 changes: 17 additions & 2 deletions src/Formatter/AbstractSlackAttachmentFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,28 @@ abstract class AbstractSlackAttachmentFormatter extends NormalizerFormatter impl
*/
private $includeContextAndExtra;

/**
* @var string|null
*/
private $channel;

/**
* @param string|null $username The username of the bot.
* @param string|null $emoji
* @param bool $includeContextAndExtra
* @param string|null $channel
*/
public function __construct(?string $username = null, ?string $emoji = null, bool $includeContextAndExtra = true)
{
public function __construct(
?string $username = null,
?string $emoji = null,
bool $includeContextAndExtra = true,
?string $channel = null
) {
parent::__construct();
$this->username = $username;
$this->emoji = $emoji !== null ? trim($emoji, ':') : null;
$this->includeContextAndExtra = $includeContextAndExtra;
$this->channel = $channel;
}

/**
Expand All @@ -64,6 +75,10 @@ public function format(array $record): array
$data['icon_emoji'] = sprintf(':%s:', $this->emoji);
}

if ($this->channel !== null) {
$data['channel'] = $this->channel;
}

$attachment = [
'fallback' => $record['message'],
'text' => $record['message'],
Expand Down
19 changes: 17 additions & 2 deletions src/Formatter/SlackLineFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,29 @@ final class SlackLineFormatter extends NormalizerFormatter implements SlackForma
*/
private $lineFormatter;

/**
* @var string|null
*/
private $channel;

/**
* @param string|null $username
* @param string|null $emoji
* @param string|null $format
* @param string|null $channel
*/
public function __construct(?string $username = null, ?string $emoji = null, ?string $format = null)
{
public function __construct(
?string $username = null,
?string $emoji = null,
?string $format = null,
?string $channel = null
) {
parent::__construct();
$format = $format ?: '%channel%.%level_name%: %message% %context% %extra%';
$this->lineFormatter = new LineFormatter($format, null, false, true);
$this->username = $username;
$this->emoji = $emoji !== null ? trim($emoji, ':') : null;
$this->channel = $channel;
}

/**
Expand All @@ -63,6 +74,10 @@ public function format(array $record): array
$data['icon_emoji'] = sprintf(':%s:', $this->emoji);
}

if ($this->channel !== null) {
$data['channel'] = $this->channel;
}

return $data;
}
}
17 changes: 17 additions & 0 deletions tests/Unit/Formatter/SlackLineFormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,21 @@ public function correctEmojiProvider()
[':warning:', ':warning:'],
];
}

public function testNoChannel()
{
$formatter = new SlackLineFormatter(null, null, null);
$data = $formatter->format($this->getRecord());

$this->assertArrayNotHasKey('channel', $data);
}

public function testHasChannel()
{
$formatter = new SlackLineFormatter(null, null, null, 'my-slack-channel');
$data = $formatter->format($this->getRecord());

$this->assertArrayHasKey('channel', $data);
$this->assertSame('my-slack-channel', $data['channel']);
}
}
17 changes: 17 additions & 0 deletions tests/Unit/Formatter/SlackLongAttachmentFormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,23 @@ public function testAddsInternalFieldDoesNotExceed2000Characters()
$this->assertStringEndsWith('... (truncated)```', $data['attachments'][0]['fields'][0]['value']);
}

public function testAddsCustomChannel()
{
$formatter = new SlackLongAttachmentFormatter(null, null, true, 'my-slack-channel');
$data = $formatter->format($this->getRecord());

$this->assertArrayHasKey('channel', $data);
$this->assertSame('my-slack-channel', $data['channel']);
}

public function testChannelIsNotAddedByDefault()
{
$formatter = new SlackLongAttachmentFormatter();
$data = $formatter->format($this->getRecord());

$this->assertArrayNotHasKey('channel', $data);
}

/**
* @param string|null $username
* @param string $userIcon
Expand Down

0 comments on commit 8711aae

Please sign in to comment.