Completed
Push — master ( 9b79e1...6f2205 )
by Elf
02:06
created

SwiftMessageHelper::getRecipients()   A

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 0
Metric Value
c 0
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
use Swift_Mime_Message;
6
7
class SwiftMessageHelper
8
{
9
    /**
10
     * Get all recipients for the given message, including To, Reply-To,
11
     * Cc and Bcc addresses.
12
     *
13
     * When the `$associated` is true, this method will return an associative
14
     * array, whereby the keys provide the actual email addresses and the values
15
     * provide the display names.
16
     *
17
     * @param  \Swift_Mime_Message  $message
18
     * @param  bool  $associated
19
     * @return string[]
20
     */
21 2
    public static function getRecipients(Swift_Mime_Message $message, $associated = false)
22
    {
23 2
        $recipients = array_merge(
24 2
            (array) $message->getTo(),
25 2
            (array) $message->getReplyTo(),
26 2
            (array) $message->getCc(),
27 2
            (array) $message->getBcc()
28 2
        );
29
30 2
        if (! $associated) {
31 2
            $recipients = array_keys($recipients);
32 2
        }
33
34 2
        return $recipients;
35
    }
36
37
    /**
38
     * Get domains of the email addresses for the message recipients.
39
     *
40
     * @param  \Swift_Mime_Message  $message
41
     * @return string[]
42
     */
43 1
    public static function getRecipientsDomains(Swift_Mime_Message $message)
44
    {
45 1
        return array_values(array_unique(array_map(
46 1
            function ($address) {
47 1
                return strtolower(last(explode('@', $address)));
48 1
            },
49 1
            static::getRecipients($message, false)
50 1
        )));
51
    }
52
}
53