Zenc_EmailLogger_Model_Zend_Mail_Logger::getLog()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 8
rs 9.4285
cc 2
eloc 4
nc 2
nop 0
1
<?php
2
3
class Zenc_EmailLogger_Model_Zend_Mail_Logger extends Zend_Mail
4
{
5
    /**
6
     * @var Zenc_EmailLogger_Model_Log
7
     */
8
    private $_log;
9
10
    /**
11
     * Public constructor
12
     *
13
     * @param string $charset
14
     */
15
    public function __construct($charset = null)
16
    {
17
        if (empty($charset)) {
18
            $charset = 'utf-8';
19
        }
20
21
        parent::__construct($charset);
22
    }
23
24
    /**
25
     * @return Zenc_EmailLogger_Model_Log
26
     */
27
    public function getLog()
28
    {
29
        if (!isset($this->_log)) {
30
            $this->_log = Mage::getModel('zenc_emaillogger/log');
31
        }
32
33
        return $this->_log;
34
    }
35
36
    /**
37
     * @param Zend_Mail_Transport_Abstract $transport
38
     *
39
     * @return Zenc_EmailLogger_Model_Zend_Mail_Transport_Logger
40
     */
41
    public function getTransportLoggerInstance(Zend_Mail_Transport_Abstract $transport = null)
42
    {
43
        return Mage::getModel('zenc_emaillogger/zend_mail_transport_logger', $transport);
44
    }
45
46
    /**
47
     * Sends this email using the given transport or a previously
48
     * set DefaultTransport or the internal mail function if no
49
     * default transport had been set.
50
     *
51
     * @param Zend_Mail_Transport_Abstract $transport
52
     *
53
     * @return Zenc_EmailLogger_Zend_Mail_Logger Provides fluent interface
54
     */
55
    public function send($transport = null)
56
    {
57
        return parent::send($this->getTransportLoggerInstance($transport));
58
    }
59
60
    /**
61
     * Helper function for adding a recipient and the corresponding header
62
     *
63
     * @param string $headerName
64
     * @param string $email
65
     * @param string $name
66
     */
67
    protected function _addRecipientAndHeader($headerName, $email, $name)
68
    {
69
        if (!$this->getLog()->hasToEmail() && $headerName === 'To') {
70
            $this->getLog()->setToEmail($email);
71
            $this->getLog()->setToName($this->_decodeBase64Header($name));
72
        } else {
73
            $this->getLog()->addRecipient(
74
                $headerName,
75
                $email,
76
                $this->_decodeBase64Header($name)
77
            );
78
        }
79
80
        parent::_addRecipientAndHeader($headerName, $email, $name);
81
    }
82
83
    /**
84
     * Sets the subject of the message
85
     *
86
     * @param string $subject
87
     *
88
     * @return Zenc_EmailLogger_Zend_Mail_Logger Provides fluent interface
89
     *
90
     * @throws Zend_Mail_Exception
91
     */
92
    public function setSubject($subject)
93
    {
94
        $this->getLog()->setSubject(
95
            $this->_decodeBase64Header($subject)
96
        );
97
98
        return parent::setSubject($subject);
99
    }
100
101
    /**
102
     * Sets From-header and sender of the message
103
     *
104
     * @param string $email
105
     * @param string $name
106
     *
107
     * @return Zenc_EmailLogger_Zend_Mail_Logger Provides fluent interface
108
     *
109
     * @throws Zend_Mail_Exception if called subsequent times
110
     */
111
    public function setFrom($email, $name = null)
112
    {
113
        $this->getLog()->setFromEmail($email);
114
        $this->getLog()->setFromName($name);
115
116
        return parent::setFrom($email, $name);
117
    }
118
119
    /**
120
     * Sets the Return-Path header of the message
121
     *
122
     * @param string $email
123
     * @return Zenc_EmailLogger_Zend_Mail_Logger Provides fluent interface
124
     *
125
     * @throws Zend_Mail_Exception if set multiple times
126
     */
127
    public function setReturnPath($email)
128
    {
129
        $this->getLog()->setReturnPath($email);
130
131
        return parent::setReturnPath($email);
132
    }
133
134
    /**
135
     * Set Reply-To Header
136
     *
137
     * @param string $email
138
     * @param string $name
139
     *
140
     * @return Zenc_EmailLogger_Zend_Mail_Logger
141
     *
142
     * @throws Zend_Mail_Exception if called more than one time
143
     */
144
    public function setReplyTo($email, $name = null)
145
    {
146
        $this->getLog()->setReplyToEmail($email);
147
        $this->getLog()->setReplyToName($name);
148
149
        return parent::setReplyTo($email, $name);
150
    }
151
152
    /**
153
     * Sets the text body for the message.
154
     *
155
     * @param string $txt
156
     * @param string $charset
157
     * @param string $encoding
158
     *
159
     * @return Zenc_EmailLogger_Zend_Mail_Logger Provides fluent interface
160
     */
161
    public function setBodyText($txt, $charset = null, $encoding = Zend_Mime::ENCODING_QUOTEDPRINTABLE)
162
    {
163
        $this->getLog()->setBodyText($txt);
164
165
        return parent::setBodyText($txt, $charset, $encoding);
166
    }
167
168
    /**
169
     * Sets the HTML body for the message
170
     *
171
     * @param string $html
172
     * @param string $charset
173
     * @param string $encoding
174
     *
175
     * @return Zenc_EmailLogger_Zend_Mail_Logger Provides fluent interface
176
     */
177
    public function setBodyHtml($html, $charset = null, $encoding = Zend_Mime::ENCODING_QUOTEDPRINTABLE)
178
    {
179
        $this->getLog()->setBodyHtml($html);
180
181
        return parent::setBodyHtml($html, $charset, $encoding);
182
    }
183
184
    private function _decodeBase64Header($header)
185
    {
186
        $prefix = '=?' . $this->getCharset() . '?B?';
187
        $suffix = '?=';
188
        if (strpos($header, $prefix) !== false) {
189
            $base64 = substr($header, strlen($prefix), strlen($suffix) * -1);
190
            return base64_decode($base64);
191
        }
192
193
        return $header;
194
    }
195
}
196