Completed
Push — 5.0 ( f11eac...a6dbd7 )
by Huberty
02:05
created

SwiftTwigMailTemplate::setBccAdresses()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace TheCodingMachine\Mail\Template;
4
5
6
use TheCodingMachine\Mail\SwiftMailTemplate;
7
8
class SwiftTwigMailTemplate implements SwiftMailTemplate
9
{
10
    /**
11
     * @var \Twig_Environment
12
     */
13
    protected $twigEnvironment;
14
15
    /**
16
     * @var string
17
     */
18
    protected $twigPath;
19
20
    /**
21
     * @var string|array
22
     */
23
    protected $fromAdresses;
24
25
    /**
26
     * @var string
27
     */
28
    protected $fromName = null;
29
30
    /**
31
     * @var string|array
32
     */
33
    protected $toAdresses;
34
35
    /**
36
     * @var string
37
     */
38
    protected $toName = null;
39
40
    /**
41
     * @var string|array
42
     */
43
    protected $bccAdresses;
44
45
    /**
46
     * @var string
47
     */
48
    protected $bccName = null;
49
50
    /**
51
     * @var string|array
52
     */
53
    protected $ccAdresses;
54
55
    /**
56
     * @var string
57
     */
58
    protected $ccName = null;
59
60
    /**
61
     * @var string|array
62
     */
63
    protected $replyToAdresses;
64
65
    /**
66
     * @var string
67
     */
68
    protected $replyToName = null;
69
70
    /**
71
     * @var int
72
     */
73
    protected $maxLineLength = 1000;
74
75
    /**
76
     * @var int
77
     */
78
    protected $priority;
79
80
    /**
81
     * @var string
82
     */
83
    protected $readReceiptTo;
84
85
    /**
86
     * @var string
87
     */
88
    protected $returnPath;
89
90
    /**
91
     * SwiftTwigMailGenerator constructor.
92
     *
93
     * @param \Twig_Environment $twig_Environment
94
     * @param string            $twigPath
95
     */
96
    public function __construct(\Twig_Environment $twig_Environment, string $twigPath)
97
    {
98
        $this->twigEnvironment = $twig_Environment;
99
        $this->twigPath = $twigPath;
100
    }
101
102
    /**
103
     * @param array $data
104
     *
105
     * @return \Swift_Message
106
     */
107
    public function renderMail(array $data = []) :\Swift_Message
108
    {
109
        $mail = new \Swift_Message();
110
111
        $twigEnvironment = clone $this->twigEnvironment;
112
        $function = new \Twig_SimpleFunction('embedImage', function ($imgPath) use ($mail) {
113
            return $mail->embed(\Swift_Image::fromPath($imgPath));
114
        });
115
        $twigEnvironment->addFunction($function);
116
117
        $template = $twigEnvironment->loadTemplate($this->twigPath);
118
119
        if (!$template->hasBlock('subject') || !$template->hasBlock('body_html') || !$template->hasBlock('body_text')) {
120
            //TODO extract body_text from body HTML
121
            throw MissingBlockException::missingBlock($template->getBlockNames());
122
        }
123
124
        $subject  = $template->renderBlock('subject', $data);
0 ignored issues
show
Bug introduced by
The method renderBlock() does not exist on Twig_TemplateInterface. Did you maybe mean render()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
125
        $bodyHtml = $template->renderBlock('body_html', $data);
0 ignored issues
show
Bug introduced by
The method renderBlock() does not exist on Twig_TemplateInterface. Did you maybe mean render()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
126
        $bodyText = $template->renderBlock('body_text', $data);
0 ignored issues
show
Bug introduced by
The method renderBlock() does not exist on Twig_TemplateInterface. Did you maybe mean render()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
127
128
        $mail->setSubject($subject);
129
        $mail->setBody($bodyHtml);
130
        $mail->addPart($bodyText);
131
132
        if ($this->fromAdresses) {
133
            $mail->setFrom($this->fromAdresses, $this->fromName);
134
            $mail->setSender($this->fromAdresses, $this->fromName);
0 ignored issues
show
Bug introduced by
It seems like $this->fromAdresses can also be of type array; however, Swift_Mime_SimpleMessage::setSender() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
135
        }
136
137
        if ($this->toAdresses) {
138
            $mail->setTo($this->toAdresses, $this->toName);
139
        }
140
141
        if ($this->bccAdresses) {
142
            $mail->setBcc($this->bccAdresses, $this->bccName);
143
        }
144
        if ($this->ccAdresses) {
145
            $mail->setCc($this->ccAdresses, $this->ccName);
146
        }
147
        if ($this->replyToAdresses) {
148
            $mail->setReplyTo($this->replyToAdresses, $this->replyToName);
149
        }
150
151
        if ($this->maxLineLength) {
152
            $mail->setMaxLineLength($this->maxLineLength);
153
        }
154
        if ($this->priority) {
155
            $mail->setPriority($this->priority);
156
        }
157
158
        if ($this->readReceiptTo) {
159
            $mail->setReadReceiptTo($this->readReceiptTo);
0 ignored issues
show
Documentation introduced by
$this->readReceiptTo is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
160
        }
161
162
        if ($this->returnPath) {
163
            $mail->setReturnPath($this->returnPath);
164
        }
165
166
        return $mail;
167
    }
168
169
    /**
170
     * @param array|string $fromAdresses
171
     */
172
    public function setFromAdresses($fromAdresses)
173
    {
174
        $this->fromAdresses = $fromAdresses;
175
    }
176
177
    /**
178
     * @param string $fromName
179
     */
180
    public function setFromName($fromName)
181
    {
182
        $this->fromName = $fromName;
183
    }
184
185
    /**
186
     * @param array|string $toAdresses
187
     */
188
    public function setToAdresses($toAdresses)
189
    {
190
        $this->toAdresses = $toAdresses;
191
    }
192
193
    /**
194
     * @param string $toName
195
     */
196
    public function setToName($toName)
197
    {
198
        $this->toName = $toName;
199
    }
200
201
    /**
202
     * @param array|string $bccAdresses
203
     */
204
    public function setBccAdresses($bccAdresses)
205
    {
206
        $this->bccAdresses = $bccAdresses;
207
    }
208
209
    /**
210
     * @param string $bccName
211
     */
212
    public function setBccName($bccName)
213
    {
214
        $this->bccName = $bccName;
215
    }
216
217
    /**
218
     * @param array|string $ccAdresses
219
     */
220
    public function setCcAdresses($ccAdresses)
221
    {
222
        $this->ccAdresses = $ccAdresses;
223
    }
224
225
    /**
226
     * @param string $ccName
227
     */
228
    public function setCcName($ccName)
229
    {
230
        $this->ccName = $ccName;
231
    }
232
233
    /**
234
     * @param array|string $replyToAdresses
235
     */
236
    public function setReplyToAdresses($replyToAdresses)
237
    {
238
        $this->replyToAdresses = $replyToAdresses;
239
    }
240
241
    /**
242
     * @param string $replyToName
243
     */
244
    public function setReplyToName($replyToName)
245
    {
246
        $this->replyToName = $replyToName;
247
    }
248
249
    /**
250
     * @param int $maxLineLength
251
     */
252
    public function setMaxLineLength($maxLineLength)
253
    {
254
        $this->maxLineLength = $maxLineLength;
255
    }
256
257
    /**
258
     * @param int $priority
259
     */
260
    public function setPriority($priority)
261
    {
262
        $this->priority = $priority;
263
    }
264
265
    /**
266
     * @param string $readReceiptTo
267
     */
268
    public function setReadReceiptTo($readReceiptTo)
269
    {
270
        $this->readReceiptTo = $readReceiptTo;
271
    }
272
273
    /**
274
     * @param string $returnPath
275
     */
276
    public function setReturnPath($returnPath)
277
    {
278
        $this->returnPath = $returnPath;
279
    }
280
}
281