Completed
Push — dev-master ( 0413d5...ea7d5a )
by Derek Stephen
06:04
created

MailService::setTransport()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Bone\Service;
4
5
use Zend\Mail;
6
use Zend\Mail\Transport\TransportInterface;
7
use Zend\Mime\Message;
8
use Zend\Mime\Part;
9
10
class MailService
11
{
12
    /** @var  Mail\Message */
13
    private $mail;
14
    /** @var  Mail\Transport\TransportInterface */
15
    private $transport;
16
17
    private $header;
18
    private $footer;
19
20
    private $message;
21
    private $from;
22
    private $to;
23
    private $subject;
24
25
26
    /**
27
     * @param array|null $config
28
     */
29 2
    public function __construct(array $config = null)
30 2
    {
31 2
        $this->mail = new Mail\Message();
32
33 2
        $this->transport = isset($config['transport']) ? $config['transport'] : new Mail\Transport\Sendmail();
34 2
        isset($config['from']) ? $this->mail->setTo($config['from']) : null;
35 2
        isset($config['to']) ? $this->mail->setTo($config['to']) : null;
36 2
        isset($config['subject']) ? $this->mail->setTo($config['subject']) : null;
37 2
        isset($config['message']) ? $this->mail->setTo($config['message']) : null;
38 2
    }
39
40
    /**
41
     * @param TransportInterface $transport
42
     */
43 2
    public function setTransport(TransportInterface $transport)
44 2
    {
45 2
        $this->transport = $transport;
46 2
    }
47
48
    /**
49
     * @return bool
50
     */
51 1
    public function send()
52 1
    {
53 1
        $msg = new Part($this->header.$this->message.$this->footer);
54 1
        $msg->type = 'text/html';
55 1
        $mime = new Message();
56 1
        $mime->setParts([$msg]);
57 1
        $this->mail->setBody($mime)
58 1
            ->setFrom($this->from)
59 1
            ->setTo($this->to)
60 1
            ->setSubject($this->subject);
61
62 1
        $this->transport->send($this->mail);
63 1
        return true;
64
    }
65
66
    /**
67
     * @param string $header
68
     * @return MailService
69
     */
70 1
    public function setHeader($header)
71 1
    {
72 1
        $this->header = $header;
73 1
        return $this;
74
    }
75
76
    /**
77
     * @param string $footer
78
     * @return MailService
79
     */
80 1
    public function setFooter($footer)
81 1
    {
82 1
        $this->footer = $footer;
83 1
        return $this;
84
    }
85
86
    /**
87
     * @param $message
88
     * @return $this
89
     */
90 1
    public function setMessage($message)
91 1
    {
92 1
        $this->message = $message;
93 1
        return $this;
94
    }
95
96
    /**
97
     * @param $from
98
     * @return $this
99
     */
100 1
    public function setFrom($from)
101 1
    {
102 1
        $this->from = $from;
103 1
        return $this;
104
    }
105
106
    /**
107
     * @param $to
108
     * @return $this
109
     */
110 1
    public function setTo($to)
111 1
    {
112 1
        $this->to = $to;
113 1
        return $this;
114
    }
115
116
    /**
117
     * @param $subject
118
     * @return $this
119
     */
120 1
    public function setSubject($subject)
121 1
    {
122 1
        $this->subject = $subject;
123 1
        return $this;
124
    }
125
}