Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
MailTrackingRecord
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
2 / 2
8
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
 fromDatabaseRow
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
1 / 1
7
1<?php
2
3declare(strict_types=1);
4
5/** @license For full copyright and license information, please see the LICENSE.md file. */
6
7namespace App\Modules\Mail\Domain\Model;
8
9defined('AMMONLY_APP') || exit('Direct script access is forbidden.');
10
11/**
12 * Immutable Mail Tracking Aggregate Model.
13 *
14 * Encapsulates email open metrics, recipient engagement, and click-through analytics.
15 *
16 * @package App\Modules\Mail\Domain\Model
17 */
18final readonly class MailTrackingRecord
19{
20    /**
21     * MailTrackingRecord constructor.
22     *
23     * @param int         $id             Primary key ID.
24     * @param string      $trackingToken  Unique cryptographic tracking token.
25     * @param int|null    $mailQueueId    Associated queue message ID.
26     * @param string      $recipientEmail Target recipient email address.
27     * @param bool        $isOpened       Whether the open tracking pixel was triggered.
28     * @param string|null $openedAt       Timestamp of the first open.
29     * @param int         $openCount      Total number of times opened.
30     * @param int         $clicksCount    Total count of link clicks.
31     * @param string|null $lastClickedAt  Timestamp of the most recent click.
32     * @param string|null $lastClickUrl   Target URL of the most recent click.
33     * @param string|null $ipAddress      IP address of the recipient client.
34     * @param string|null $userAgent      User-agent string of the email client.
35     * @param string      $createdAt      Record creation timestamp.
36     */
37    public function __construct(
38        public int $id,
39        public string $trackingToken,
40        public ?int $mailQueueId,
41        public string $recipientEmail,
42        public bool $isOpened = false,
43        public ?string $openedAt = null,
44        public int $openCount = 0,
45        public int $clicksCount = 0,
46        public ?string $lastClickedAt = null,
47        public ?string $lastClickUrl = null,
48        public ?string $ipAddress = null,
49        public ?string $userAgent = null,
50        public string $createdAt = ''
51    ) {
52    }
53
54    /**
55     * Creates model instance from database row array.
56     *
57     * @param array<string, mixed> $data Raw database row.
58     * @return self Populated tracking record model.
59     */
60    public static function fromDatabaseRow(array $data): self
61    {
62        return new self(
63            id: (int)($data['id'] ?? 0),
64            trackingToken: (string)($data['tracking_token'] ?? ''),
65            mailQueueId: isset($data['mail_queue_id']) ? (int)$data['mail_queue_id'] : null,
66            recipientEmail: (string)($data['recipient_email'] ?? ''),
67            isOpened: (bool)($data['is_opened'] ?? false),
68            openedAt: isset($data['opened_at']) ? (string)$data['opened_at'] : null,
69            openCount: (int)($data['open_count'] ?? 0),
70            clicksCount: (int)($data['clicks_count'] ?? 0),
71            lastClickedAt: isset($data['last_clicked_at']) ? (string)$data['last_clicked_at'] : null,
72            lastClickUrl: isset($data['last_click_url']) ? (string)$data['last_click_url'] : null,
73            ipAddress: isset($data['ip_address']) ? (string)$data['ip_address'] : null,
74            userAgent: isset($data['user_agent']) ? (string)$data['user_agent'] : null,
75            createdAt: (string)($data['created_at'] ?? '')
76        );
77    }
78}