Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
92.00% covered (success)
92.00%
23 / 25
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
ClientMailTemplate
91.67% covered (success)
91.67%
22 / 24
50.00% covered (danger)
50.00%
1 / 2
8.04
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 fromRow
91.30% covered (success)
91.30%
21 / 23
0.00% covered (danger)
0.00%
0 / 1
7.03
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 * Client Business Email Template Aggregate Model.
13 *
14 * Encapsulates CRM and sales email templates configured in Client profile.
15 *
16 * @package App\Modules\Mail\Domain\Model
17 */
18final readonly class ClientMailTemplate
19{
20    /**
21     * ClientMailTemplate constructor.
22     *
23     * @param int                  $id                 Primary ID.
24     * @param string               $code               Template identifier.
25     * @param string               $name               Display title.
26     * @param string               $category           Category (sales, support, billing, general).
27     * @param string               $subject            Default subject.
28     * @param string               $bodyHtml           HTML template content.
29     * @param string|null          $bodyText           Plaintext fallback content.
30     * @param string               $languageCode       Language locale (pl, en).
31     * @param array<string, string> $availableVariables Available CRM placeholder tokens.
32     * @param bool                 $isActive           Active flag.
33     * @param int                  $owner              Owner user ID.
34     */
35    public function __construct(
36        public int $id,
37        public string $code,
38        public string $name,
39        public string $category,
40        public string $subject,
41        public string $bodyHtml,
42        public ?string $bodyText = null,
43        public string $languageCode = 'pl',
44        public array $availableVariables = [],
45        public bool $isActive = true,
46        public int $owner = 1
47    ) {
48    }
49
50    /**
51     * Instantiates ClientMailTemplate from database row.
52     *
53     * @param array<string, mixed> $row Database row.
54     * @return self Populated instance.
55     */
56    public static function fromRow(array $row): self
57    {
58        $rawVars = $row['available_variables'] ?? null;
59        $variables = [];
60        if (is_string($rawVars) && $rawVars !== '') {
61            $decoded = json_decode($rawVars, true);
62            if (is_array($decoded)) {
63                /** @var array<string, string> $decoded */
64                $variables = $decoded;
65            }
66        } elseif (is_array($rawVars)) {
67            /** @var array<string, string> $rawVars */
68            $variables = $rawVars;
69        }
70
71        return new self(
72            id: (int)($row['id'] ?? 0),
73            code: (string)($row['code'] ?? ''),
74            name: (string)($row['name'] ?? ''),
75            category: (string)($row['category'] ?? 'general'),
76            subject: (string)($row['subject'] ?? ''),
77            bodyHtml: (string)($row['body_html'] ?? ''),
78            bodyText: isset($row['body_text']) && (string)$row['body_text'] !== ''
79                ? (string)$row['body_text']
80                : null,
81            languageCode: (string)($row['language_code'] ?? 'pl'),
82            availableVariables: $variables,
83            isActive: (bool)($row['is_active'] ?? true),
84            owner: (int)($row['owner'] ?? 1)
85        );
86    }
87}