TemplateMailableRenderer::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 2
1
<?php
2
3
namespace Spatie\MailTemplates;
4
5
use Mustache_Engine;
6
use Illuminate\Support\Str;
7
use Spatie\MailTemplates\Exceptions\CannotRenderTemplateMailable;
8
9
class TemplateMailableRenderer
10
{
11
    public const RENDER_HTML_LAYOUT = 0;
12
    public const RENDER_TEXT_LAYOUT = 1;
13
14
    /** @var \Spatie\MailTemplates\TemplateMailable */
15
    protected $templateMailable;
16
17
    /** @var \Spatie\MailTemplates\Interfaces\MailTemplateInterface */
18
    protected $mailTemplate;
19
20
    /** @var \Mustache_Engine */
21
    protected $mustache;
22
23
    public function __construct(TemplateMailable $templateMailable, Mustache_Engine $mustache)
24
    {
25
        $this->templateMailable = $templateMailable;
26
        $this->mustache = $mustache;
27
        $this->mailTemplate = $templateMailable->getMailTemplate();
28
    }
29
30
    public function renderHtmlLayout(array $data = []): string
31
    {
32
        $body = $this->mustache->render(
33
            $this->mailTemplate->getHtmlTemplate(),
34
            $data
35
        );
36
37
        return $this->renderInLayout($body, static::RENDER_HTML_LAYOUT, $data);
38
    }
39
40
    public function renderTextLayout(array $data = []): ?string
41
    {
42
        if (! $this->mailTemplate->getTextTemplate()) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->mailTemplate->getTextTemplate() of type null|string is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
43
            return $this->textView ?? null;
0 ignored issues
show
Bug introduced by
The property textView does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
44
        }
45
46
        $body = $this->mustache->render(
47
            $this->mailTemplate->getTextTemplate(),
48
            $data
49
        );
50
51
        return $this->renderInLayout($body, static::RENDER_TEXT_LAYOUT, $data);
52
    }
53
54
    public function renderSubject(array $data = []): string
55
    {
56
        return $this->mustache->render(
57
            $this->mailTemplate->getSubject(),
58
            $data
59
        );
60
    }
61
62
    protected function renderInLayout(string $body, int $layoutType, array $data = []): string
63
    {
64
        $method = $layoutType === static::RENDER_HTML_LAYOUT ? 'getHtmlLayout' : 'getTextLayout';
65
        $layout = $this->templateMailable->$method()
66
            ?? (method_exists($this->mailTemplate, $method) ? $this->mailTemplate->$method() : null)
67
            ?? '{{{ body }}}';
68
69
        $this->guardAgainstInvalidLayout($layout);
70
71
        $data = array_merge(['body' => $body], $data);
72
73
        return $this->mustache->render($layout, $data);
74
    }
75
76
    protected function guardAgainstInvalidLayout(string $layout): void
77
    {
78
        if (! Str::contains($layout, [
79
            '{{{body}}}',
80
            '{{{ body }}}',
81
            '{{body}}',
82
            '{{ body }}',
83
            '{{ $body }}',
84
            '{!! $body !!}',
85
        ])) {
86
            throw CannotRenderTemplateMailable::layoutDoesNotContainABodyPlaceHolder($this->templateMailable);
87
        }
88
    }
89
}
90