Skip to content

Commit fc0b6b6

Browse files
committed
ISSUE-345: relation to template in message
1 parent 743fc18 commit fc0b6b6

File tree

5 files changed

+52
-39
lines changed

5 files changed

+52
-39
lines changed

src/Domain/Model/Messaging/Message.php

+12-1
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,18 @@ class Message implements DomainModel, Identity, ModificationDate
4949
#[ORM\JoinColumn(name: 'owner', referencedColumnName: 'id', nullable: true)]
5050
private ?Administrator $owner;
5151

52+
#[ORM\ManyToOne(targetEntity: Template::class)]
53+
#[ORM\JoinColumn(name: 'template', referencedColumnName: 'id', nullable: true, onDelete: 'SET NULL')]
54+
private ?Template $template = null;
55+
5256
public function __construct(
5357
MessageFormat $format,
5458
MessageSchedule $schedule,
5559
MessageMetadata $metadata,
5660
MessageContent $content,
5761
MessageOptions $options,
58-
?Administrator $owner = null
62+
?Administrator $owner,
63+
?Template $template = null,
5964
) {
6065
$this->format = $format;
6166
$this->schedule = $schedule;
@@ -64,6 +69,7 @@ public function __construct(
6469
$this->options = $options;
6570
$this->uuid = bin2hex(random_bytes(18));
6671
$this->owner = $owner;
72+
$this->template = $template;
6773
}
6874

6975
public function getFormat(): MessageFormat
@@ -100,4 +106,9 @@ public function getOwner(): ?Administrator
100106
{
101107
return $this->owner;
102108
}
109+
110+
public function getTemplate(): ?Template
111+
{
112+
return $this->template;
113+
}
103114
}

src/Domain/Model/Messaging/Message/MessageFormat.php

+24-16
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace PhpList\Core\Domain\Model\Messaging\Message;
66

77
use Doctrine\ORM\Mapping as ORM;
8+
use InvalidArgumentException;
89

910
#[ORM\Embeddable]
1011
class MessageFormat
@@ -16,36 +17,43 @@ class MessageFormat
1617
private ?string $sendFormat = null;
1718

1819
#[ORM\Column(name: 'astext', type: 'integer', options: ['default' => 0])]
19-
private bool $asText;
20+
private bool $asText = false;
2021

2122
#[ORM\Column(name: 'ashtml', type: 'integer', options: ['default' => 0])]
22-
private bool $asHtml;
23+
private bool $asHtml = false;
2324

2425
#[ORM\Column(name: 'aspdf', type: 'integer', options: ['default' => 0])]
25-
private bool $asPdf;
26+
private bool $asPdf = false;
2627

2728
#[ORM\Column(name: 'astextandhtml', type: 'integer', options: ['default' => 0])]
28-
private bool $asTextAndHtml;
29+
private bool $asTextAndHtml = false;
2930

3031
#[ORM\Column(name: 'astextandpdf', type: 'integer', options: ['default' => 0])]
31-
private bool $asTextAndPdf;
32+
private bool $asTextAndPdf = false;
33+
34+
public const FORMAT_TEXT = 'text';
35+
public const FORMAT_HTML = 'html';
36+
public const FORMAT_PDF = 'pdf';
3237

3338
public function __construct(
3439
bool $htmlFormatted,
35-
string $sendFormat = null,
36-
bool $asText = false,
37-
bool $asHtml = false,
38-
bool $asPdf = false,
39-
bool $asTextAndHtml = false,
40-
bool $asTextAndPdf = false,
40+
?string $sendFormat,
41+
array $formatOptions = []
4142
) {
4243
$this->htmlFormatted = $htmlFormatted;
4344
$this->sendFormat = $sendFormat;
44-
$this->asText = $asText;
45-
$this->asHtml = $asHtml;
46-
$this->asPdf = $asPdf;
47-
$this->asTextAndHtml = $asTextAndHtml;
48-
$this->asTextAndPdf = $asTextAndPdf;
45+
46+
foreach ($formatOptions as $option) {
47+
match ($option) {
48+
self::FORMAT_TEXT => $this->asText = true,
49+
self::FORMAT_HTML => $this->asHtml = true,
50+
self::FORMAT_PDF => $this->asPdf = true,
51+
default => throw new InvalidArgumentException('Invalid format option: ' . $option)
52+
};
53+
}
54+
55+
$this->asTextAndHtml = $this->asText && $this->asHtml;
56+
$this->asTextAndPdf = $this->asText && $this->asPdf;
4957
}
5058

5159
public function isHtmlFormatted(): bool

src/Domain/Model/Messaging/Message/MessageOptions.php

-10
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,6 @@ class MessageOptions
2525
#[ORM\Column(name: 'userselection', type: 'text', nullable: true)]
2626
private ?string $userSelection;
2727

28-
#[ORM\Column(type: 'integer', nullable: true)]
29-
private ?int $template;
30-
3128
#[ORM\Column(name: 'sendstart', type: 'datetime', nullable: true)]
3229
private ?DateTime $sendStart;
3330

@@ -40,7 +37,6 @@ public function __construct(
4037
string $replyTo = '',
4138
?DateTime $embargo = null,
4239
?string $userSelection = null,
43-
?int $template = null,
4440
?DateTime $sendStart = null,
4541
?string $rssTemplate = null
4642
) {
@@ -49,7 +45,6 @@ public function __construct(
4945
$this->replyTo = $replyTo;
5046
$this->embargo = $embargo;
5147
$this->userSelection = $userSelection;
52-
$this->template = $template;
5348
$this->sendStart = $sendStart;
5449
$this->rssTemplate = $rssTemplate;
5550
}
@@ -79,11 +74,6 @@ public function getUserSelection(): ?string
7974
return $this->userSelection;
8075
}
8176

82-
public function getTemplate(): ?int
83-
{
84-
return $this->template;
85-
}
86-
8777
public function getSendStart(): ?DateTime
8878
{
8979
return $this->sendStart;

src/Domain/Model/Messaging/Template.php

+7-6
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ class Template implements DomainModel, Identity
2222
private string $title;
2323

2424
#[ORM\Column(name: 'template', type: 'blob', nullable: true)]
25-
private ?string $template = null;
25+
private mixed $template;
2626

2727
#[ORM\Column(name: 'template_text', type: 'blob', nullable: true)]
28-
private ?string $templateText = null;
28+
private mixed $templateText;
2929

3030
#[ORM\Column(name: 'listorder', type: 'integer', nullable: true)]
3131
private ?int $listOrder = null;
@@ -50,12 +50,12 @@ public function getTitle(): string
5050

5151
public function getTemplate(): ?string
5252
{
53-
return $this->template;
53+
return is_resource($this->template) ? stream_get_contents($this->template) : $this->template;
5454
}
5555

5656
public function getTemplateText(): ?string
5757
{
58-
return $this->templateText;
58+
return is_resource($this->templateText) ? stream_get_contents($this->templateText) : $this->templateText;
5959
}
6060

6161
public function getListOrder(): ?int
@@ -76,13 +76,14 @@ public function setTitle(string $title): self
7676

7777
public function setTemplate(?string $template): self
7878
{
79-
$this->template = $template;
79+
$this->template = $template !== null ? fopen('data://text/plain,' . $template, 'r') : null;
8080
return $this;
8181
}
8282

83+
8384
public function setTemplateText(?string $templateText): self
8485
{
85-
$this->templateText = $templateText;
86+
$this->templateText = $templateText !== null ? fopen('data://text/plain,' . $templateText, 'r') : null;
8687
return $this;
8788
}
8889

tests/Integration/Domain/Repository/Fixtures/MessageFixture.php

+9-6
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use PhpList\Core\Domain\Model\Messaging\Message\MessageMetadata;
1515
use PhpList\Core\Domain\Model\Messaging\Message\MessageOptions;
1616
use PhpList\Core\Domain\Model\Messaging\Message\MessageSchedule;
17+
use PhpList\Core\Domain\Model\Messaging\Template;
1718
use PhpList\Core\TestingSupport\Traits\ModelTestTrait;
1819
use RuntimeException;
1920

@@ -36,6 +37,7 @@ public function load(ObjectManager $manager): void
3637

3738
$headers = fgetcsv($handle);
3839
$adminRepository = $manager->getRepository(Administrator::class);
40+
$templateRepository = $manager->getRepository(Template::class);
3941

4042
do {
4143
$data = fgetcsv($handle);
@@ -44,15 +46,16 @@ public function load(ObjectManager $manager): void
4446
}
4547
$row = array_combine($headers, $data);
4648
$admin = $adminRepository->find($row['owner']);
49+
$template = $templateRepository->find($row['template']);
4750

4851
$format = new MessageFormat(
4952
$row['htmlformatted'],
5053
$row['sendformat'],
51-
(bool)$row['astext'],
52-
(bool)$row['ashtml'],
53-
(bool)$row['aspdf'],
54-
(bool)$row['astextandhtml'],
55-
(bool)$row['astextandpdf']
54+
array_keys(array_filter([
55+
MessageFormat::FORMAT_TEXT => $row['astext'],
56+
MessageFormat::FORMAT_HTML => $row['ashtml'],
57+
MessageFormat::FORMAT_PDF => $row['aspdf'],
58+
]))
5659
);
5760

5861
$schedule = new MessageSchedule(
@@ -81,7 +84,6 @@ public function load(ObjectManager $manager): void
8184
$row['replyto'],
8285
$row['embargo'],
8386
$row['userselection'],
84-
$row['template'],
8587
$row['sendstart'],
8688
$row['rsstemplate'],
8789
);
@@ -93,6 +95,7 @@ public function load(ObjectManager $manager): void
9395
$content,
9496
$options,
9597
$admin,
98+
$template,
9699
);
97100
$this->setSubjectId($message, (int)$row['id']);
98101
$this->setSubjectProperty($message, 'uuid', $row['uuid']);

0 commit comments

Comments
 (0)