Completed
Pull Request — 5.0 (#6)
by Huberty
05:35
created

SwiftTwigMailTemplate::setToName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace TheCodingMachine\Mail\Template;
4
5
use TheCodingMachine\Mail\SwiftMailTemplate;
6
7
class SwiftTwigMailTemplate implements SwiftMailTemplate
8
{
9
    /**
10
     * @var \Twig_Environment
11
     */
12
    protected $twigEnvironment;
13
14
    /**
15
     * @var string
16
     */
17
    protected $twigPath;
18
19
    /**
20
     * @var string|array
21
     */
22
    protected $fromAddresses;
23
24
    /**
25
     * @var string
26
     */
27
    protected $fromName = null;
28
29
    /**
30
     * @var string|array
31
     */
32
    protected $toAddresses;
33
34
    /**
35
     * @var string
36
     */
37
    protected $toName = null;
38
39
    /**
40
     * @var string|array
41
     */
42
    protected $bccAddresses;
43
44
    /**
45
     * @var string
46
     */
47
    protected $bccName = null;
48
49
    /**
50
     * @var string|array
51
     */
52
    protected $ccAddresses;
53
54
    /**
55
     * @var string
56
     */
57
    protected $ccName = null;
58
59
    /**
60
     * @var string|array
61
     */
62
    protected $replyToAddresses;
63
64
    /**
65
     * @var string
66
     */
67
    protected $replyToName = null;
68
69
    /**
70
     * @var int
71
     */
72
    protected $maxLineLength = 1000;
73
74
    /**
75
     * @var int
76
     */
77
    protected $priority;
78
79
    /**
80
     * @var string
81
     */
82
    protected $readReceiptTo;
83
84
    /**
85
     * @var string
86
     */
87
    protected $returnPath;
88
89
    /**
90
     * SwiftTwigMailGenerator constructor.
91
     *
92
     * @param \Twig_Environment $twig_Environment
93
     * @param string            $twigPath
94
     */
95 4
    public function __construct(\Twig_Environment $twig_Environment, string $twigPath)
96
    {
97 4
        $this->twigEnvironment = $twig_Environment;
98 4
        $this->twigPath = $twigPath;
99 4
    }
100
101
    /**
102
     * @param array $data
103
     *
104
     * @return \Swift_Message
105
     */
106 4
    public function renderMail(array $data = []) :\Swift_Message
107
    {
108 4
        $mail = new \Swift_Message();
109
110 4
        $twigEnvironment = clone $this->twigEnvironment;
111 4
        $function = new \Twig_SimpleFunction('embedImage', function ($imgPath) use ($mail) {
112
            return $mail->embed(\Swift_Image::fromPath($imgPath));
113 4
        });
114 4
        $twigEnvironment->addFunction($function);
115
116 4
        $template = $twigEnvironment->loadTemplate($this->twigPath);
117
118 4
        if (!$template->hasBlock('subject') || (!$template->hasBlock('body_html') && !$template->hasBlock('body_text'))) {
119 1
            throw MissingBlockException::missingBlock($template->getBlockNames());
120
        }
121
122 3
        $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...
123 3
        $mail->setSubject($subject);
124
125 3
        if ($template->hasBlock('body_html')) {
126 2
            $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...
127 2
            if (!$template->hasBlock('body_text')) {
128 1
                $bodyText = $this->removeHtml($bodyHtml);
129
            } else {
130 1
                $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...
131
            }
132
133 2
            $mail->setBody($bodyHtml, 'text/html');
134 2
            $mail->addPart($bodyText, 'text/plain');
135
        } else {
136 1
            $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...
137
138 1
            $mail->setBody($bodyText, 'text/plain');
139
        }
140
141 3
        $i = 0;
142 3
        while($i != 10) {
143
            switch (true) {
144 3
                case $this->fromAddresses && $i < 1 :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
145 1
                    $i = 1;
146 1
                    $mail->setFrom($this->fromAddresses, $this->fromName);
147 1
                    $mail->setSender($this->fromAddresses, $this->fromName);
148 1
                    break;
149 3
                case $this->toAddresses && $i < 2 :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
150 1
                    $i = 2;
151 1
                    $mail->setTo($this->toAddresses, $this->toName);
152 1
                    break;
153 3
                case $this->bccAddresses && $i < 3 :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
154 1
                    $i = 3;
155 1
                    $mail->setBcc($this->bccAddresses, $this->bccName);
156 1
                    break;
157 3
                case $this->ccAddresses && $i < 4 :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
158 1
                    $i = 4;
159 1
                    $mail->setCc($this->ccAddresses, $this->ccName);
160 1
                    break;
161 3
                case $this->replyToAddresses && $i < 5 :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
162 1
                    $i = 5;
163 1
                    $mail->setReplyTo($this->replyToAddresses, $this->replyToName);
164 1
                    break;
165 3
                case $this->maxLineLength && $i < 6 :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
166 3
                    $i = 6;
167 3
                    $mail->setMaxLineLength($this->maxLineLength);
168 3
                    break;
169 3
                case $this->priority && $i < 7 :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
170 1
                    $i = 7;
171 1
                    $mail->setPriority($this->priority);
172 1
                    break;
173 3
                case $this->readReceiptTo && $i < 8 :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
174 1
                    $i = 8;
175 1
                    $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...
176 1
                    break;
177 3
                case $this->returnPath && $i < 9 :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
178 1
                    $i = 9;
179 1
                    $mail->setReturnPath($this->returnPath);
180 1
                    break;
181
                default:
182 3
                    $i = 10;
183 3
                    break;
184
            }
185
        }
186
187 3
        return $mail;
188
    }
189
190
    /**
191
     * @param array|string $fromAddresses
192
     */
193 1
    public function setFromAddresses($fromAddresses)
194
    {
195 1
        $this->fromAddresses = $fromAddresses;
196 1
    }
197
198
    /**
199
     * @param string $fromName
200
     */
201 1
    public function setFromName($fromName)
202
    {
203 1
        $this->fromName = $fromName;
204 1
    }
205
206
    /**
207
     * @param array|string $toAddresses
208
     */
209 1
    public function setToAddresses($toAddresses)
210
    {
211 1
        $this->toAddresses = $toAddresses;
212 1
    }
213
214
    /**
215
     * @param string $toName
216
     */
217 1
    public function setToName($toName)
218
    {
219 1
        $this->toName = $toName;
220 1
    }
221
222
    /**
223
     * @param array|string $bccAddresses
224
     */
225 1
    public function setBccAddresses($bccAddresses)
226
    {
227 1
        $this->bccAddresses = $bccAddresses;
228 1
    }
229
230
    /**
231
     * @param string $bccName
232
     */
233 1
    public function setBccName($bccName)
234
    {
235 1
        $this->bccName = $bccName;
236 1
    }
237
238
    /**
239
     * @param array|string $ccAddresses
240
     */
241 1
    public function setCcAddresses($ccAddresses)
242
    {
243 1
        $this->ccAddresses = $ccAddresses;
244 1
    }
245
246
    /**
247
     * @param string $ccName
248
     */
249 1
    public function setCcName($ccName)
250
    {
251 1
        $this->ccName = $ccName;
252 1
    }
253
254
    /**
255
     * @param array|string $replyToAddresses
256
     */
257 1
    public function setReplyToAddresses($replyToAddresses)
258
    {
259 1
        $this->replyToAddresses = $replyToAddresses;
260 1
    }
261
262
    /**
263
     * @param string $replyToName
264
     */
265 1
    public function setReplyToName($replyToName)
266
    {
267 1
        $this->replyToName = $replyToName;
268 1
    }
269
270
    /**
271
     * @param int $maxLineLength
272
     */
273 1
    public function setMaxLineLength($maxLineLength)
274
    {
275 1
        $this->maxLineLength = $maxLineLength;
276 1
    }
277
278
    /**
279
     * @param int $priority
280
     */
281 1
    public function setPriority($priority)
282
    {
283 1
        $this->priority = $priority;
284 1
    }
285
286
    /**
287
     * @param string $readReceiptTo
288
     */
289 1
    public function setReadReceiptTo($readReceiptTo)
290
    {
291 1
        $this->readReceiptTo = $readReceiptTo;
292 1
    }
293
294
    /**
295
     * @param string $returnPath
296
     */
297 1
    public function setReturnPath($returnPath)
298
    {
299 1
        $this->returnPath = $returnPath;
300 1
    }
301
302
    /**
303
     * Removes the HTML tags from the text.
304
     *
305
     * @param string $s
306
     * @param string $keep   The list of tags to keep
307
     * @param string $expand The list of tags to remove completely, along their content
308
     */
309 1
    private function removeHtml(string $s, string $keep = '', string $expand = 'script|style|noframes|select|option') :string
310
    {
311
        /**///prep the string
312 1
        $s = ' '.$s;
313
314
        /**///initialize keep tag logic
315 1
        if (strlen($keep) > 0) {
316
            $k = explode('|', $keep);
317
            $s = $this->keepTag($s, $k, '<', '[{(');
318
        }
319
        //begin removal
320
        /**///remove comment blocks
321 1
        $s = $this->removeElement($s, '<!--', '-->');
322
323
        /**///remove tags with content between them
324 1
        if (strlen($expand) > 0) {
325 1
            $e = explode('|', $expand);
326 1
            $count = count($e);
327 1
            $pos = [];
328 1
            $len = [];
329 1
            for ($i = 0;$i < $count;++$i) {
330 1
                while (stripos($s, '<'.$e[$i]) > 0) {
331
                    $len[1] = strlen('<'.$e[$i]);
332
                    $pos[1] = stripos($s, '<'.$e[$i]);
333
                    $pos[2] = stripos($s, $e[$i].'>', $pos[1] + $len[1]);
334
                    $len[2] = $pos[2] - $pos[1] + $len[1];
335
                    $x = substr($s, $pos[1], $len[2]);
336
                    $s = str_replace($x, '', $s);
337
                }
338
            }
339
        }
340
341
        /**///remove remaining tags
342 1
        $s = $this->removeElement($s, '<', '>');
343
344
        /**///finalize keep tag
345 1
        if (isset($k)) {
346
            $s = $this->keepTag($s, $k, '[{(', '<');
347
        }
348
349 1
        return trim($s);
350
    }
351
352
    /**
353
     * @param string $s
354
     * @param string $openTag
355
     * @param string $closeTag
356
     *
357
     * @return mixed|string
358
     */
359 1
    private function removeElement(string $s, string $openTag, string $closeTag)
360
    {
361 1
        $pos = [];
362 1
        $len = [];
363 1
        while (stripos($s, $openTag) > 0) {
364 1
            $pos[1] = stripos($s, $openTag);
365 1
            $pos[2] = stripos($s, $closeTag, $pos[1]);
366 1
            $len[1] = $pos[2] - $pos[1] + 1;
367 1
            $x = substr($s, $pos[1], $len[1]);
368 1
            $s = str_replace($x, '', $s);
369
        }
370
371 1
        return $s;
372
    }
373
374
    /**
375
     * @param string $s
376
     * @param array  $tagToKeep
377
     * @param string $initial
378
     * @param string $finalize
379
     *
380
     * @return string
381
     */
382
    private function keepTag(string $s, array $tagToKeep, string $initial, string $finalize):string
383
    {
384
        $count = count($tagToKeep);
385
        for ($i = 0;$i < $count;++$i) {
386
            $s = str_replace($initial.$tagToKeep[$i], $finalize.$tagToKeep[$i], $s);
387
            $s = str_replace($initial.'/'.$tagToKeep[$i], $finalize.'/'.$tagToKeep[$i], $s);
388
        }
389
390
        return $s;
391
    }
392
}
393