Passed
Pull Request — master (#50)
by Ronan
09:06
created

Email   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 294
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 21
eloc 56
c 1
b 0
f 0
dl 0
loc 294
rs 10

18 Methods

Rating   Name   Duplication   Size   Complexity  
A addCc() 0 5 1
A setFrom() 0 5 1
A setFromName() 0 5 1
A getFromName() 0 3 1
A setSubject() 0 5 1
A getTemplateHtml() 0 7 2
A getDefaultTemplateName() 0 27 2
A getTemplateText() 0 7 2
A getFrom() 0 3 1
A getTo() 0 3 1
A getSubject() 0 3 1
A addTemplateContext() 0 5 1
A getTemplateContext() 0 3 1
A getBcc() 0 3 1
A getCc() 0 3 1
A addBcc() 0 5 1
A addTo() 0 4 1
A setTemplateContext() 0 5 1
1
<?php
2
3
namespace App\Mail;
4
5
/**
6
 * The Email class represents a single email
7
 *
8
 * @author Ronan Chilvers <[email protected]>
9
 */
10
class Email
11
{
12
    /**
13
     * @var string
14
     */
15
    protected $namespace = 'App\Mail';
16
17
    /**
18
     * @var string
19
     */
20
    protected $from;
21
22
    /**
23
     * @var string
24
     */
25
    protected $fromName = null;
26
27
    /**
28
     * @array
29
     */
30
    protected $to = [];
31
32
    /**
33
     * @var array
34
     */
35
    protected $cc = [];
36
37
    /**
38
     * @var array
39
     */
40
    protected $bcc = [];
41
42
    /**
43
     * @var string
44
     */
45
    protected $subject;
46
47
    /**
48
     * @var string
49
     */
50
    protected $templateHtml = null;
51
52
    /**
53
     * @var string
54
     */
55
    protected $templateText = null;
56
57
    /**
58
     * @var array
59
     */
60
    protected $templateContext = [];
61
62
    /**
63
     * Get the from address
64
     *
65
     * @return string
66
     * @author Ronan Chilvers <[email protected]>
67
     */
68
    public function getFrom()
69
    {
70
        return $this->from;
71
    }
72
73
    /**
74
     * Set the from address
75
     *
76
     * @param string $value
77
     * @author Ronan Chilvers <[email protected]>
78
     */
79
    public function setFrom($value)
80
    {
81
        $this->from = $value;
82
83
        return $this;
84
    }
85
86
    /**
87
     * Get the from name
88
     *
89
     * @return string
90
     * @author Ronan Chilvers <[email protected]>
91
     */
92
    public function getFromName()
93
    {
94
        return $this->fromName;
95
    }
96
97
    /**
98
     * Set the from name
99
     *
100
     * @param string $value
101
     * @author Ronan Chilvers <[email protected]>
102
     */
103
    public function setFromName($value)
104
    {
105
        $this->fromName = $value;
106
107
        return $this;
108
    }
109
110
    /**
111
     * Get the 'to' addresses
112
     *
113
     * @return array
114
     * @author Ronan Chilvers <[email protected]>
115
     */
116
    public function getTo()
117
    {
118
        return $this->to;
119
    }
120
121
    /**
122
     * Set a 'to' address
123
     *
124
     * @author Ronan Chilvers <[email protected]>
125
     */
126
    public function addTo($email, $name = null)
127
    {
128
        $this->to[$email] = $name;
129
        return $this;
130
    }
131
132
    /**
133
     * Get the 'cc' addresses
134
     *
135
     * @return array
136
     * @author Ronan Chilvers <[email protected]>
137
     */
138
    public function getCc()
139
    {
140
        return $this->cc;
141
    }
142
143
    /**
144
     * Set a 'cc' address
145
     *
146
     * @author Ronan Chilvers <[email protected]>
147
     */
148
    public function addCc($email, $name = null)
149
    {
150
        $this->cc[$email] = $name;
151
152
        return $this;
153
    }
154
155
    /**
156
     * Get the 'bcc' addresses
157
     *
158
     * @return array
159
     * @author Ronan Chilvers <[email protected]>
160
     */
161
    public function getBcc()
162
    {
163
        return $this->bcc;
164
    }
165
166
    /**
167
     * Set a 'cc' address
168
     *
169
     * @author Ronan Chilvers <[email protected]>
170
     */
171
    public function addBcc($email, $name = null)
172
    {
173
        $this->bcc[$email] = $name;
174
175
        return $this;
176
    }
177
178
    /**
179
     * Get the subject line
180
     *
181
     * @return string
182
     * @author Ronan Chilvers <[email protected]>
183
     */
184
    public function getSubject()
185
    {
186
        return $this->subject;
187
    }
188
189
    /**
190
     * Set the subject for this email
191
     *
192
     * @param string
193
     * @author Ronan Chilvers <[email protected]>
194
     */
195
    public function setSubject($subject)
196
    {
197
        $this->subject = $subject;
198
199
        return $this;
200
    }
201
202
    /**
203
     * Get the HTML template filename for this email
204
     *
205
     * @return string
206
     * @author Ronan Chilvers <[email protected]>
207
     */
208
    public function getTemplateHtml()
209
    {
210
        if (is_null($this->templateHtml)) {
0 ignored issues
show
introduced by
The condition is_null($this->templateHtml) is always false.
Loading history...
211
            $this->templateHtml = $this->getDefaultTemplateName() . '.html.twig';
212
        }
213
214
        return $this->templateHtml;
215
    }
216
217
    /**
218
     * Get the plain text  template filename for this email
219
     *
220
     * @return string
221
     * @author Ronan Chilvers <[email protected]>
222
     */
223
    public function getTemplateText()
224
    {
225
        if (is_null($this->templateText)) {
0 ignored issues
show
introduced by
The condition is_null($this->templateText) is always false.
Loading history...
226
            $this->templateText = $this->getDefaultTemplateName() . '.txt.twig';
227
        }
228
229
        return $this->templateText;
230
    }
231
232
    /**
233
     * Get the template context
234
     *
235
     * @return array
236
     * @author Ronan Chilvers <[email protected]>
237
     */
238
    public function getTemplateContext()
239
    {
240
        return $this->templateContext;
241
    }
242
243
    /**
244
     * Set the template context
245
     *
246
     * @param  array $value
247
     * @author Ronan Chilvers <[email protected]>
248
     */
249
    public function setTemplateContext($value)
250
    {
251
        $this->templateContext = $value;
252
253
        return $this;
254
    }
255
256
    /**
257
     * Add a key to the template context
258
     *
259
     * @param string $key
260
     * @param mixed $value
261
     * @return static
262
     * @author Ronan Chilvers <[email protected]>
263
     */
264
    public function addTemplateContext($key, $value)
265
    {
266
        $this->templateContext[$key] = $value;
267
268
        return $this;
269
    }
270
271
    /**
272
     * Get the default template name
273
     *
274
     * @return string
275
     * @author Ronan Chilvers <[email protected]>
276
     */
277
    protected function getDefaultTemplateName()
278
    {
279
        $name = str_replace(
280
            [$this->namespace . '\\'],
281
            [''],
282
            get_called_class()
283
        );
284
        if ('Mail' == substr($name, -4)) {
285
            $name = substr(
286
                $name,
287
                0,
288
                -4
289
            );
290
        }
291
        $name = explode(
292
            '\\',
293
            $name
294
        );
295
        array_walk(
296
            $name,
297
            function (&$val) {
298
                $val = lcfirst($val);
299
            }
300
        );
301
        $name = '@emails/' . implode('/', $name);
302
303
        return $name;
304
    }
305
}
306