Skip to content

Commit f6f8791

Browse files
committed
Remove redundant library, move Email to Template
1 parent 1f100cf commit f6f8791

File tree

2 files changed

+106
-97
lines changed

2 files changed

+106
-97
lines changed

src/Entities/Template.php

Lines changed: 106 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php namespace Tatter\Outbox\Entities;
22

3+
use CodeIgniter\Email\Email;
34
use CodeIgniter\Entity;
45
use Tatter\Outbox\Models\TemplateModel;
56
use TijsVerkoyen\CssToInlineStyles\CssToInlineStyles;
@@ -9,56 +10,146 @@ class Template extends Entity
910
protected $table = 'outbox_templates';
1011
protected $dates = ['created_at', 'updated_at', 'deleted_at'];
1112
protected $casts = [
12-
'tokens' => 'csv',
1313
'parent_id' => '?int',
1414
];
1515

1616
/**
17-
* Renders this Template with inlined CSS.
17+
* Stored parent Template.
18+
*
19+
* @var self|null
20+
*/
21+
protected $parent;
22+
23+
//--------------------------------------------------------------------
24+
25+
/**
26+
* Returns the parent Template, if set.
27+
*
28+
* @return self|null
29+
*/
30+
public function getParent(): ?self
31+
{
32+
if (! isset($this->attributes['parent_id']))
33+
{
34+
return null;
35+
}
36+
37+
if (is_null($this->parent))
38+
{
39+
$this->parent = model(TemplateModel::class)->find($this->attributes['parent_id']);
40+
}
41+
42+
return $this->parent;
43+
}
44+
45+
/**
46+
* Returns the subject from the parent Template if this one is empty.
47+
*
48+
* @return string
49+
*/
50+
public function getSubject(): string
51+
{
52+
if (! empty($this->attributes['subject']))
53+
{
54+
return $this->attributes['subject'];
55+
}
56+
57+
return $this->getParent() ? $this->parent->subject : '';
58+
}
59+
60+
/**
61+
* Returns any subject or body tokens from this and parent.
62+
*
63+
* @return array
64+
*/
65+
public function getTokens(): array
66+
{
67+
preg_match_all('#\{(\w+)\}#', $this->attributes['subject'] . $this->attributes['body'], $matches);
68+
69+
if ($parent = $this->getParent())
70+
{
71+
$matches[1] = array_unique(array_merge($parent->getTokens(), $matches[1]));
72+
}
73+
74+
return $matches[1];
75+
}
76+
77+
//--------------------------------------------------------------------
78+
79+
/**
80+
* Renders the body with inlined CSS.
1881
*
1982
* @param array $data Variables to exchange for Template tokens
2083
* @param string|null $styles CSS to use for inlining, defaults to configured view
2184
*
2285
* @return string
2386
*/
24-
public function render($data = [], string $styles = null): string
87+
public function renderBody($data = [], string $styles = null): string
2588
{
2689
if (empty($this->attributes['body']))
2790
{
2891
return '';
2992
}
3093

3194
// Replace tokens with $data values
32-
$body = service('parser')->setData($data, 'raw')->renderString($this->attributes['body'], ['debug' => false]);
95+
$body = service('parser')
96+
->setData($data, 'raw')
97+
->renderString($this->attributes['body'], ['debug' => false]);
3398

3499
// If this has a parent Template then render it with this body
35100
if ($parent = $this->getParent())
36101
{
37102
$data['body'] = $body;
38103

39-
return $parent->render($data, $styles);
104+
return $parent->renderBody($data, $styles);
40105
}
41106

42107
// Determine styling
43108
$styles = $styles ?? view(config('Outbox')->styles, [], ['debug' => false]);
44-
if (empty($styles))
109+
110+
return $styles === '' ? $body : (new CssToInlineStyles)->convert($body, $styles);
111+
}
112+
113+
/**
114+
* Renders the subject.
115+
*
116+
* @param array $data Variables to exchange for Template tokens
117+
*
118+
* @return string
119+
*/
120+
public function renderSubject($data = []): string
121+
{
122+
if (! $subject = $this->getSubject())
45123
{
46-
return $body;
124+
return '';
47125
}
48126

49-
return (new CssToInlineStyles)->convert($body, $styles);
127+
// Replace tokens with $data values
128+
return service('parser')
129+
->setData($data, 'raw')
130+
->renderString($subject, ['debug' => false]);
50131
}
51132

133+
//--------------------------------------------------------------------
134+
52135
/**
53-
* Returns the parent Template, if set.
136+
* Returns an Email instance primed to this Template's rendered values.
54137
*
55-
* @return self|null
138+
* @param array $data Variables to use when rendering the body
139+
* @param string|null $styles CSS to use for inlining, null to use configured view
140+
*
141+
* @return Email
56142
*/
57-
public function getParent(): ?self
143+
public function email($data = [], string $styles = null): Email
58144
{
59-
return $this->parent_id
60-
? model(TemplateModel::class)->find($this->parent_id)
61-
: null;
145+
// Start with the default config and add necessary settings
146+
$email = service('email');
147+
$email->mailType = 'html';
148+
$email->wordWrap = false;
149+
150+
// Render the subject and body and return the Email instance
151+
return $email
152+
->setSubject($this->renderSubject($data))
153+
->setMessage($this->renderBody($data, $styles));
62154
}
63155
}
64-

src/Outbox.php

Lines changed: 0 additions & 82 deletions
This file was deleted.

0 commit comments

Comments
 (0)