RecordTrait::buildMailMessageFrom()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Nip\Mail\Models\Mailable;
6
7
use Nip\Mail\Message;
8
use Nip\Mail\Traits\MailableTrait;
9
use Nip\Mail\Utility\Address;
10
11
/**
12
 * Class RecordTrait.
13
 */
14
trait RecordTrait
15
{
16
    use MailableTrait;
17
18
    /**
19
     * @param Message $message
20
     */
21
    public function buildMailMessageFrom(&$message)
22
    {
23
        $message->setFrom($this->getFrom());
0 ignored issues
show
Deprecated Code introduced by
The function Nip\Mail\Message::setFrom() has been deprecated: use addFrom() instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

23
        /** @scrutinizer ignore-deprecated */ $message->setFrom($this->getFrom());

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
24
    }
25
26
    /**
27
     * @return string
28
     */
29
    abstract public function getFrom();
30
31
    /**
32
     * @param Message $message
33
     */
34
    public function buildMailMessageRecipients(&$message)
35
    {
36
        foreach (['to', 'cc', 'bcc', 'replyTo'] as $type) {
37
            $method = 'get'.ucfirst($type).'s';
38
            $recipients = method_exists($this, $method) ? $this->{$method}() : $this->{$type};
39
            if (is_array($recipients)) {
40
                $message->{'add'.ucfirst($type)}(...Address::fromArray($recipients));
41
            }
42
        }
43
    }
44
45
    /**
46
     * @param Message $message
47
     */
48
    public function buildMailMessageSubject(&$message)
49
    {
50
        $message->subject((string) $this->getSubject());
51
    }
52
53
    /**
54
     * @return ?string
55
     */
56
    abstract public function getSubject(): ?string;
57
58
    /**
59
     * @param Message $message
60
     */
61
    public function buildMailMessageBody(&$message)
62
    {
63
        $message->html($this->getBody());
64
    }
65
66
    /**
67
     * @return string
68
     */
69
    abstract public function getBody();
70
71
    /**
72
     * @param Message $message
73
     */
74
    public function buildMailMessageAttachments(&$message)
75
    {
76
    }
77
78
    /**
79
     * @param Message $message
80
     */
81
    public function buildMailMessageMergeTags(&$message)
82 1
    {
83
        $mergeTags = $this->getMergeTags();
84 1
        $body = $this->getBody();
85 1
        foreach ($mergeTags as $tag => $value) {
86 1
            $encapsulated = \Nip\Mail\Models\MergeTags\RecordTrait::encapsulate($tag);
87 1
            if (false !== strpos($body, $encapsulated)) {
88 1
                $message->addMergeTag($tag, $value);
89 1
            }
90
        }
91
    }
92 1
93
    /**
94
     * @return array
95
     */
96
    abstract public function getMergeTags();
97
98
    /**
99
     * @param Message $message
100
     */
101
    public function buildMailMessageCustomArgs(&$message)
102
    {
103
        $message->setCustomArgs($this->getCustomArgs());
104
    }
105
106
    /**
107
     * @return array
108
     */
109
    abstract protected function getCustomArgs();
110
111
    /**
112
     * @return string
113
     */
114
    abstract public function getTos();
115
}
116