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

Composer::createTemplate()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 11
cts 11
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 11
nc 4
nop 1
crap 3
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\InvalidConfigException;
12
use yii\base\Object;
13
use yii\web\View;
14
15
/**
16
 * Composer composes the mail messages via view rendering.
17
 *
18
 * @property \yii\base\View $view View instance. Note that the type of this property differs in getter and setter. See
19
 * [[getView()]] and [[setView()]] for details.
20
 * @property string $viewPath The directory that contains the view files for composing mail messages Defaults
21
 * to '@app/mail'.
22
 *
23
 * @author Paul Klimov <[email protected]>
24
 * @since 2.1
25
 */
26
class Composer extends Object
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...
27
{
28
    /**
29
     * @var string|bool HTML layout view name.
30
     * See [[Template::$htmlLayout]] for detailed documentation.
31
     */
32
    public $htmlLayout = 'layouts/html';
33
    /**
34
     * @var string|bool text layout view name.
35
     * See [[Template::$textLayout]] for detailed documentation.
36
     */
37
    public $textLayout = 'layouts/text';
38
    /**
39
     * @var array the configuration that should be applied to any newly created message template.
40
     */
41
    public $templateConfig = [];
42
43
    /**
44
     * @var \yii\base\View|array view instance or its array configuration.
45
     */
46
    private $_view = [];
47
    /**
48
     * @var string the directory containing view files for composing mail messages.
49
     */
50
    private $_viewPath;
51
52
53
    /**
54
     * @return string the directory that contains the view files for composing mail messages
55
     * Defaults to '@app/mail'.
56
     */
57 2
    public function getViewPath()
58
    {
59 2
        if ($this->_viewPath === null) {
60
            $this->setViewPath('@app/mail');
61
        }
62 2
        return $this->_viewPath;
63
    }
64
65
    /**
66
     * @param string $path the directory that contains the view files for composing mail messages
67
     * This can be specified as an absolute path or a path alias.
68
     */
69 2
    public function setViewPath($path)
70
    {
71 2
        $this->_viewPath = Yii::getAlias($path);
0 ignored issues
show
Documentation Bug introduced by
It seems like \Yii::getAlias($path) can also be of type boolean. However, the property $_viewPath is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
72 2
    }
73
74
    /**
75
     * @param array|\yii\base\View $view view instance or its array configuration that will be used to
76
     * render message bodies.
77
     * @throws InvalidConfigException on invalid argument.
78
     */
79 1
    public function setView($view)
80
    {
81 1
        if (!is_array($view) && !is_object($view)) {
82
            throw new InvalidConfigException('"' . get_class($this) . '::view" should be either object or configuration array, "' . gettype($view) . '" given.');
83
        }
84 1
        $this->_view = $view;
85 1
    }
86
87
    /**
88
     * @return \yii\base\View view instance.
89
     */
90 4
    public function getView()
91
    {
92 4
        if (!is_object($this->_view)) {
93 4
            $this->_view = $this->createView($this->_view);
94
        }
95
96 4
        return $this->_view;
97
    }
98
99
    /**
100
     * Creates view instance from given configuration.
101
     * @param array $config view configuration.
102
     * @return \yii\base\View view instance.
103
     */
104 4
    protected function createView(array $config)
105
    {
106 4
        if (!array_key_exists('class', $config)) {
107 4
            $config['class'] = View::class;
108
        }
109
110 4
        return Yii::createObject($config);
111
    }
112
113
    /**
114
     * Creates new message view template.
115
     * The newly created instance will be initialized with the configuration specified by [[templateConfig]].
116
     * @param string|array $viewName view name for the template.
117
     * @return Template message template instance.
118
     * @throws InvalidConfigException if the [[templateConfig]] is invalid.
119
     */
120 2
    protected function createTemplate($viewName)
121
    {
122 2
        $config = $this->templateConfig;
123 2
        if (!array_key_exists('class', $config)) {
124 2
            $config['class'] = Template::class;
125
        }
126 2
        if (!array_key_exists('view', $config)) {
127 2
            $config['view'] = $this->getView();
128
        }
129
130 2
        $config['viewPath'] = $this->getViewPath();
131 2
        $config['htmlLayout'] = $this->htmlLayout;
132 2
        $config['textLayout'] = $this->textLayout;
133 2
        $config['viewName'] = $viewName;
134
135 2
        return Yii::createObject($config);
136
    }
137
138
    /**
139
     * @param MessageInterface $message the message to be composed.
140
     * @param string|array $view the view to be used for rendering the message body. This can be:
141
     *
142
     * - a string, which represents the view name or path alias for rendering the HTML body of the email.
143
     *   In this case, the text body will be generated by applying `strip_tags()` to the HTML body.
144
     * - an array with 'html' and/or 'text' elements. The 'html' element refers to the view name or path alias
145
     *   for rendering the HTML body, while 'text' element is for rendering the text body. For example,
146
     *   `['html' => 'contact-html', 'text' => 'contact-text']`.
147
     *
148
     * @param array $params the parameters (name-value pairs) that will be extracted and made available in the view file.
149
     */
150 1
    public function compose($message, $view, array $params = [])
151
    {
152 1
        $this->createTemplate($view)->compose($message, $params);
153
    }
154
}