Completed
Pull Request — 3.0 (#5)
by Huberty
03:23 queued 01:31
created

Mail::autoCreateBodyText()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
namespace Mouf\Utils\Mailer;
3
4
use Html2Text\Html2Text;
5
use Pelago\Emogrifier;
6
7
/**
8
 * This class represents a mail to be sent using a Mailer class extending the MailerInterface.
9
 * + it has special features to add a text mail for any HTML mail that has not been provided the text mail.
10
 * 
11
 * Note: default encoding for the mail is UTF-8 if not specified.
12
 * 
13
 * @Component
14
 */
15
class Mail implements MailInterface {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The type Mouf\Utils\Mailer\Mail has been defined more than once; this definition is ignored, only the first definition in src/Mouf/Mail.php (L15-301) is considered.

This check looks for classes that have been defined more than once.

If you can, we would recommend to use standard object-oriented programming techniques. For example, to avoid multiple types, it might make sense to create a common interface, and then multiple, different implementations for that interface.

This also has the side-effect of providing you with better IDE auto-completion, static analysis and also better OPCode caching from PHP.

Loading history...
16
	
17
	protected $bodyText;
18
	protected $bodyHtml;
19
	protected $title;
20
	protected $from;
21
	protected $toRecipients = array();
22
	protected $ccRecipients = array();
23
	protected $bccRecipients = array();
24
	protected $attachements = array();
25
	protected $encoding = "utf-8";
26
	protected $autocreateMissingText = true;
27
	protected $css;
28
	
29
	/**
30
	 * Returns the mail text body.
31
	 *
32
	 * @return string
33
	 */
34
	public function getBodyText() {
35
		if ($this->bodyText != null) {
36
			return $this->bodyText;
37
		} elseif ($this->autocreateMissingText == true) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
38
			return Html2Text::convert($this->getBodyHtml());
39
		}
40
	}
41
	
42
	/**
43
	 * The mail text body.
44
	 *
45
	 * @Property
46
	 * @param string $bodyText
47
	 */
48
	public function setBodyText($bodyText) {
49
		$this->bodyText = $bodyText;
50
	}
51
	
52
	/**
53
	 * Returns the HTML text before "emogrification".
54
	 * This method can be overwritten by subclasses to overwrite the mail body and still applying "emogrification".
55
	 * 
56
	 * @return string
57
	 */
58
	protected function getBodyHtmlBeforeEmogrify() {
59
		return $this->bodyHtml;
60
	}
61
	
62
	/**
63
	 * Returns the mail html body.
64
	 *
65
	 * @return string
66
	 */
67
	public function getBodyHtml() {
68
		if ($this->css) {
69
			$emogrifier = new Emogrifier($this->getBodyHtmlBeforeEmogrify(), $this->css);
0 ignored issues
show
Bug introduced by
The method getBodyHtmlBeforeEmogrify() does not exist on Mouf\Utils\Mailer\Mail. Did you maybe mean getBodyHtml()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
70
			$finalHtml = $emogrifier->emogrify();
71
		} else {
72
			$finalHtml = $this->getBodyHtmlBeforeEmogrify();
0 ignored issues
show
Bug introduced by
The method getBodyHtmlBeforeEmogrify() does not exist on Mouf\Utils\Mailer\Mail. Did you maybe mean getBodyHtml()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
73
		}
74
		
75
		return $finalHtml;
76
	}
77
78
	/**
79
	 * The mail html body.
80
	 *
81
	 * @Property
82
	 * @param string $bodyHtml
83
	 */
84
	public function setBodyHtml($bodyHtml) {
85
		$this->bodyHtml = $bodyHtml;
0 ignored issues
show
Documentation Bug introduced by
It seems like $bodyHtml of type string is incompatible with the declared type object<Mouf\Html\HtmlEle...t\HtmlElementInterface> of property $bodyHtml.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
86
	}
87
	
88
	/**
89
	 * Returns the mail title.
90
	 *
91
	 * @return string
92
	 */
93
	public function getTitle() {
94
		return $this->title;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->title; (Mouf\Html\HtmlElement\HtmlElementInterface) is incompatible with the return type declared by the interface Mouf\Utils\Mailer\MailInterface::getTitle of type string.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
95
	}
96
	
97
	/**
98
	 * The mail title.
99
	 *
100
	 * @Property
101
	 * @param string $title
102
	 */
103
	public function setTitle($title) {
104
		$this->title = $title;
0 ignored issues
show
Documentation Bug introduced by
It seems like $title of type string is incompatible with the declared type object<Mouf\Html\HtmlEle...t\HtmlElementInterface> of property $title.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
105
	}
106
	
107
	/**
108
	 * Returns the "From" email address
109
	 *
110
	 * @return MailAddressInterface The first element is the email address, the second the name to display.
111
	 */
112
	public function getFrom() {
113
		return $this->from;
114
	}
115
116
	/**
117
	 * The mail from address.
118
	 *
119
	 * @Property
120
	 * @param MailAddressInterface $from
121
	 */
122
	public function setFrom(MailAddressInterface $from) {
123
		$this->from = $from;
124
	}
125
	
126
	/**
127
	 * Returns an array containing the recipients.
128
	 *
129
	 * @return MailAddressInterface[]
130
	 */
131
	public function getToRecipients() {
132
		return $this->toRecipients;
133
	}
134
135
	/**
136
	 * An array containing the recipients.
137
	 *
138
	 * @Property
139
	 * @param MailAddressInterface[] $toRecipients
140
	 */
141
	public function setToRecipients(array $toRecipients) {
142
		$this->toRecipients = $toRecipients;
143
	}
144
	
145
	/**
146
	 * Adss a recipient.
147
	 *
148
	 * @param MailAddressInterface $toRecipient
149
	 */
150
	public function addToRecipient(MailAddressInterface $toRecipient) {
151
		$this->toRecipients[] = $toRecipient;
152
	}
153
	
154
	/**
155
	 * Returns an array containing the recipients in Cc.
156
	 *
157
	 * @return MailAddressInterface[]
158
	 */
159
	public function getCcRecipients() {
160
		return $this->ccRecipients;
161
	}
162
	
163
	/**
164
	 * An array containing the recipients.
165
	 *
166
	 * @Property
167
	 * @param MailAddressInterface[] $ccRecipients
168
	 */
169
	public function setCcRecipients(array $ccRecipients) {
170
		$this->ccRecipients = $ccRecipients;
171
	}
172
	
173
	/**
174
	 * Adds a recipient.
175
	 *
176
	 * @param MailAddressInterface $ccRecipient
177
	 */
178
	public function addCcRecipient(MailAddressInterface $ccRecipient) {
179
		$this->ccRecipients[] = $ccRecipient;
180
	}
181
	
182
	/**
183
	 * Returns an array containing the recipients in Bcc.
184
	 *
185
	 * @return MailAddressInterface[]
186
	 */
187
	public function getBccRecipients() {
188
		return $this->bccRecipients;
189
	}
190
	
191
	/**
192
	 * An array containing the recipients.
193
	 *
194
	 * @Property
195
	 * @param MailAddressInterface[] $bccRecipients
196
	 */
197
	public function setBccRecipients(array $bccRecipients) {
198
		$this->bccRecipients = $bccRecipients;
199
	}
200
	
201
	/**
202
	 * Adds a recipient.
203
	 *
204
	 * @param MailAddressInterface $bccRecipient
205
	 */
206
	public function addBccRecipient(MailAddressInterface $bccRecipient) {
207
		$this->bccRecipients[] = $bccRecipient;
208
	}
209
	
210
	/**
211
	 * Returns an array of attachements for that mail.
212
	 *
213
	 * @return array<MailAttachmentInterface>
214
	 */
215
	public function getAttachements() {
216
		return $this->attachements;
217
	}
218
	
219
	/**
220
	 * An array containing the attachments.
221
	 *
222
	 * @Property
223
	 * @param array<MailAttachmentInterface> $attachements
224
	 */
225
	public function setAttachements(array $attachements) {
226
		$this->attachements = $attachements;
227
	}
228
	
229
	/**
230
	 * Adds an attachment.
231
	 *
232
	 * @param MailAttachmentInterface attachement
233
	 */
234
	public function addAttachement(MailAttachmentInterface $attachement) {
235
		$this->attachements[] = $attachement;
236
	}
237
	
238
	/**
239
	 * Returns the encoding of the mail.
240
	 *
241
	 * @return string
242
	 */
243
	public function getEncoding() {
244
		return $this->encoding;
245
	}
246
	
247
	/**
248
	 * The mail encoding. Defaults to utf-8.
249
	 *
250
	 * @Property
251
	 * @param string $encoding
252
	 */
253
	public function setEncoding($encoding) {
254
		$this->encoding = $encoding;
255
	}
256
	
257
	/**
258
	 * If no body text is set for that mail, and if autoCreateBodyText is set to true, this object will create the body text from the body HTML text,
259
	 * by removing any tags.
260
	 * 
261
	 * @param boolean $autoCreate
262
	 */
263
	public function autoCreateBodyText($autoCreate) {
264
		$this->autocreateMissingText = $autoCreate;
265
	}
266
	
267
    /**
268
     * Registers some CSS to be applied to the HTML.
269
     * When sending the mail, the CSS will be DIRECTLY applied to the HTML, resulting in some HTML with inline CSS.
270
     * 
271
     * CSS is inlined using the Emogrifier library.
272
     * 
273
     * @param string $css The CSS to apply.
274
     */
275
    public function addCssText($css) {
276
    	$this->css .= $css; 
277
    }
278
    
279
    /**
280
     * Registers a CSS file to be applied to the HTML.
281
     * When sending the mail, the CSS will be DIRECTLY applied to the HTML, resulting in some HTML with inline CSS.
282
     *
283
     * CSS is inlined using the Emogrifier library.
284
     *
285
     * @param string $file The CSS file to apply.
286
     */
287
    public function addCssFile($file) {
288
    	$this->css .= file_get_contents($file);
289
    }
290
}
291