Part   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 242
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 4
Bugs 0 Features 1
Metric Value
wmc 17
c 4
b 0
f 1
lcom 2
cbo 3
dl 0
loc 242
ccs 20
cts 20
cp 1
rs 10

14 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 2
A __call() 0 11 3
A processTemplate() 0 7 1
A setType() 0 5 1
A setId() 0 5 1
A setDisposition() 0 5 1
A setDescription() 0 5 1
A setFileName() 0 5 1
A setCharset() 0 5 1
A setBoundary() 0 5 1
A setLocation() 0 5 1
A setLanguage() 0 5 1
A getHeaders() 0 4 1
A getContent() 0 4 1
1
<?php
2
3
/**
4
 * This file is part of slick/mail package
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace Slick\Mail\Mime;
11
12
use Slick\Mail\ContentFromTemplateMethods;
13
use Slick\Mail\Mime;
14
use Slick\Template\TemplateEngineInterface;
15
16
/**
17
 * Part
18
 *
19
 * @package Slick\Mail\Mime
20
 * @author  Filipe Silva <[email protected]>
21
 *
22
 * @method string getType()
23
 * @method string getId()
24
 * @method string getDisposition()
25
 * @method string getDescription()
26
 * @method string getFileName()
27
 * @method string getCharset()
28
 * @method string getBoundary()
29
 * @method string getLocation()
30
 * @method string getLanguage()
31
 * @method string getHeadersArray($EOL = Mime::LINEEND)
32
 * @method Part   setEncoding(string $encoding)
33
 * @method string getEncoding()
34
 */
35
class Part implements MimePartInterface
36
{
37
38
    /**
39
     * @var \Zend\Mime\Part
40
     */
41
    protected $mimePart;
42
43
    /**
44
     * @var string
45
     */
46
    protected $template;
47
48
    /**
49
     * @var array
50
     */
51
    protected $data;
52
53
    /**
54
     * @var TemplateEngineInterface
55
     */
56
    protected $templateEngine;
57
58
    /**
59
     * To load content from twig templates
60
     */
61
    use ContentFromTemplateMethods;
62
63
    /**
64
     * Part
65
     *
66
     * Loads a template or a file for this part
67
     *
68
     * @param string $template
69
     * @param array  $data
70
     *
71
     */
72 12
    public function __construct($template, $data = [])
73
    {
74 12
        $this->data = $data;
75 12
        $this->template = $template;
76 12
        $content = (file_exists($template))
77 6
            ? fopen("file://$template", 'r', true)
78 12
            : $this->processTemplate();
79 12
        $this->mimePart = new \Zend\Mime\Part($content);
80 12
    }
81
82
    /**
83
     * Magic method to handle Zend\Mime\Part method calls
84
     *
85
     * @param string $name
86
     * @param array  $arguments
87
     *
88
     * @return mixed
89
     */
90 6
    public function __call($name, $arguments)
91
    {
92 6
        if (!method_exists($this->mimePart, $name)) {
93 2
            throw new \BadMethodCallException("Undefined method call.");
94
        }
95
96 4
        $return = call_user_func_array([$this->mimePart, $name], $arguments);
97 4
        return ($return instanceOf \Zend\Mime\Part)
98 3
            ? $this
99 4
            : $return;
100
    }
101
102
    /**
103
     * Get content from template
104
     *
105
     * @return string
106
     */
107 12
    protected function processTemplate()
108
    {
109 12
        $text = $this->getTemplateEngine()
110 12
            ->parse($this->template)
111 12
            ->process($this->data);
112 12
        return trim($text);
113
    }
114
115
    /**
116
     * Set type
117
     *
118
     * @param string $type
119
     *
120
     * @return MimePartInterface
121
     *
122
     * @codeCoverageIgnore
123
     */
124
    public function setType($type = Mime::TYPE_OCTETSTREAM)
125
    {
126
        $this->mimePart->setType($type);
127
        return $this;
128
    }
129
130
    /**
131
     * Set id
132
     *
133
     * @param string $id
134
     *
135
     * @return MimePartInterface
136
     *
137
     * @codeCoverageIgnore
138
     */
139
    public function setId($id)
140
    {
141
        $this->mimePart->setId($id);
142
        return $this;
143
    }
144
145
    /**
146
     * Set disposition
147
     *
148
     * @param string $disposition
149
     *
150
     * @return MimePartInterface
151
     *
152
     * @codeCoverageIgnore
153
     */
154
    public function setDisposition($disposition)
155
    {
156
        $this->mimePart->setDisposition($disposition);
157
        return $this;
158
    }
159
160
    /**
161
     * Set description
162
     *
163
     * @param string $description
164
     *
165
     * @return MimePartInterface
166
     *
167
     * @codeCoverageIgnore
168
     */
169
    public function setDescription($description)
170
    {
171
        $this->setDescription($description);
172
        return $this;
173
    }
174
175
    /**
176
     * Set filename
177
     *
178
     * @param string $fileName
179
     *
180
     * @return MimePartInterface
181
     *
182
     * @codeCoverageIgnore
183
     */
184
    public function setFileName($fileName)
185
    {
186
        $this->mimePart->setFileName($fileName);
187
        return $this;
188
    }
189
190
    /**
191
     * Set charset
192
     *
193
     * @param string $charset
194
     *
195
     * @return MimePartInterface
196
     *
197
     * @codeCoverageIgnore
198
     */
199
    public function setCharset($charset)
200
    {
201
        $this->mimePart->setCharset($charset);
202
        return $this;
203
    }
204
205
    /**
206
     * Set boundary
207
     *
208
     * @param string $boundary
209
     *
210
     * @return MimePartInterface
211
     *
212
     * @codeCoverageIgnore
213
     */
214
    public function setBoundary($boundary)
215
    {
216
        $this->mimePart->setBoundary($boundary);
217
        return $this;
218
    }
219
220
    /**
221
     * Set location
222
     *
223
     * @param string $location
224
     *
225
     * @return MimePartInterface
226
     *
227
     * @codeCoverageIgnore
228
     */
229
    public function setLocation($location)
230
    {
231
        $this->mimePart->setLocation($location);
232
        return $this;
233
    }
234
235
    /**
236
     * Set language
237
     *
238
     * @param string $language
239
     *
240
     * @return MimePartInterface
241
     *
242
     * @codeCoverageIgnore
243
     */
244
    public function setLanguage($language)
245
    {
246
        $this->mimePart->setLanguage($language);
247
        return $this;
248
    }
249
250
    /**
251
     * Return the headers for this part as a string
252
     *
253
     * @param string $eol
254
     *
255
     * @return String
256
     *
257
     * @codeCoverageIgnore
258
     */
259
    public function getHeaders($eol = Mime::LINEEND)
260
    {
261
        return $this->mimePart->getHeaders($eol);
262
    }
263
264
    /**
265
     * Get the Content of the current Mime Part in the given encoding.
266
     *
267
     * @param string $eol
268
     * @return string
269
     *
270
     * @codeCoverageIgnore
271
     */
272
    public function getContent($eol = Mime::LINEEND)
273
    {
274
        return $this->mimePart->getContent($eol);
275
    }
276
}