Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
SendMailMessage
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
3 / 3
5
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 toArray
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
 fromArray
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2
3/**
4 * Message for queued mail delivery.
5 *
6 * This Source Code Form is subject to the terms of the Mozilla Public License,
7 * v. 2.0. If a copy of the MPL was not distributed with this file, You can
8 * obtain one at https://mozilla.org/MPL/2.0/.
9 *
10 * @package   phpMyFAQ
11 * @author    Thorsten Rinne <thorsten@phpmyfaq.de>
12 * @copyright 2026 phpMyFAQ Team
13 * @license   https://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
14 * @link      https://www.phpmyfaq.de
15 * @since     2026-02-11
16 */
17
18declare(strict_types=1);
19
20namespace phpMyFAQ\Queue\Message;
21
22final readonly class SendMailMessage implements QueueMessageInterface
23{
24    /**
25     * @param array<string, mixed> $metadata
26     */
27    public function __construct(
28        public string $recipient,
29        public string $subject,
30        public string $body,
31        public array $metadata = [],
32    ) {
33    }
34
35    public function toArray(): array
36    {
37        return [
38            'recipient' => $this->recipient,
39            'subject' => $this->subject,
40            'body' => $this->body,
41            'metadata' => $this->metadata,
42        ];
43    }
44
45    public static function fromArray(array $payload): self
46    {
47        $rawMetadata = is_array($payload['metadata'] ?? null) ? $payload['metadata'] : [];
48        $metadata = [];
49        foreach ($rawMetadata as $metadataKey => $metadataValue) {
50            $metadata[(string) $metadataKey] = $metadataValue;
51        }
52
53        return new self(
54            recipient: (string) ($payload['recipient'] ?? ''),
55            subject: (string) ($payload['subject'] ?? ''),
56            body: (string) ($payload['body'] ?? ''),
57            metadata: $metadata,
58        );
59    }
60}