Completed
Push — master ( aa8b87...f1e278 )
by
unknown
01:40
created

TemplateMailableRenderer::renderTextView()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
namespace Spatie\MailTemplates;
4
5
use Illuminate\Mail\Markdown;
6
use Illuminate\Support\Facades\Mail;
7
use Mustache_Engine;
8
use Spatie\MailTemplates\Exceptions\CannotRenderTemplateMailable;
9
use Spatie\MailTemplates\Models\MailTemplate;
10
11
class TemplateMailableRenderer
12
{
13
    /** @var TemplateMailable */
14
    protected $templateMailable;
15
16
    /** @var MailTemplate */
17
    protected $mailTemplate;
18
19
    /** @var Mustache_Engine */
20
    protected $mustache;
21
22
    /** @var Markdown */
23
    protected $markdown;
24
25
    public function __construct(TemplateMailable $templateMailable, Mustache_Engine $mustache, Markdown $markdown)
26
    {
27
        $this->templateMailable = $templateMailable;
28
        $this->mustache = $mustache;
29
        $this->markdown = $markdown;
30
31
        $templateModel = $this->templateMailable->getTemplateModel();
32
        $this->mailTemplate = $templateModel::findForMailable($templateMailable);
33
    }
34
35
    public function render(array $data = []): string
36
    {
37
        $renderer = ($this->mailTemplate->isMarkdown()
38
            ? $this->markdown->theme($this->mailTemplate->markdown_theme ?? 'default')
0 ignored issues
show
Documentation introduced by
The property markdown_theme does not exist on object<Spatie\MailTemplates\Models\MailTemplate>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
39
            : $this->mustache);
40
41
        $html = $renderer->render(
42
            $this->mailTemplate->template,
0 ignored issues
show
Documentation introduced by
The property template does not exist on object<Spatie\MailTemplates\Models\MailTemplate>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
43
            $data
44
        );
45
46
        return $this->renderInLayout($html, $data);
47
    }
48
49
    public function renderTextView(array $data = []): ?string
50
    {
51
        if ($this->mailTemplate->isMarkdown()) {
52
            return $this->templateMailable->textView
53
                ?? $this->markdown->renderText($this->mailTemplate->template, $data);
0 ignored issues
show
Documentation introduced by
The property template does not exist on object<Spatie\MailTemplates\Models\MailTemplate>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
54
        }
55
56
        return $this->templateMailable->textView ?? null;
57
    }
58
59
    public function renderSubject(array $data = []): string
60
    {
61
        return $this->mustache->render(
62
            $this->mailTemplate->subject,
0 ignored issues
show
Documentation introduced by
The property subject does not exist on object<Spatie\MailTemplates\Models\MailTemplate>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
63
            $data
64
        );
65
    }
66
67
    protected function renderInLayout(string $html, array $data = []): string
68
    {
69
        $layout = $this->templateMailable->getLayout()
70
            ?? $this->mailTemplate->getLayout()
71
            ?? '{{{ body }}}';
72
73
        // TODO: Regex for finding {{{ body }}} in layout string
74
        if ($layout && ! str_contains($layout, ['{{{body}}}', '{{{ body }}}', '{{body}}', '{{ body }}'])) {
75
            throw CannotRenderTemplateMailable::layoutDoesNotContainABodyPlaceHolder($this->templateMailable, $this->mailTemplate, $layout);
76
        }
77
78
        $data = array_merge(['body' => $html], $data);
79
80
        return $this->mustache->render($layout, $data);
81
    }
82
}
83