Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
95.65% covered (success)
95.65%
22 / 23
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
MailSmtp
100.00% covered (success)
100.00%
22 / 22
100.00% covered (success)
100.00%
2 / 2
6
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
 fromRow
100.00% covered (success)
100.00%
21 / 21
100.00% covered (success)
100.00%
1 / 1
5
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 * SMTP Server Configuration Aggregate Model.
13 *
14 * @package App\Modules\Mail\Domain\Model
15 */
16final readonly class MailSmtp
17{
18    /**
19     * MailSmtp constructor.
20     *
21     * @param int         $id             Primary ID.
22     * @param string      $name           Server display name.
23     * @param string      $host           Host address.
24     * @param int         $port           Port number (25, 465, 587).
25     * @param string      $security       Security protocol (none, tls, ssl, starttls).
26     * @param string      $authMethod     Authentication method (login, plain, cram-md5).
27     * @param string      $username       SMTP login username.
28     * @param string      $password       SMTP password.
29     * @param string      $fromEmail      Default sender email.
30     * @param string      $fromName       Default sender name.
31     * @param string|null $replyToEmail   Optional reply-to email.
32     * @param int         $timeoutSeconds Socket timeout in seconds.
33     * @param bool        $isDefault      Whether server is system default.
34     * @param bool        $isActive       Whether server is active.
35     */
36    public function __construct(
37        public int $id,
38        public string $name,
39        public string $host,
40        public int $port,
41        public string $security,
42        public string $authMethod,
43        public string $username,
44        public string $password,
45        public string $fromEmail,
46        public string $fromName,
47        public ?string $replyToEmail = null,
48        public int $timeoutSeconds = 30,
49        public bool $isDefault = false,
50        public bool $isActive = true,
51        public ?int $mailServerId = null
52    ) {
53    }
54
55    /**
56     * Instantiates MailSmtp from database row.
57     *
58     * @param array<string, mixed> $row Database associative record.
59     * @return self Populated MailSmtp instance.
60     */
61    public static function fromRow(array $row): self
62    {
63        return new self(
64            id: (int)($row['id'] ?? 0),
65            name: (string)($row['name'] ?? ''),
66            host: (string)($row['host'] ?? ''),
67            port: (int)($row['port'] ?? 587),
68            security: (string)($row['security'] ?? 'tls'),
69            authMethod: (string)($row['auth_method'] ?? 'login'),
70            username: (string)($row['username'] ?? ''),
71            password: (string)($row['password'] ?? ''),
72            fromEmail: (string)($row['from_email'] ?? ''),
73            fromName: (string)($row['from_name'] ?? ''),
74            replyToEmail: isset($row['reply_to_email']) && (string)$row['reply_to_email'] !== ''
75                ? (string)$row['reply_to_email']
76                : null,
77            timeoutSeconds: (int)($row['timeout_seconds'] ?? 30),
78            isDefault: (bool)($row['is_default'] ?? false),
79            isActive: (bool)($row['is_active'] ?? true),
80            mailServerId: isset($row['mail_server_id']) && $row['mail_server_id'] !== ''
81                ? (int)$row['mail_server_id']
82                : null
83        );
84    }
85}