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

Mail::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
1
<?php
2
3
namespace Mouf\Utils\Mailer;
4
5
use Mouf\Html\HtmlElement\HtmlElementInterface;
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
16
{
17
    /**
18
     * @var string
19
     */
20
    protected $bodyText;
21
22
    /**
23
     * @var HtmlElementInterface
24
     */
25
    protected $bodyHtml;
26
27
    /**
28
     * @var HtmlElementInterface
29
     */
30
    protected $title;
31
32
    /**
33
     * @var MailAddressInterface
34
     */
35
    protected $from;
36
37
    /**
38
     * @var MailAddressInterface[]
39
     */
40
    protected $toRecipients = array();
41
42
    /**
43
     * @var MailAddressInterface[]
44
     */
45
    protected $ccRecipients = array();
46
47
    /**
48
     * @var MailAddressInterface[]
49
     */
50
    protected $bccRecipients = array();
51
52
    /**
53
     * @var MailAttachmentInterface[]
54
     */
55
    protected $attachements = array();
56
57
    /**
58
     * @var string
59
     */
60
    protected $encoding = 'utf-8';
61
62
    /**
63
     * @var bool
64
     */
65
    protected $autocreateMissingText = true;
66
67
    /**
68
     * @var string
69
     */
70
    protected $css;
71
72
    public function __construct(string $title, string $bodyText = null)
73
    {
74
        $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...
75
        $this->bodyText = $bodyText;
76
    }
77
78
    /**
79
     * Returns the mail text body.
80
     *
81
     * @return string
82
     */
83
    public function getBodyText() :string
84
    {
85
        return $this->bodyText;
86
    }
87
88
    /**
89
     * The mail text body.
90
     *
91
     * @Property
92
     *
93
     * @param string $bodyText
94
     */
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
    public function setBodyText(string $bodyText) :string
96
    {
97
        $this->bodyText = $bodyText;
98
    }
99
100
    /**
101
     * Returns the mail html body.
102
     *
103
     * @return string
104
     */
105
    public function getBodyHtml():string
106
    {
107
        return $this->bodyHtml->getHtml();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Mouf\Html\HtmlElement\HtmlElementInterface as the method getHtml() does only exist in the following implementations of said interface: Mouf\Html\HtmlElement\HtmlBlock.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
108
    }
109
110
    /**
111
     * The mail html body.
112
     *
113
     * @param HtmlElementInterface $bodyHtml
114
     */
115
    public function setBodyHtml(HtmlElementInterface $bodyHtml)
116
    {
117
        $this->bodyHtml = $bodyHtml;
118
    }
119
120
    /**
121
     * Returns the mail title.
122
     *
123
     * @return string
124
     */
125
    public function getTitle():string
126
    {
127
        return $this->title->getHtml();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Mouf\Html\HtmlElement\HtmlElementInterface as the method getHtml() does only exist in the following implementations of said interface: Mouf\Html\HtmlElement\HtmlBlock.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
128
    }
129
130
    /**
131
     * The mail title.
132
     *
133
     * @param string $title
134
     */
135
    public function setTitle(HtmlElementInterface $title)
136
    {
137
        $this->title = $title;
138
    }
139
140
    /**
141
     * Returns the "From" email address.
142
     *
143
     * @return MailAddressInterface The first element is the email address, the second the name to display.
144
     */
145
    public function getFrom():MailAddressInterface
146
    {
147
        return $this->from;
148
    }
149
150
    /**
151
     * The mail from address.
152
     *
153
     * @param MailAddressInterface $from
154
     */
155
    public function setFrom(MailAddressInterface $from)
156
    {
157
        $this->from = $from;
158
    }
159
160
    /**
161
     * Returns an array containing the recipients.
162
     *
163
     * @return MailAddressInterface[]
164
     */
165
    public function getToRecipients(): array
166
    {
167
        return $this->toRecipients;
168
    }
169
170
    /**
171
     * An array containing the recipients.
172
     *
173
     * @param MailAddressInterface[] $toRecipients
174
     */
175
    public function setToRecipients(array $toRecipients)
176
    {
177
        $this->toRecipients = $toRecipients;
178
    }
179
180
    /**
181
     * Adss a recipient.
182
     *
183
     * @param MailAddressInterface $toRecipient
184
     */
185
    public function addToRecipient(MailAddressInterface $toRecipient)
186
    {
187
        $this->toRecipients[] = $toRecipient;
188
    }
189
190
    /**
191
     * Returns an array containing the recipients in Cc.
192
     *
193
     * @return MailAddressInterface[]
194
     */
195
    public function getCcRecipients(): array
196
    {
197
        return $this->ccRecipients;
198
    }
199
200
    /**
201
     * An array containing the recipients.
202
     *
203
     *
204
     * @param MailAddressInterface[]$ccRecipients
205
     */
206
    public function setCcRecipients(array $ccRecipients)
207
    {
208
        $this->ccRecipients = $ccRecipients;
209
    }
210
211
    /**
212
     * Adds a recipient.
213
     *
214
     * @param MailAddressInterface $ccRecipient
215
     */
216
    public function addCcRecipient(MailAddressInterface $ccRecipient)
217
    {
218
        $this->ccRecipients[] = $ccRecipient;
219
    }
220
221
    /**
222
     * Returns an array containing the recipients in Bcc.
223
     *
224
     * @return MailAddressInterface[]
225
     */
226
    public function getBccRecipients():array
227
    {
228
        return $this->bccRecipients;
229
    }
230
231
    /**
232
     * An array containing the recipients.
233
     *
234
     * @param MailAddressInterface[] $bccRecipients
235
     */
236
    public function setBccRecipients(array $bccRecipients)
237
    {
238
        $this->bccRecipients = $bccRecipients;
239
    }
240
241
    /**
242
     * Adds a recipient.
243
     *
244
     * @param MailAddressInterface $bccRecipient
245
     */
246
    public function addBccRecipient(MailAddressInterface $bccRecipient)
247
    {
248
        $this->bccRecipients[] = $bccRecipient;
249
    }
250
251
    /**
252
     * Returns an array of attachements for that mail.
253
     *
254
     * @return MailAttachmentInterface[]
255
     */
256
    public function getAttachements():array
257
    {
258
        return $this->attachements;
259
    }
260
261
    /**
262
     * An array containing the attachments.
263
     *
264
     * @param array<MailAttachmentInterface> $attachements
265
     */
266
    public function setAttachements(array $attachements)
267
    {
268
        $this->attachements = $attachements;
269
    }
270
271
    /**
272
     * Adds an attachment.
273
     *
274
     * @param MailAttachmentInterface $attachement
275
     */
276
    public function addAttachement(MailAttachmentInterface $attachement)
277
    {
278
        $this->attachements[] = $attachement;
279
    }
280
281
    /**
282
     * Returns the encoding of the mail.
283
     *
284
     * @return string
285
     */
286
    public function getEncoding():string
287
    {
288
        return $this->encoding;
289
    }
290
291
    /**
292
     * The mail encoding. Defaults to utf-8.
293
     *
294
     * @param string $encoding
295
     */
296
    public function setEncoding($encoding)
297
    {
298
        $this->encoding = $encoding;
299
    }
300
301
}
302