Completed
Push — master ( f7bed6...7b8b9a )
by Gabriel
05:48 queued 11s
created

MailableTrait::afterSend()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 3
crap 2
1
<?php
2
3
namespace Nip\Mail\Traits;
4
5
use Nip\Mail\Mailer;
6
use Nip\Mail\Message;
7
8
/**
9
 * Class MailableTrait
10
 * @package Nip\Mail\Traits
11
 */
12
trait MailableTrait
13
{
14
    use MailerAwareTrait;
15
16
    /**
17
     * @return int
18
     */
19
    public function send()
20
    {
21
        $mailer = $this->getMailer();
22
        $message = $this->buildMailMessage();
23
24
        $this->beforeSend($mailer, $message);
0 ignored issues
show
Bug introduced by
It seems like $mailer defined by $this->getMailer() on line 21 can be null; however, Nip\Mail\Traits\MailableTrait::beforeSend() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
25
        $recipients = $mailer->send($message);
26
        $this->afterSend($mailer, $message, $recipients);
0 ignored issues
show
Bug introduced by
It seems like $mailer defined by $this->getMailer() on line 21 can be null; however, Nip\Mail\Traits\MailableTrait::afterSend() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
27
28
        return $recipients;
29
    }
30
31
    /**
32
     * @return Message
33
     */
34
    public function buildMailMessage()
35
    {
36
        $message = $this->newMailMessage();
37
        $this->buildMailMessageFrom($message);
38
        $this->buildMailMessageRecipients($message);
39
        $this->buildMailMessageSubject($message);
40
        $this->buildMailMessageBody($message);
41
        $this->buildMailMessageAttachments($message);
42
        $this->buildMailMessageMergeTags($message);
43
        $this->buildMailMessageCustomArgs($message);
44
45
        return $message;
46
    }
47
48
    /**
49
     * @return Message
50
     */
51 1
    public function newMailMessage()
52
    {
53 1
        $message = new Message();
54
55 1
        return $message;
56
    }
57
58
    /**
59
     * @param Mailer $mailer
60
     * @param Message $message
61
     */
62
    protected function beforeSend($mailer, $message)
0 ignored issues
show
Unused Code introduced by
The parameter $mailer is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $message is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
63
    {
64
    }
65
66
    /**
67
     * @param Mailer $mailer
68
     * @param Message $message
69
     * @param int $recipients
70
     */
71
    protected function afterSend($mailer, $message, $recipients)
0 ignored issues
show
Unused Code introduced by
The parameter $mailer is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $message is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $recipients is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
72
    {
73
    }
74
75
    /**
76
     * @param Message $message
77
     */
78
    abstract public function buildMailMessageFrom(&$message);
79
80
    /**
81
     * @param Message $message
82
     */
83
    abstract public function buildMailMessageRecipients(&$message);
84
85
    /**
86
     * @param Message $message
87
     */
88
    abstract public function buildMailMessageSubject(&$message);
89
90
    /**
91
     * @param Message $message
92
     */
93
    abstract public function buildMailMessageBody(&$message);
94
95
    /**
96
     * @param Message $message
97
     */
98
    abstract public function buildMailMessageAttachments(&$message);
99
100
    /**
101
     * @param Message $message
102
     */
103
    abstract public function buildMailMessageMergeTags(&$message);
104
105
    /**
106
     * @param Message $message
107
     */
108
    abstract public function buildMailMessageCustomArgs(&$message);
109
}
110