MessageHelper::getRecipients()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 15
ccs 11
cts 11
cp 1
rs 9.4285
cc 2
eloc 9
nc 2
nop 2
crap 2
1
<?php
2
3
namespace ElfSundae\Multimail;
4
5
class MessageHelper
6
{
7
    /**
8
     * Get all recipients for the given message, including To, Reply-To,
9
     * Cc and Bcc addresses.
10
     *
11
     * When the `$associated` is true, this method will return an associative
12
     * array, whereby the keys provide the actual email addresses and the values
13
     * provide the display names.
14
     *
15
     * @param  \Swift_Mime_Message|\Illuminate\Mail\Message  $message
16
     * @param  bool  $associated
17
     * @return string[]
18
     */
19 2
    public static function getRecipients($message, $associated = false)
20
    {
21 2
        $recipients = array_merge(
22 2
            (array) $message->getTo(),
23 2
            (array) $message->getReplyTo(),
24 2
            (array) $message->getCc(),
25 2
            (array) $message->getBcc()
26 2
        );
27
28 2
        if (! $associated) {
29 2
            $recipients = array_keys($recipients);
30 2
        }
31
32 2
        return $recipients;
33
    }
34
35
    /**
36
     * Get domains of the email addresses for the message recipients.
37
     *
38
     * @param  \Swift_Mime_Message|\Illuminate\Mail\Message  $message
39
     * @return string[]
40
     */
41 1
    public static function getRecipientsDomains($message)
42
    {
43 1
        return array_values(array_unique(array_map(
44 1
            function ($address) {
45 1
                return strtolower(last(explode('@', $address)));
46 1
            },
47 1
            static::getRecipients($message, false)
48 1
        )));
49
    }
50
}
51