Skip to content

Commit b7697f8

Browse files
committed
Fix review comments
1 parent 953482c commit b7697f8

File tree

4 files changed

+79
-188
lines changed

4 files changed

+79
-188
lines changed

src/Monolog/Handler/Teams/TeamsRecord.php renamed to src/Monolog/Handler/Teams/TeamsPayload.php

Lines changed: 20 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,10 @@
2222
*
2323
* @author Sébastien Alfaiate <[email protected]>
2424
* @see https://learn.microsoft.com/adaptive-cards/authoring-cards/getting-started
25+
*
26+
* @internal
2527
*/
26-
class TeamsRecord
28+
class TeamsPayload
2729
{
2830
public const COLOR_ATTENTION = 'attention';
2931

@@ -33,53 +35,34 @@ class TeamsRecord
3335

3436
public const COLOR_DEFAULT = 'default';
3537

36-
/**
37-
* Whether the card should include context and extra data
38-
*/
39-
private bool $includeContextAndExtra;
40-
41-
/**
42-
* Dot separated list of fields to exclude from MS Teams message. E.g. ['context.field1', 'extra.field2']
43-
* @var string[]
44-
*/
45-
private array $excludeFields;
46-
47-
/**
48-
* Dot separated list of fields to display with a toggle button in MS Teams message. E.g. ['context.field1', 'extra.field2']
49-
* @var string[]
50-
*/
51-
private array $toggleFields;
52-
53-
private FormatterInterface|null $formatter;
54-
5538
private NormalizerFormatter $normalizerFormatter;
5639

5740
/**
58-
* @param string[] $excludeFields
59-
* @param string[] $toggleFields
41+
* @param bool $includeContextAndExtra Whether the card should include context and extra data
42+
* @param bool $formatMessage Whether the message should be formatted
43+
* @param string[] $excludeFields Dot separated list of fields to exclude from MS Teams message. E.g. ['context.field1', 'extra.field2']
44+
* @param string[] $toggleFields Dot separated list of fields to display with a toggle button in MS Teams message. E.g. ['context.field1', 'extra.field2']
6045
*/
6146
public function __construct(
62-
bool $includeContextAndExtra = false,
63-
array $excludeFields = [],
64-
array $toggleFields = [],
65-
FormatterInterface|null $formatter = null
47+
private bool $includeContextAndExtra = false,
48+
private bool $formatMessage = false,
49+
private array $excludeFields = [],
50+
private array $toggleFields = [],
6651
) {
67-
$this
68-
->includeContextAndExtra($includeContextAndExtra)
69-
->excludeFields($excludeFields)
70-
->toggleFields($toggleFields)
71-
->setFormatter($formatter);
52+
if ($this->includeContextAndExtra) {
53+
$this->normalizerFormatter = new NormalizerFormatter();
54+
}
7255
}
7356

7457
/**
7558
* Returns required data in format that MS Teams is expecting.
7659
*
7760
* @phpstan-return mixed[]
7861
*/
79-
public function getAdaptiveCardPayload(LogRecord $record): array
62+
public function getAdaptiveCardPayload(LogRecord $record, ?FormatterInterface $formatter = null): array
8063
{
81-
if ($this->formatter !== null) {
82-
$message = $this->formatter->format($record);
64+
if ($formatter !== null && $this->formatMessage) {
65+
$message = $formatter->format($record);
8366
} else {
8467
$message = $record->message;
8568
}
@@ -150,7 +133,7 @@ public function getAdaptiveCardPayload(LogRecord $record): array
150133
/**
151134
* Returns MS Teams container style associated with provided level.
152135
*/
153-
public function getContainerStyle(Level $level): string
136+
private function getContainerStyle(Level $level): string
154137
{
155138
return match ($level) {
156139
Level::Error, Level::Critical, Level::Alert, Level::Emergency => static::COLOR_ATTENTION,
@@ -165,7 +148,7 @@ public function getContainerStyle(Level $level): string
165148
*
166149
* @param mixed[] $fields
167150
*/
168-
public function stringify(array $fields): string
151+
private function stringify(array $fields): string
169152
{
170153
/** @var array<array<mixed>|bool|float|int|string|null> $normalized */
171154
$normalized = $this->normalizerFormatter->normalizeValue($fields);
@@ -178,52 +161,6 @@ public function stringify(array $fields): string
178161
: Utils::jsonEncode($normalized, Utils::DEFAULT_JSON_FLAGS);
179162
}
180163

181-
/**
182-
* @return $this
183-
*/
184-
public function includeContextAndExtra(bool $includeContextAndExtra = false): self
185-
{
186-
$this->includeContextAndExtra = $includeContextAndExtra;
187-
188-
if ($this->includeContextAndExtra) {
189-
$this->normalizerFormatter = new NormalizerFormatter();
190-
}
191-
192-
return $this;
193-
}
194-
195-
/**
196-
* @param string[] $excludeFields
197-
* @return $this
198-
*/
199-
public function excludeFields(array $excludeFields = []): self
200-
{
201-
$this->excludeFields = $excludeFields;
202-
203-
return $this;
204-
}
205-
206-
/**
207-
* @param string[] $toggleFields
208-
* @return $this
209-
*/
210-
public function toggleFields(array $toggleFields = []): self
211-
{
212-
$this->toggleFields = $toggleFields;
213-
214-
return $this;
215-
}
216-
217-
/**
218-
* @return $this
219-
*/
220-
public function setFormatter(?FormatterInterface $formatter = null): self
221-
{
222-
$this->formatter = $formatter;
223-
224-
return $this;
225-
}
226-
227164
/**
228165
* Generates fact field
229166
*
@@ -308,6 +245,7 @@ private function generateContextAndExtraFields(array $data, string $type): array
308245
private function removeExcludedFields(LogRecord $record): array
309246
{
310247
$recordData = $record->toArray();
248+
311249
foreach ($this->excludeFields as $field) {
312250
$keys = explode('.', $field);
313251
$node = &$recordData;

src/Monolog/Handler/TeamsWebhookHandler.php

Lines changed: 8 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,9 @@
1111

1212
namespace Monolog\Handler;
1313

14-
use Monolog\Formatter\FormatterInterface;
1514
use Monolog\Level;
1615
use Monolog\Utils;
17-
use Monolog\Handler\Teams\TeamsRecord;
16+
use Monolog\Handler\Teams\TeamsPayload;
1817
use Monolog\LogRecord;
1918

2019
/**
@@ -34,9 +33,9 @@ class TeamsWebhookHandler extends AbstractProcessingHandler
3433
private string $webhookUrl;
3534

3635
/**
37-
* Instance of the TeamsRecord util class preparing data for MS Teams API.
36+
* Instance of the TeamsPayload util class preparing data for MS Teams API.
3837
*/
39-
private TeamsRecord $teamsRecord;
38+
private TeamsPayload $teamsPayload;
4039

4140
/**
4241
* @param non-empty-string $webhookUrl MS Teams Webhook URL
@@ -49,6 +48,7 @@ class TeamsWebhookHandler extends AbstractProcessingHandler
4948
public function __construct(
5049
string $webhookUrl,
5150
bool $includeContextAndExtra = false,
51+
bool $formatMessage = false,
5252
$level = Level::Critical,
5353
bool $bubble = true,
5454
array $excludeFields = [],
@@ -62,29 +62,20 @@ public function __construct(
6262

6363
$this->webhookUrl = $webhookUrl;
6464

65-
$this->teamsRecord = new TeamsRecord(
66-
$includeContextAndExtra,
67-
$excludeFields,
68-
$toggleFields
69-
);
65+
$this->teamsPayload = new TeamsPayload($includeContextAndExtra, $formatMessage, $excludeFields, $toggleFields);
7066
}
7167

72-
public function getTeamsRecord(): TeamsRecord
68+
public function getTeamsPayload(): TeamsPayload
7369
{
74-
return $this->teamsRecord;
75-
}
76-
77-
public function getWebhookUrl(): string
78-
{
79-
return $this->webhookUrl;
70+
return $this->teamsPayload;
8071
}
8172

8273
/**
8374
* @inheritDoc
8475
*/
8576
protected function write(LogRecord $record): void
8677
{
87-
$postData = $this->teamsRecord->getAdaptiveCardPayload($record);
78+
$postData = $this->teamsPayload->getAdaptiveCardPayload($record, $this->getFormatter());
8879
$postString = Utils::jsonEncode($postData);
8980

9081
$ch = curl_init();
@@ -100,20 +91,4 @@ protected function write(LogRecord $record): void
10091

10192
Curl\Util::execute($ch);
10293
}
103-
104-
public function setFormatter(FormatterInterface $formatter): HandlerInterface
105-
{
106-
parent::setFormatter($formatter);
107-
$this->teamsRecord->setFormatter($formatter);
108-
109-
return $this;
110-
}
111-
112-
public function getFormatter(): FormatterInterface
113-
{
114-
$formatter = parent::getFormatter();
115-
$this->teamsRecord->setFormatter($formatter);
116-
117-
return $formatter;
118-
}
11994
}

0 commit comments

Comments
 (0)