sendTransactionalEmail()   B
last analyzed

Complexity

Conditions 9
Paths 1

Size

Total Lines 33
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 15.0484

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 9
eloc 17
c 1
b 0
f 0
nc 1
nop 11
dl 0
loc 33
ccs 11
cts 19
cp 0.5789
crap 15.0484
rs 8.0555

How to fix   Many Parameters   

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
 * File: TransactionalEmailHandler.php
4
 *
5
 * @author      Maciej Sławik <[email protected]>
6
 * Github:      https://github.com/maciejslawik
7
 */
8
9
namespace MSlwk\FreshMail\Handler\Message;
10
11
use MSlwk\FreshMail\Handler\AbstractHandler;
12
13
class TransactionalEmailHandler extends AbstractHandler
14
{
15
    const API_ENDPOINT = '/rest/mail';
16
17
    /**
18
     * @param string $email
19
     * @param string $subject
20
     * @param string $content
21
     * @param string $fromEmail
22
     * @param string $fromName
23
     * @param string $replyTo
24
     * @param string $encoding
25
     * @param string $attachmentUrl
26
     * @param string $tag
27
     * @param bool $tracking
28
     * @param string $domain
29
     * @return null
30
     */
31 112
    public function sendTransactionalEmail(
32
        string $email,
33
        string $subject,
34
        string $content,
35
        string $fromEmail = '',
36
        string $fromName = '',
37
        string $replyTo = '',
38
        string $encoding = 'UTF-8',
39
        string $attachmentUrl = '',
40
        string $tag = '',
41
        bool $tracking = false,
42
        string $domain = ''
43
    ) {
44 112
        $getParameters = [];
45 32
        $postParameters = [
46 112
            'subscriber' => $email,
47 112
            'subject' => $subject,
48 112
            'html' => $content,
49
            'from' => $fromEmail ?: null,
50
            'from_name' => $fromName ?: null,
51
            'reply_to' => $replyTo ?: null,
52
            'encoding' => $encoding ?: null,
53
            'attachments' => $attachmentUrl ?: null,
54
            'tracking' => $tracking ?: null,
55
            'domain' => $domain ?: null,
56
            'tag' => $tag ?: null
57
        ];
58
59 80
        $postParameters = array_filter($postParameters, function ($value) {
60 112
            return $value !== null;
61 112
        });
62 112
        $this->processRequest($getParameters, $postParameters);
63 7
        return null;
64
    }
65
66
    /**
67
     * @return string
68
     */
69 7
    protected function getApiEndpoint(): string
70
    {
71 7
        return self::API_ENDPOINT;
72
    }
73
}
74