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

SwiftMessageHelper   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
c 1
b 0
f 0
lcom 0
cbo 0
dl 0
loc 46
ccs 18
cts 18
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getRecipients() 0 15 2
A getRecipientsDomains() 0 9 1
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