MailHelper::getSenderEmail()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
/**
4
 * Platine Framework
5
 *
6
 * Platine Framework is a lightweight, high-performance, simple and elegant
7
 * PHP Web framework
8
 *
9
 * This content is released under the MIT License (MIT)
10
 *
11
 * Copyright (c) 2020 Platine Framework
12
 *
13
 * Permission is hereby granted, free of charge, to any person obtaining a copy
14
 * of this software and associated documentation files (the "Software"), to deal
15
 * in the Software without restriction, including without limitation the rights
16
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17
 * copies of the Software, and to permit persons to whom the Software is
18
 * furnished to do so, subject to the following conditions:
19
 *
20
 * The above copyright notice and this permission notice shall be included in all
21
 * copies or substantial portions of the Software.
22
 *
23
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29
 * SOFTWARE.
30
 */
31
32
declare(strict_types=1);
33
34
namespace Platine\Framework\Helper;
35
36
use Platine\Config\Config;
37
use Platine\Mail\Mailer;
38
use Platine\Mail\Message;
39
use Platine\Mail\Transport\TransportInterface;
40
use Platine\Stdlib\Helper\Arr;
41
use Platine\Template\Template;
42
43
/**
44
 * @class MailHelper
45
 * @package Platine\Framework\Helper
46
 * @template T
47
 */
48
class MailHelper
49
{
50
    /**
51
     * Create new instance
52
     * @param Template $template
53
     * @param TransportInterface $transport
54
     * @param PrintHelper<T> $printHelper
55
     * @param Config<T> $config
56
     */
57
    public function __construct(
58
        protected Template $template,
59
        protected TransportInterface $transport,
60
        protected PrintHelper $printHelper,
61
        protected Config $config,
62
    ) {
63
    }
64
65
    /**
66
     * Send the mail using report content
67
     * @param int|string $reportId
68
     * @param string $object
69
     * @param string|array<string> $receiverAddress
70
     * @param array<string, mixed> $data
71
     * @param array<int|string, string> $attachments
72
     * @param string $senderAddress
73
     * @param string $senderName
74
     * @return bool
75
     */
76
    public function sendReportMail(
77
        int|string $reportId,
78
        string $object,
79
        string|array $receiverAddress,
80
        array $data = [],
81
        array $attachments = [],
82
        string $senderAddress = '',
83
        string $senderName = ''
84
    ): bool {
85
        $content = $this->printHelper->getReportContent($reportId);
86
87
        return $this->sendMail(
88
            $reportId,
89
            $content,
90
            $object,
91
            $receiverAddress,
92
            $data,
93
            $attachments,
94
            $senderAddress,
95
            $senderName
96
        );
97
    }
98
99
100
    /**
101
     * Main function to send the mail
102
     * @param int|string $reportId this parameter is used only for
103
     * debug of report content
104
     * @param string $content
105
     * @param string $object
106
     * @param string|array<string> $receiverAddress
107
     * @param array<string, mixed> $data
108
     * @param array<int|string, string> $attachments
109
     * @param string $senderAddress
110
     * @param string $senderName
111
     * @return bool
112
     */
113
    public function sendMail(
114
        int|string $reportId,
115
        string $content,
116
        string $object,
117
        string|array $receiverAddress,
118
        array $data = [],
119
        array $attachments = [],
120
        string $senderAddress = '',
121
        string $senderName = ''
122
    ): bool {
123
        // @codeCoverageIgnoreStart
124
        if ($this->isEnabled() === false) {
0 ignored issues
show
introduced by
The condition $this->isEnabled() === false is always false.
Loading history...
125
            return true;
126
        }
127
        // @codeCoverageIgnoreEnd
128
129
        if (empty($receiverAddress)) {
130
            return false;
131
        }
132
133
        $mainInformations = $this->printHelper->getMainData();
134
        if (empty($senderAddress)) {
135
            $senderAddress = $this->getSenderEmail();
136
        }
137
138
        if (empty($senderName)) {
139
            $senderName = $this->config->get('app.name');
140
        }
141
142
        $reportData = $data + $mainInformations;
143
144
        $receivers = Arr::wrap($receiverAddress);
145
146
        $mailer = new Mailer($this->transport);
147
148
        foreach ($receivers as $receiver) {
149
            /*
150
             * TODO: normally the next lines should not be added in the loop
151
             * but in order to customized the message content based on receiver user
152
             * we had it here in the loop the in the mail template we can know which user is
153
             * the current receiver and adapt the mail content based on the receiver email
154
             * address.
155
             */
156
            $reportData['receiver_email'] = $receiver;
157
            $mailBody = $this->template->renderString($content, $reportData);
158
            // If need debug
159
            $this->printHelper->debugReport($reportId, $reportData);
160
161
162
            $message = new Message();
163
            $message->setFrom($senderAddress, $senderName)
164
                    ->setTo($receiver)
165
                    ->setSubject($object)
166
                    ->setBody($mailBody)
167
                    ->setHtml();
168
169
            foreach ($attachments as $name => $path) {
170
                if (is_string($name)) {
171
                    $message->addAttachment($path, $name);
172
                } else {
173
                    $message->addAttachment($path);
174
                }
175
            }
176
177
            if ($mailer->send($message) === false) {
178
                return false;
179
            }
180
        }
181
182
        return true;
183
    }
184
185
    /**
186
     * Whether the sending mail feature is enabled or not
187
     * @return bool
188
     */
189
    public function isEnabled(): bool
190
    {
191
        return true;
192
    }
193
194
    /**
195
     * Return the sender email address
196
     * @return string
197
     */
198
    protected function getSenderEmail(): string
199
    {
200
        return '';
201
    }
202
}
203