Skip to content

Commit 38c6825

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

File tree

8 files changed

+75
-58
lines changed

8 files changed

+75
-58
lines changed

Diff for: 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
}

Diff for: 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

Diff for: src/Domain/Model/Messaging/Message/MessageMetadata.php

+15-11
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ class MessageMetadata
1313
#[ORM\Column(type: 'string', length: 255, nullable: true)]
1414
private ?string $status = null;
1515

16-
#[ORM\Column(type: 'integer', options: ['unsigned' => true, 'default' => 0])]
17-
private int $processed;
16+
#[ORM\Column(type: 'boolean', options: ['unsigned' => true, 'default' => false])]
17+
private bool $processed;
1818

19-
#[ORM\Column(type: 'integer', options: ['default' => 0])]
20-
private int $viewed;
19+
#[ORM\Column(type: 'boolean', options: ['default' => false])]
20+
private bool $viewed = false;
2121

2222
#[ORM\Column(name: 'bouncecount', type: 'integer', options: ['default' => 0])]
2323
private int $bounceCount;
@@ -30,15 +30,13 @@ class MessageMetadata
3030

3131
public function __construct(
3232
?string $status = null,
33-
int $processed = 0,
34-
int $viewed = 0,
3533
int $bounceCount = 0,
3634
?DateTime $entered = null,
3735
?DateTime $sent = null,
3836
) {
3937
$this->status = $status;
40-
$this->processed = $processed;
41-
$this->viewed = $viewed;
38+
$this->processed = false;
39+
$this->viewed = false;
4240
$this->bounceCount = $bounceCount;
4341
$this->entered = $entered ?? new DateTime();
4442
$this->sent = $sent;
@@ -49,18 +47,24 @@ public function getStatus(): ?string
4947
return $this->status;
5048
}
5149

52-
public function getProcessed(): int
50+
public function isProcessed(): bool
5351
{
5452
return $this->processed;
5553
}
5654

57-
public function setProcessed(int $processed): self
55+
public function setProcessed(bool $processed): self
5856
{
5957
$this->processed = $processed;
6058
return $this;
6159
}
6260

63-
public function getViewed(): int
61+
public function setViewed(bool $viewed): self
62+
{
63+
$this->viewed = $viewed;
64+
return $this;
65+
}
66+
67+
public function isViewed(): bool
6468
{
6569
return $this->viewed;
6670
}

Diff for: 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;

Diff for: 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

Diff for: tests/Integration/Domain/Repository/Fixtures/MessageFixture.php

+11-8
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(
@@ -63,12 +66,12 @@ public function load(ObjectManager $manager): void
6366
);
6467
$metadata = new MessageMetadata(
6568
$row['status'],
66-
$row['processed'],
67-
$row['viewed'],
6869
$row['bouncecount'],
6970
$row['entered'],
7071
$row['sent']
7172
);
73+
$metadata->setProcessed((bool) $row['processed']);
74+
$metadata->setViewed((bool)$row['viewed']);
7275
$content = new MessageContent(
7376
$row['subject'],
7477
$row['message'],
@@ -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']);

Diff for: tests/Integration/Domain/Repository/Messaging/MessageRepositoryTest.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public function testMessageIsPersistedAndFetchedCorrectly(): void
4646
$this->entityManager->persist($admin);
4747

4848
$message = new Message(
49-
new MessageFormat(true),
49+
new MessageFormat(true, 'text'),
5050
new MessageSchedule(1, null, 3, null),
5151
new MessageMetadata('done'),
5252
new MessageContent('Hello world!'),
@@ -75,7 +75,7 @@ public function testGetByOwnerIdReturnsOnlyOwnedMessages(): void
7575
$this->entityManager->persist($admin2);
7676

7777
$msg1 = new Message(
78-
new MessageFormat(true),
78+
new MessageFormat(true, MessageFormat::FORMAT_TEXT),
7979
new MessageSchedule(1, null, 3, null),
8080
new MessageMetadata('done'),
8181
new MessageContent('Owned by Admin 1!'),
@@ -84,7 +84,7 @@ public function testGetByOwnerIdReturnsOnlyOwnedMessages(): void
8484
);
8585

8686
$msg2 = new Message(
87-
new MessageFormat(true),
87+
new MessageFormat(true, MessageFormat::FORMAT_TEXT),
8888
new MessageSchedule(1, null, 3, null),
8989
new MessageMetadata(null),
9090
new MessageContent('Owned by Admin 2!'),
@@ -93,7 +93,7 @@ public function testGetByOwnerIdReturnsOnlyOwnedMessages(): void
9393
);
9494

9595
$msg3 = new Message(
96-
new MessageFormat(true),
96+
new MessageFormat(true, MessageFormat::FORMAT_TEXT),
9797
new MessageSchedule(1, null, 3, null),
9898
new MessageMetadata(null),
9999
new MessageContent('Hello world!'),
@@ -118,7 +118,7 @@ public function testMessageTimestampsAreSetOnPersist(): void
118118
$expectedDate = new DateTime();
119119

120120
$message = new Message(
121-
new MessageFormat(true),
121+
new MessageFormat(true, MessageFormat::FORMAT_TEXT),
122122
new MessageSchedule(1, null, 3, null),
123123
new MessageMetadata(null),
124124
new MessageContent('Hello world!'),

Diff for: tests/Unit/Domain/Model/Messaging/MessageTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class MessageTest extends TestCase
2929

3030
protected function setUp(): void
3131
{
32-
$this->format = new MessageFormat(true);
32+
$this->format = new MessageFormat(true, MessageFormat::FORMAT_TEXT);
3333
$this->schedule = new MessageSchedule(1, new DateTime(), 2, new DateTime());
3434
$this->metadata = new MessageMetadata();
3535
$this->content = new MessageContent('This is the body');

0 commit comments

Comments
 (0)