Completed
Push — 2.1 ( f59067...a01579 )
by
unknown
21:13 queued 09:54
created

Template   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 96.43%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 4
dl 0
loc 105
ccs 27
cts 28
cp 0.9643
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getViewPath() 0 4 1
A render() 0 8 2
D compose() 0 34 9
1
<?php
2
/**
3
 * @link http://www.yiiframework.com/
4
 * @copyright Copyright (c) 2008 Yii Software LLC
5
 * @license http://www.yiiframework.com/license/
6
 */
7
8
namespace yii\mail;
9
10
use Yii;
11
use yii\base\Object;
12
use yii\base\ViewContextInterface;
13
14
/**
15
 * Template composes the message from view templates, ensuring isolated view rendering. It allows
16
 * changing of the rendering options such as layout during composing of the particular message without
17
 * affecting other messages.
18
 *
19
 * An instance of this class serves as a view context during the mail template rendering and is available
20
 * inside a view template file via [[\yii\base\View::context]].
21
 *
22
 * @see BaseMailer::compose()
23
 *
24
 * @author Paul Klimov <[email protected]>
25
 * @since 2.1
26
 */
27
class Template extends Object implements ViewContextInterface
0 ignored issues
show
Deprecated Code introduced by
The class yii\base\Object has been deprecated with message: since 2.0.13, the class name `Object` is invalid since PHP 7.2, use [[BaseObject]] instead.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
28
{
29
    /**
30
     * @var MessageInterface related mail message instance.
31
     */
32
    public $message;
33
    /**
34
     * @var \yii\base\View view instance used for rendering.
35
     */
36
    public $view;
37
    /**
38
     * @var string path to the directory containing view files.
39
     */
40
    public $viewPath;
41
    /**
42
     * @var string|array name of the view to use as a template. The value could be:
43
     *
44
     * - a string that contains either a view name or a path alias for rendering HTML body of the email.
45
     *   The text body in this case is generated by applying `strip_tags()` to the HTML body.
46
     * - an array with 'html' and/or 'text' elements. The 'html' element refers to a view name or a path alias
47
     *   for rendering the HTML body, while 'text' element is for rendering the text body. For example,
48
     *   `['html' => 'contact-html', 'text' => 'contact-text']`.
49
     */
50
    public $viewName;
51
    /**
52
     * @var string|false HTML layout view name. It is the layout used to render HTML mail body.
53
     * The property can take the following values:
54
     *
55
     * - a relative view name: a view file relative to [[viewPath]], e.g., 'layouts/html'.
56
     * - a path alias: an absolute view file path specified as a path alias, e.g., '@app/mail/html'.
57
     * - a bool false: the layout is disabled.
58
     */
59
    public $htmlLayout = false;
60
    /**
61
     * @var string|false text layout view name. This is the layout used to render TEXT mail body.
62
     * Please refer to [[htmlLayout]] for possible values that this property can take.
63
     */
64
    public $textLayout = false;
65
66
67
    /**
68
     * @inheritdoc
69
     */
70 6
    public function getViewPath()
71
    {
72 6
        return $this->viewPath;
73
    }
74
75
    /**
76
     * Composes the given mail message according to this template.
77
     * @param MessageInterface $message the message to be composed.
78
     * @param array $params the parameters (name-value pairs) that will be extracted and made available in the view file.
79
     */
80 4
    public function compose(MessageInterface $message, $params = [])
81
    {
82 4
        $this->message = $message;
83
84 4
        if (is_array($this->viewName)) {
85 2
            if (isset($this->viewName['html'])) {
86 2
                $html = $this->render($this->viewName['html'], $params, $this->htmlLayout);
87
            }
88 2
            if (isset($this->viewName['text'])) {
89 2
                $text = $this->render($this->viewName['text'], $params, $this->textLayout);
90
            }
91
        } else {
92 4
            $html = $this->render($this->viewName, $params, $this->htmlLayout);
93
        }
94
95 4
        if (isset($html)) {
96 4
            $this->message->setHtmlBody($html);
97
        }
98 4
        if (isset($text)) {
99 2
            $this->message->setTextBody($text);
100
        } elseif (isset($html)) {
101 4
            if (preg_match('~<body[^>]*>(.*?)</body>~is', $html, $match)) {
102 1
                $html = $match[1];
103
            }
104
            // remove style and script
105 4
            $html = preg_replace('~<((style|script))[^>]*>(.*?)</\1>~is', '', $html);
106
            // strip all HTML tags and decode HTML entities
107 4
            $text = html_entity_decode(strip_tags($html), ENT_QUOTES | ENT_HTML5, Yii::$app ? Yii::$app->charset : 'UTF-8');
108
            // improve whitespace
109 4
            $text = preg_replace("~^[ \t]+~m", '', trim($text));
110 4
            $text = preg_replace('~\R\R+~mu', "\n\n", $text);
111 4
            $this->message->setTextBody($text);
112
        }
113 4
    }
114
115
    /**
116
     * Renders the view specified with optional parameters and layout.
117
     * The view will be rendered using the [[view]] component.
118
     * @param string $view a view name or a path alias of the view file.
119
     * @param array $params the parameters (name-value pairs) that will be extracted and made available in the view file.
120
     * @param string|bool $layout layout view name or a path alias. If the value is false, no layout will be applied.
121
     * @return string the rendering result.
122
     */
123 6
    public function render($view, $params = [], $layout = false)
124
    {
125 6
        $output = $this->view->render($view, $params, $this);
126 6
        if ($layout === false) {
127 5
            return $output;
128
        }
129 1
        return $this->view->render($layout, ['content' => $output], $this);
0 ignored issues
show
Bug introduced by
It seems like $layout defined by parameter $layout on line 123 can also be of type boolean; however, yii\base\View::render() does only seem to accept string, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
130
    }
131
}
132