EmailService::send()   B
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 30
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 30
ccs 22
cts 22
cp 1
rs 8.8571
cc 3
eloc 21
nc 4
nop 5
crap 3
1
<?php
2
/**
3
 * Copyright (c) 2014 Roave, LLC.
4
 * All rights reserved.
5
 *
6
 * Redistribution and use in source and binary forms, with or without
7
 * modification, are permitted provided that the following conditions
8
 * are met:
9
 *
10
 *   * Redistributions of source code must retain the above copyright
11
 *     notice, this list of conditions and the following disclaimer.
12
 *
13
 *   * Redistributions in binary form must reproduce the above copyright
14
 *     notice, this list of conditions and the following disclaimer in
15
 *     the documentation and/or other materials provided with the
16
 *     distribution.
17
 *
18
 *   * Neither the names of the copyright holders nor the names of the
19
 *     contributors may be used to endorse or promote products derived
20
 *     from this software without specific prior written permission.
21
 *
22
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33
 * POSSIBILITY OF SUCH DAMAGE.
34
 *
35
 * @author Antoine Hedgecock
36
 *
37
 * @copyright 2014 Roave, LLC
38
 * @license http://www.opensource.org/licenses/bsd-license.php  BSD License
39
 */
40
41
namespace Roave\EmailTemplates\Service;
42
43
use Zend\Mime\Part as MimePart;
44
use Zend\Mime\Message as MimeMessage;
45
use Zend\Mail\Message as MailMessage;
46
use Zend\Mail\Transport\TransportInterface;
47
use Zend\Stdlib\ArrayUtils;
48
use Roave\EmailTemplates\Options\EmailServiceOptions;
49
50
/**
51
 * Class EmailService
52
 */
53
class EmailService implements EmailServiceInterface
54
{
55
    /**
56
     * @var EmailServiceOptions
57
     */
58
    protected $options;
59
60
    /**
61
     * @var TemplateServiceInterface
62
     */
63
    protected $templates;
64
65
    /**
66
     * @var TransportInterface
67
     */
68
    protected $transport;
69
70
    /**
71
     * @param TemplateServiceInterface $templates
72
     * @param EmailServiceOptions      $options
73
     */
74
    public function __construct(TemplateServiceInterface $templates, EmailServiceOptions $options = null)
75
    {
76
        $this->options   = $options ?: new EmailServiceOptions();
77
        $this->templates = $templates;
78
    }
79
80
    /**
81
     * @param TransportInterface $transport
82
     */
83 1
    public function setTransport(TransportInterface $transport = null)
84
    {
85 1
        $this->transport = $transport;
86 1
    }
87
88
    /**
89
     * Get the mail transport
90
     *
91
     * @return TransportInterface
92
     */
93 1
    public function getTransport()
94
    {
95 1
        if ($this->transport === null) {
96 1
            $className = $this->options->getDefaultTransport();
97 1
            $this->transport = new $className;
98
        }
99
100 1
        return $this->transport;
101
    }
102
103 3
    public function send($email, $templateId, $params = [], $locale = null, $replyTo = null)
104
    {
105 3
        $params = ArrayUtils::iteratorToArray($params);
106 3
        $locale = $locale ?: $this->options->getDefaultLocale();
107 3
        $replyTo = $replyTo ?: $this->options->getReplyTo();
108
109 3
        list ($subject, $html, $text) = $this->templates->render($templateId, $locale, $params);
110
111 3
        $htmlPart = new MimePart($html);
112 3
        $htmlPart->type = 'text/html';
113
114 3
        $textPart = new MimePart($text);
115 3
        $textPart->type = 'text/plain';
116
117 3
        $body = new MimeMessage();
118 3
        $body->setParts([$htmlPart, $textPart]);
119
120 3
        $message = new MailMessage();
121 3
        $message->setEncoding($this->options->getEncoding());
122 3
        $message->setBody($body);
123
124 3
        $message->setSubject($subject);
125 3
        $message->setReplyTo($replyTo);
126 3
        $message->setFrom($this->options->getFrom());
127 3
        $message->setTo($email);
128 3
        $message->setBcc($this->options->getBcc());
129 3
        $message->getHeaders()->get('content-type')->setType('multipart/alternative');
130
131 3
        $this->getTransport()->send($message);
132 3
    }
133
}
134