Passed
Push — develop ( d3c53a...e28085 )
by nguereza
14:20
created

MailHelper::sendMail()   B

Complexity

Conditions 9
Paths 30

Size

Total Lines 61
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 9
eloc 29
c 1
b 0
f 0
nc 30
nop 8
dl 0
loc 61
rs 8.0555

How to fix   Long Method    Many Parameters   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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
        // If need debug
145
        $this->printHelper->debugReport($reportId, $reportData);
146
147
        $receivers = Arr::wrap($receiverAddress);
148
        $mailBody = $this->template->renderString($content, $reportData);
149
150
        $mailer = new Mailer($this->transport);
151
152
        foreach ($receivers as $receiver) {
153
            $message = new Message();
154
            $message->setFrom($senderAddress, $senderName)
155
                    ->setTo($receiver)
156
                    ->setSubject($object)
157
                    ->setBody($mailBody)
158
                    ->setHtml();
159
160
            foreach ($attachments as $name => $path) {
161
                if (is_string($name)) {
162
                    $message->addAttachment($path, $name);
163
                } else {
164
                    $message->addAttachment($path);
165
                }
166
            }
167
168
            if ($mailer->send($message) === false) {
169
                return false;
170
            }
171
        }
172
173
        return true;
174
    }
175
176
    /**
177
     * Whether the sending mail feature is enabled or not
178
     * @return bool
179
     */
180
    public function isEnabled(): bool
181
    {
182
        return true;
183
    }
184
185
    /**
186
     * Return the sender email address
187
     * @return string
188
     */
189
    protected function getSenderEmail(): string
190
    {
191
        return '';
192
    }
193
}
194