Test Failed
Push — develop ( 4f588d...bd3d82 )
by nguereza
03:51
created

MailHelper::sendReportMailAsync()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 7
dl 0
loc 11
rs 10
c 0
b 0
f 0
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
     * @param bool $async whether to send using asynchronous method
75
     *
76
     * @return bool
77
     */
78
    public function sendReportMail(
79
        int|string $reportId,
80
        string $object,
81
        string|array $receiverAddress,
82
        array $data = [],
83
        array $attachments = [],
84
        string $senderAddress = '',
85
        string $senderName = '',
86
        bool $async = false,
87
    ): bool {
88
        if ($async) {
89
            return $this->sendReportMailAsync(
90
                $reportId,
91
                $object,
92
                $receiverAddress,
93
                $data,
94
                $attachments,
95
                $senderAddress,
96
                $senderName
97
            );
98
        }
99
        $content = $this->printHelper->getReportContent($reportId);
100
101
        return $this->sendMail(
102
            $reportId,
103
            $content,
104
            $object,
105
            $receiverAddress,
106
            $data,
107
            $attachments,
108
            $senderAddress,
109
            $senderName
110
        );
111
    }
112
113
    /**
114
     * Send the asynchronously mail using report content
115
     * @param int|string $reportId
116
     * @param string $object
117
     * @param string|array<string> $receiverAddress
118
     * @param array<string, mixed> $data
119
     * @param array<int|string, string> $attachments
120
     * @param string $senderAddress
121
     * @param string $senderName
122
     * @return bool
123
     */
124
    public function sendReportMailAsync(
125
        int|string $reportId,
0 ignored issues
show
Unused Code introduced by
The parameter $reportId is not used and could be removed. ( Ignorable by Annotation )

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

125
        /** @scrutinizer ignore-unused */ int|string $reportId,

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

Loading history...
126
        string $object,
0 ignored issues
show
Unused Code introduced by
The parameter $object is not used and could be removed. ( Ignorable by Annotation )

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

126
        /** @scrutinizer ignore-unused */ string $object,

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

Loading history...
127
        string|array $receiverAddress,
0 ignored issues
show
Unused Code introduced by
The parameter $receiverAddress is not used and could be removed. ( Ignorable by Annotation )

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

127
        /** @scrutinizer ignore-unused */ string|array $receiverAddress,

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

Loading history...
128
        array $data = [],
0 ignored issues
show
Unused Code introduced by
The parameter $data is not used and could be removed. ( Ignorable by Annotation )

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

128
        /** @scrutinizer ignore-unused */ array $data = [],

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

Loading history...
129
        array $attachments = [],
0 ignored issues
show
Unused Code introduced by
The parameter $attachments is not used and could be removed. ( Ignorable by Annotation )

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

129
        /** @scrutinizer ignore-unused */ array $attachments = [],

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

Loading history...
130
        string $senderAddress = '',
0 ignored issues
show
Unused Code introduced by
The parameter $senderAddress is not used and could be removed. ( Ignorable by Annotation )

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

130
        /** @scrutinizer ignore-unused */ string $senderAddress = '',

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

Loading history...
131
        string $senderName = ''
0 ignored issues
show
Unused Code introduced by
The parameter $senderName is not used and could be removed. ( Ignorable by Annotation )

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

131
        /** @scrutinizer ignore-unused */ string $senderName = ''

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

Loading history...
132
    ): bool {
133
        // TODO: implement me
134
        return true;
135
    }
136
137
138
    /**
139
     * Main function to send the mail
140
     * @param int|string $reportId this parameter is used only for
141
     * debug of report content
142
     * @param string $content
143
     * @param string $object
144
     * @param string|array<string> $receiverAddress
145
     * @param array<string, mixed> $data
146
     * @param array<int|string, string> $attachments
147
     * @param string $senderAddress
148
     * @param string $senderName
149
     * @return bool
150
     */
151
    public function sendMail(
152
        int|string $reportId,
153
        string $content,
154
        string $object,
155
        string|array $receiverAddress,
156
        array $data = [],
157
        array $attachments = [],
158
        string $senderAddress = '',
159
        string $senderName = ''
160
    ): bool {
161
        // @codeCoverageIgnoreStart
162
        if ($this->isEnabled() === false) {
0 ignored issues
show
introduced by
The condition $this->isEnabled() === false is always false.
Loading history...
163
            return true;
164
        }
165
        // @codeCoverageIgnoreEnd
166
167
        if (empty($receiverAddress)) {
168
            return false;
169
        }
170
171
        $mainInformations = $this->printHelper->getMainData();
172
        if (empty($senderAddress)) {
173
            $senderAddress = $this->getSenderEmail();
174
        }
175
176
        if (empty($senderName)) {
177
            $senderName = $this->config->get('app.name');
178
        }
179
180
        $reportData = $data + $mainInformations;
181
182
        $receivers = Arr::wrap($receiverAddress);
183
184
        $mailer = new Mailer($this->transport);
185
186
        foreach ($receivers as $receiver) {
187
            /*
188
             * TODO: normally the next lines should not be added in the loop
189
             * but in order to customized the message content based on receiver user
190
             * we had it here in the loop the in the mail template we can know which user is
191
             * the current receiver and adapt the mail content based on the receiver email
192
             * address.
193
             */
194
            $reportData['receiver_email'] = $receiver;
195
            $mailBody = $this->template->renderString($content, $reportData);
196
            // If need debug
197
            $this->printHelper->debugReport($reportId, $reportData);
198
199
200
            $message = new Message();
201
            $message->setFrom($senderAddress, $senderName)
202
                    ->setTo($receiver)
203
                    ->setSubject($object)
204
                    ->setBody($mailBody)
205
                    ->setHtml();
206
207
            foreach ($attachments as $name => $path) {
208
                if (is_string($name)) {
209
                    $message->addAttachment($path, $name);
210
                } else {
211
                    $message->addAttachment($path);
212
                }
213
            }
214
215
            if ($mailer->send($message) === false) {
216
                return false;
217
            }
218
        }
219
220
        return true;
221
    }
222
223
    /**
224
     * Whether the sending mail feature is enabled or not
225
     * @return bool
226
     */
227
    public function isEnabled(): bool
228
    {
229
        return true;
230
    }
231
232
    /**
233
     * Return the sender email address
234
     * @return string
235
     */
236
    protected function getSenderEmail(): string
237
    {
238
        return '';
239
    }
240
}
241